Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

PhP Arrays


  • Please log in to reply

#1
Luke1988

Luke1988

    Member

  • Member
  • PipPip
  • 37 posts
i have created an array to take 10 values and output them in ascending and descending order. i now need to modify it so that it asks for the number of elements to be input, starting with a default of 10. this is the bit im struggling to work out.

my code so far is below, thanks for any help

**Luke**

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Basic Output</title>
<link href="BasicOutput.css" rel="stylesheet" type="text/css" />
</head>
<body>

<form method="post" action="example.php">
	<input type="text" name="names[]" id="txt1" value="Name 1" />
	<input type="text" name="names[]" id="txt2" value="Name 2" />
	<input type="text" name="names[]" id="txt1" value="Name 3" />
	<input type="text" name="names[]" id="txt1" value="Name 4" />
	<input type="text" name="names[]" id="txt1" value="Name 5" />
	<input type="text" name="names[]" id="txt1" value="Name 6" />
	<input type="text" name="names[]" id="txt1" value="Name 7" />
	<input type="text" name="names[]" id="txt1" value="Name 8" />
	<input type="text" name="names[]" id="txt1" value="Name 9" />
	<input type="text" name="names[]" id="txt1" value="Name 10" />
	<input type="submit" value="Submit" />

</form>
<?php

$names = $_POST['names'];

echo "<br />";
echo ("Array Output");
echo "<br />";

foreach ($names as $value)
{
echo "Value: " . $value . "<br />";
}


echo "<br />";
echo ("Array Ascending"); 
echo "<br />";

sort($names);
foreach ($names as $value)
{
echo "Value: " . $value . "<br />";
}

echo "<br />";
echo ("Array Descending"); 
echo "<br />";

rsort($names);
foreach ($names as $value)
{
echo "Value: " . $value . "<br />";
}

?>

</body>
</html>

  • 0

Advertisements







Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP