PhP Arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
frnkln1988
Forum Newbie
Posts: 3
Joined: Fri Nov 28, 2008 11:19 am

PhP Arrays

Post 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>
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: PhP Arrays

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: PhP Arrays

Post by RobertGonzalez »

Does it need to build the requested number of input fields? IS that what you are asking?
frnkln1988
Forum Newbie
Posts: 3
Joined: Fri Nov 28, 2008 11:19 am

Re: PhP Arrays

Post by frnkln1988 »

yea it does need to build the requested number of input fields.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: PhP Arrays

Post 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.
frnkln1988
Forum Newbie
Posts: 3
Joined: Fri Nov 28, 2008 11:19 am

Re: PhP Arrays

Post 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**
Post Reply