Page 1 of 2

Getting loop-generated values

Posted: Mon Aug 14, 2006 12:02 am
by tristanlee85
To see the layout of my page, check out http://stats.fedexunderground.com/add_stats.php

The layout you see is generated from a while loop pulling the door numbers from a database like so:

Code: Select all

//Get the layout fields from the database
echo "<table width='50%' align='center' border='0'><tr><td align=\"center\" width=\"40%\"><table border=\"0\" width=\"100%\" align=\"center\"></td></tr>
<tr>
<td align=\"center\" colspan='2'><b>Door list</b></td>
</tr>";
if ($door_list == 0)
{
	echo "There are no load doors in the databse";
}
else
{
$count=0;
while ($doornum = mysql_fetch_row($door_num)) {
	$doorname = mysql_fetch_row($door_name);
	//$loadername = mysql_fetch_row($loader_name);
	
         echo "<tr align='center'><td align='right'> <b>Door $doornum[0]</b></td><td align='center'>Misloaded zips: <input type='text' name='zips_$doornum[0]'><input type='hidden' name='$doornum[0]' value='$doornum[0]'></td></tr>";
}
$count++;
}
echo "</table>";
?>
That code is what generates the layout you see. Now my problem is trying to $_POST all of these fields. I'm basically going blank here. Since the layout depends on the database informartion, I really can't set specific $_POST calls since the number of posted values can change depending on the database.

Right now I'm simply trying to do tests by echo-ing out what's posted. My $_POST statment is as follows:

Code: Select all

if (isset($_POST['post'])) {
$zips = $_POST['zips'];
$door_num = $_POST['door_number'];
$date = $_POST['date'];

echo "$door_num - $zips - $date<br>";
}
With that code, it only displays the very last field that is posted. How can I get information from all of the fields?

Posted: Mon Aug 14, 2006 12:10 am
by RobertGonzalez
How are $_POST and your database fetch/display related here? Is someone posting something in a form and the script is searching? What is the script doing?

Posted: Mon Aug 14, 2006 12:34 am
by tristanlee85
In the link I posted, when you submit that form, it's supposed to submit all of the values to the database. The database table "misloads" consists of these fields:

door_num
misload_zip
date

Let's assume that on the form, a user submits that in door 418 there were misloads with the following zip codes: 43228,43123

When the form is submitted with only filling in the text field for door 418, it should submit to the databse:

Code: Select all

door_num "418"
misload_zip "43228,43123"
date "1155274352"
and since none of the other fields were filled in, it will submit:

Code: Select all

door_num "419"
misload_zip ""
date "1155274352"

Code: Select all

door_num "420"
misload_zip ""
date "1155274352"

Code: Select all

door_num "421"
misload_zip ""
date "1155274352"
and so on...

I really can't describe it in any other way.

Posted: Mon Aug 14, 2006 1:06 am
by RobertGonzalez
The doors come from a database, right? So you use the database to fill up a list so the user has to select a door. Automatically this makes at least one field in the form complete. The date field, have it default to today if the date they pass is invalid or blank. That fills the next field. The last one is the tricky one. I am guessing that you really do not want to store like five different zips all in one row in the database. So you either use checkboxes to allow multiple Zips (although I am not sure the page would allow for all those zips) or you can rely on the input of the user.

What I would do is check to see if the user supplied data is actually there (!empty()) and actually makes sense (is_numeric() & strlen()). If it passes the steps of sensibility, then run the query. Otherwise, error out.

Posted: Mon Aug 14, 2006 1:25 am
by tristanlee85
I kind of got it working. Here is the WHILE loop that creates the form:

Code: Select all

$count=0;
while ($doornum = mysql_fetch_row($door_num)) {
	$doorname = mysql_fetch_row($door_name);
	//$loadername = mysql_fetch_row($loader_name);
	
         echo "<tr align='center'><td align='right'> <b>Door $doornum[0]</b></td><td align='center'>Misloaded zips: <input type='text' name='zips$doornum[0]'><input type='hidden' name='door_number$doornum[0]' value='$doornum[0]'></td></tr>";
}
$count++;
Once the page is generated, it looks like this in HTML:

Code: Select all

<tr align='center'><td align='right'> <b>Door 418</b></td><td align='center'>Misloaded zips: <input type='text' name='zips418'><input type='hidden' name='door_number418' value='418'></td></tr>

<tr align='center'><td align='right'> <b>Door 419</b></td><td align='center'>Misloaded zips: <input type='text' name='zips419'><input type='hidden' name='door_number419' value='419'></td></tr>

<tr align='center'><td align='right'> <b>Door 420</b></td><td align='center'>Misloaded zips: <input type='text' name='zips420'><input type='hidden' name='door_number420' value='420'></td></tr>

<tr align='center'><td align='right'> <b>Door 421</b></td><td align='center'>Misloaded zips: <input type='text' name='zips421'><input type='hidden' name='door_number421' value='421'></td></tr>

<tr align='center'><td align='right'> <b>Door 422</b></td><td align='center'>Misloaded zips: <input type='text' name='zips422'><input type='hidden' name='door_number422' value='422'></td></tr>

<tr align='center'><td align='right'> <b>Door 423</b></td><td align='center'>Misloaded zips: <input type='text' name='zips423'><input type='hidden' name='door_number423' value='423'></td></tr>

<tr align='center'><td align='right'> <b>Door 424</b></td><td align='center'>Misloaded zips: <input type='text' name='zips424'><input type='hidden' name='door_number424' value='424'></td></tr>

