Page 1 of 1

PhP Arrays

Posted: Fri Nov 28, 2008 11:21 am
by frnkln1988
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**

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>

Re: PhP Arrays

Posted: Sun Nov 30, 2008 3:14 pm
by omniuni
My first impression is have a page that asks for the number, use 'is_numeric()' to check and then use a PHP loop to display the form.

-OmniUni

Re: PhP Arrays

Posted: Sun Nov 30, 2008 11:29 pm
by RobertGonzalez
Does it need to build the requested number of input fields? IS that what you are asking?

Re: PhP Arrays

Posted: Mon Dec 01, 2008 6:38 am
by frnkln1988
yea it does need to build the requested number of input fields.

Re: PhP Arrays

Posted: Mon Dec 01, 2008 12:34 pm
by RobertGonzalez
Use a for loop and loop to the supplied number of iterations. Of course, you should validate the input the user offers to make sure it is numeric, greater than 0, things like that. But the loop is fairly straight forward.

Code: Select all

<?php
$limit = 10; // Set a default
$max = 100; // Set a maximum so the user doesn't loop your server into oblivion
$min = 1; // Set a minimum so you can actually have something to show
 
// DId the user send you something?
if (isset($_POST['userinput'])) {
  $cap = $_POST['userinput'];
  // Is the something the user sent acceptable?
  if (is_numeric($cap) && $cap >= $min && $cap <= $max) {
    $limit = $cap;
  }
}
 
// Start the loop, at one since your inputs and outputs are 1 based
for ($i = 1; $i <= $limit; $i++) {
  // echo your stuff here using $i as your counter
}
?>
Play around with this a little bit. I am sure you can use it as a basis for something like what you want to do.

Re: PhP Arrays

Posted: Wed Dec 03, 2008 1:53 pm
by frnkln1988
cheers, i worked it out before you replied but my solution was near enough the same as yours.

thanks for the help.

**Luke**