PhP Arrays
Posted: Fri Nov 28, 2008 11:21 am
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**
my code so far is below, thanks for any help
**Luke**
Code: Select all
<!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>