Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
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