954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Create array from form using a loop

Hi,

I am trying to create a loop that will create an array from the values posted into a form and then sort them. The user is first asked how many words they want to enter and then a loop creates a form with the appropriate number of text boxes and then an array is created from the values entered and the values are then sorted when the sort button is pressed. However, for some reason it's not creating the array. I have been working at this for 3 days now and I've got to admit defeat, where am I going wrong? (There are some commented out bits form where I was trying to get it working but I've a feeling the problem is the first part of the php, probably the loop)

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Exercise 2 4</title>

</head>
<style type="text/css">

form label {
  display: inline-block;
  width: 150px;
  font-family:Verdana, Geneva, sans-serif;
}

body {font-family:Verdana, Geneva, sans-serif;
}
</style>

<body>
<h1>Sorting Arrays</h1>
<h3>Please enter how words you would like to sort and then click 'sort'</h3>
<form method="post" action="">
<label for="num">Number of words:</label><input type="text" name="num" />
<p><input type="submit" value="Enter" name="enter" /></p>
<p><input type="submit" value="Sort" name="submit" /></p>

</form>


<!-- create table to display results-->
<?php 
if(isset($_POST['enter'])){
	$num=$_POST['num'];
	//while($i <= $num){
		for ($i=1;$i<=$num;$i++){
		echo "<form>";
		echo "<label for=\"words$i\">Word $i</label><input type=\"text\" name=\"words[$i]\" id=\"words$i\" />";
		//$words =($_POST['words']);
		
	}
	echo "</form>";
	
}
print_r($words);


echo '<table border="0">';
  echo '<tr>';
   echo '<td width="150" td align="center"><u>Values</u></td>';  
    echo '<td width="150" td align="center"><u>Ascending</u></td>';
    echo '<td width="150" td align="center"><u>Descending</u></td>';
  echo '</tr>';
  echo '<tr>';
    echo '<td align="center">';
   
	print_r($words);
	//when sort is pressed display the values entered
if(isset($_POST['submit'])){
	print_r($words);
//$words =$_POST['words'];
foreach ($words as $value){
	echo $value.'';
//}
}
    echo '</td>';
    echo '<td align="center">';
		//when sort is pressed display the values entered in ascending orders
if(isset($_POST['submit'])){
//$words =$_POST['words'];
asort($words);
foreach ($words as $value){
	echo $value.'';
}
   echo '</td>';
    echo '<td align="center">';
		//when sort is pressed display the values in descending order
if(isset($_POST['submit'])){
//$words =$_POST['words'];
rsort($words);
foreach ($words as $value){
	echo $value.'';
}
}
   echo '</td>';
  echo '</tr>';
echo '</table>';
}
?>

</body>
</html>
tatt727
Newbie Poster
11 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

Hi,

I did operations like you do (with the array from forms), very similar to the techniques you are using and everything worked well.

What I see as a possible error at the code could be that every form tag must have ist own submit (between the and , because you can have more forms on a page, so how do you know which one the users wants to submit? And you are trying to use the submit from the number of words to sort the entered words.

Try to place the submit into the second form.

I hope it helped. Let me know pls.

gotschai
Newbie Poster
16 posts since Sep 2009
Reputation Points: 10
Solved Threads: 2
 

Hi,

I did operations like you do (with the array from forms), very similar to the techniques you are using and everything worked well.

What I see as a possible error at the code could be that every form tag must have ist own submit (between the and , because you can have more forms on a page, so how do you know which one the users wants to submit? And you are trying to use the submit from the number of words to sort the entered words.

Try to place the submit into the second form.

I hope it helped. Let me know pls.

Hi,

Thanks for the reply. I moved the submit to the second form and we have some progess (yay!). Now the array at least appears to get data in it as the print_r functions I put in to test are now printing out it's contents but it still not sorting the contents or displying the results in the tables

tatt727
Newbie Poster
11 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

hi, I have tried the code, you have to add also the form method in the second form, as default is get (maybe in your php config not, I just notices the variables in the address bar). It is good to know that theese array operations with forms do not work with get method.

Hope it helps.

gotschai
Newbie Poster
16 posts since Sep 2009
Reputation Points: 10
Solved Threads: 2
 

Hi, Thanks again.

Did you manage to get the code working? I made the changes but it still won't display in the tables. I have worked out that the point that it gets stuck if the first if(isset($_POST['submit'])). Up unitll that point print_r will display the array contents, after that it won't.

Thanks again for the help you are giving me, this has been driving me nutst

tatt727
Newbie Poster
11 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

Hi,

After a brackets check and the corrections discussed before it is working.
I attached the file, try to take a look on it and correct what you have wrong.

Good luck

Attachments test.zip (0.91KB)
gotschai
Newbie Poster
16 posts since Sep 2009
Reputation Points: 10
Solved Threads: 2
 

Got ya! No action in the second form, I guess this is because this was in the first form? And $i in the words[$i] in the form loop (I have to admit I put that in and took it out a million times whilst trying to get this working!). Lastly the baces were in the wrong place after the for each functions, they should've been after the echo $value and before the table seperators.

Again, thanks for that, I think when you look at something too long it becomes counter productive but at least I know I wasn't too far off the mark and I've been given some very valuable advice too.

Thanks again

tatt727
Newbie Poster
11 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

Tatt,

for ($i=1;$i<=$num;$i++){
	echo "<form>";
	echo "<label for=\"words$i\">Word $i</label><input type=\"text\" name=\"words[$i]\" id=\"words$i\" />";
	}
echo "</form>";


<form> is inside loop and </form> is outside. This will mess things up bigtime.

Either both inside or both outside depending on what you want. As it stands the code will generate lots of unclosed forms.Airshow

Airshow
WiFi Lounge Lizard
Moderator
2,683 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
 

Thanks for that! Something to watch out for in the future!

tatt727
Newbie Poster
11 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You