<tr align='center'><td align='right'> <b>Door 425</b></td><td align='center'>Misloaded zips: <input type='text' name='zips425'><input type='hidden' name='door_number425' value='425'></td></tr>

<tr align='center'><td align='right'> <b>Door 426</b></td><td align='center'>Misloaded zips: <input type='text' name='zips426'><input type='hidden' name='door_number426' value='426'></td></tr>

<tr align='center'><td align='right'> <b>Door 427</b></td><td align='center'>Misloaded zips: <input type='text' name='zips427'><input type='hidden' name='door_number427' value='427'></td></tr>

<tr align='center'><td align='right'> <b>Door 428</b></td><td align='center'>Misloaded zips: <input type='text' name='zips428'><input type='hidden' name='door_number428' value='428'></td></tr>
After that form is submitted, I have a IF statement that to see if the $_POST value "post" has been sent. If so, it goes through this loop to generate the $_POST variables. This loop would be just like if I manually typed in

Code: Select all

$zips418 = $_POST['zips418'];
$zips419 = $_POST['zips419'];
$zips420 = $_POST['zips420'];
Since I can't manually type it in to the code because the database may change, that's why I created this loop to make the variables for me:

Code: Select all

if (isset($_POST['post'])) {
$door_num="SELECT door_num FROM load_doors";
$door_num=mysql_query($door_num);
$date = $_POST['date'];
$count = 0;
while ($doornum = mysql_fetch_row($door_num)) {
	$zips[0] = $_POST['zips[0]'];
	//$zips['$doornum[0]'];
	echo "Door num: $doornum[0] - Zips: $zips[0] - Date: $date<br>";
}
$count++;
}
Since I'm simply echo-ing everything out for testing purposes, everything is working except for the $zips[0] array. The door number generate on the page after submission by $doornum[0] and the $date works too, but I can't get it to make

Code: Select all

$zips[0] = $_POST['zips[0]'];
generate this

Code: Select all

$zips418 = $_POST['zips418'];
$zips419 = $_POST['zips419'];
$zips420 = $_POST['zips420'];
. If I can get the WHILE loop to generate the $zips[0], I know how to go from there.

Posted: Mon Aug 14, 2006 1:28 am
by RobertGonzalez

Posted: Mon Aug 14, 2006 1:33 am
by tristanlee85
I tried that and nothing changed. I'm pretty sure my problem enlies with

Code: Select all

$zips[0] = $_POST['zips[0]'];
I don't think that is generating

Code: Select all

$zips418 = $_POST['zips418'];

Posted: Mon Aug 14, 2006 1:42 am
by RobertGonzalez
You could those vars like this...

Code: Select all

<?php
foreach ($_POST as $key => $value)
{
    ${$key} = $value;
}
?>

Posted: Mon Aug 14, 2006 2:11 am
by tristanlee85
What would I use as $key and $value? I'm assuming $value is what's typed into the text box on the page and submitted, but what about $key?

Posted: Mon Aug 14, 2006 2:17 am
by RobertGonzalez
In that example, $key is the $_POST array index ('fieldname' in $_POST['fieldname']). $value is the value entered in the form (If 'field data' was entered, $value = 'field data'). That code is just a little snippet that takes your post array and assigns each member of the array to individual vars using the array key as the var name and the array value as the var value.

Posted: Mon Aug 14, 2006 2:22 am
by tristanlee85
But $_POST['fieldname'] isn't "hard-coded" into the script. I can't use:

Code: Select all

foreach ($_POST as $zips => $value)
{
    ${$zips} = $value;
}
because it need to be looking for zips418, zips419, etc... unless I'm not understanding it.

Posted: Mon Aug 14, 2006 2:27 am
by RobertGonzalez

Code: Select all

<form method="post" action="">
<p><input type="text" name="f1" /></p>
<p><input type="text" name="f2" /></p>
<p><input type="text" name="f3" /></p>
<p><input type="text" name="f4" /></p>
<p><input type="text" name="f5" /></p>
<p><input type="submit" name="sendButton" value="Test The Send" /></p>
</form>
When posted the $_POST array will contain this...

Code: Select all

$_POST = array(
    'f1' => 'WhatEverTheUserSuppliedForThisField',
    'f2' => 'WhatEverTheUserSuppliedForThisField',
    'f3' => 'WhatEverTheUserSuppliedForThisField',
    'f4' => 'WhatEverTheUserSuppliedForThisField',
    'f5' => 'WhatEverTheUserSuppliedForThisField',
    'sendButton' => 'Test The Send'
);
To get the value of each of the $_POST array members, you could reference them like this...

Code: Select all

$f1 = $_POST['f1'];
$f2 = $_POST['f2'];
$f3 = $_POST['f3'];
$f4 = $_POST['f4'];
$f5 = $_POST['f5'];
or to achieve the same affect, do this...

Code: Select all

foreach ($_POST as $key => $value)
{
    ${$key} = $value;
}

Posted: Mon Aug 14, 2006 2:30 am
by tristanlee85
Oh I see. Hmm. I got it working. I gets the date, the value entered and then the field name and then goes to the next value. If I can get it to split this up, I'll have it made.

Posted: Mon Aug 14, 2006 2:31 am
by RobertGonzalez
Splitting a string... explode()

Posted: Mon Aug 14, 2006 2:43 am
by tristanlee85
Yeh, I've been using that, but I know how to split it when there is a common explode characher like ":", but with a string like this: 115528209812345418419420421422423424425426427428

Red being the date, blue being the zip, and green being the door numbers.