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
siefkencp
Forum Commoner
Posts: 69 Joined: Thu Dec 16, 2004 8:50 am
Post
by siefkencp » Mon Nov 07, 2005 9:20 am
I have a problem that would lend it self to being able to do this.
1st:
A form that extends itself through a dynamically generated list.
Code: Select all
$table .= "
while(some condition = TRUE){
$form = "<td>$i<br>\n
<input name=\"r$i\" type=\"text\" id=\"r$i\" size=\"1\"><font size=\"-1\">Regular</font><br>\n";
}
print $form;
2nd: a process to get these variables out (in a loop) and use them.
Code: Select all
while (not finnished working) {
$r = $_POST['r$1'];
dosomething_with($r);
}
Any help would be greatly apreciated!
Chris
Grim...
DevNet Resident
Posts: 1445 Joined: Tue May 18, 2004 5:32 am
Location: London, UK
Post
by Grim... » Mon Nov 07, 2005 9:31 am
You are on the right track, but line 3 of the first chunck of code should be :
(note the full stop).
Next, define the variable name in a variable:
Code: Select all
for ($x = 0; $x <= $i; $x++)
{
$variable = "r".$x;
$r = $_POST[$$variable]
}
Not tested, but try that and see how you get on...
siefkencp
Forum Commoner
Posts: 69 Joined: Thu Dec 16, 2004 8:50 am
Post
by siefkencp » Mon Nov 07, 2005 9:48 am
I'm getting:
PHP Notice: Undefined variable: r1 in c:\Intranet\facstaffstuff\timereport\i_proc.php on line 7 PHP Notice: Undefined index: in c:\Intranet\facstaffstuff\timereport\i_proc.php on line 7
For grin's I have included the whole thing on both ends:
Code: Select all
$t = 1;
$today = date("m/d/y");
$table = "
<form name=\"form1\" method=\"post\" action=\"i_proc.php\">
\n<table border=\"1\" cellpadding=\"4\" align=\"center\">\n";
$start_date = getdate(mktime(0,0,0,$month,1,$year));
$end_date = getdate(mktime(0,0,0,$month+1,0,$year));
$i = $start_date['mday'];
$end_mday = $end_date['mday'];
$long_month = $start_date['month'];
print $long_month . "<br>\n";
$table .= "
<tr>\n
<td>Mon</td>\n
<td>Tue</td>\n
<td>Wed</td>\n
<td>Thur</td>\n
<td>Fri</td>\n
</tr>\n";
while($i != $end_mday){
$work_date = getdate(mktime(0,0,0,$month,$i,$year));
$w = $work_date['wday'];
if($w == '0'){
$table .= "<tr>\n";
}
if(($w != '0') and ($w != '6')){
if(($w != 1)and ($i == 1)){
while($w != $t){
$table .= "<td></td>";
$t++; }}
$table .= "
<td>$i<br>\n
<input name=\"r$i\" type=\"text\" id=\"r$i\" value=\"0\" size=\"1\"><font size=\"-1\">Regular</font><br>\n
<input name=\"s$i\" type=\"text\" id=\"s$i\" value=\"0\" size=\"1\"><font size=\"-1\">Sick</font><br>\n
<input name=\"v$i\" type=\"text\" id=\"v$i\" value=\"0\" size=\"1\"><font size=\"-1\">Vacation</font></td>\n";
}
if($w == '6'){
$table .= "</tr>\n";
}
$i++;
}
$table .= "</table><input name=\"Submit\" type=\"submit\"></form>\n";
print $table;
The processing file:
Code: Select all
$max = 32;
$int = 1;
while($int != $max){
$variable = "r".$int;
$r = $_POST[$$variable];
print $r;
$int++;
}
I was thinking maybe I need to add a self building array on the first form to drive the second... Thoughts?
Grim...
DevNet Resident
Posts: 1445 Joined: Tue May 18, 2004 5:32 am
Location: London, UK
Post
by Grim... » Mon Nov 07, 2005 10:03 am
Add a hidden form value after the while loop with the value of $i, then get it again on the next page.
siefkencp
Forum Commoner
Posts: 69 Joined: Thu Dec 16, 2004 8:50 am
Post
by siefkencp » Mon Nov 07, 2005 10:18 am
I now get this:
PHP Notice: Undefined index: 5 in c:\Intranet\facstaffstuff\timereport\i_proc.php on line 7
foobar
Forum Regular
Posts: 613 Joined: Wed Sep 28, 2005 10:08 am
Post
by foobar » Mon Nov 07, 2005 10:21 am
error_reporting(E_PARSE);
So that you don't get those unnecessary notices.
Last edited by
foobar on Mon Nov 07, 2005 10:24 am, edited 1 time in total.
siefkencp
Forum Commoner
Posts: 69 Joined: Thu Dec 16, 2004 8:50 am
Post
by siefkencp » Mon Nov 07, 2005 10:23 am
OK, nevermind the last one... I have register global's on so i swapped out $_POST[$$variable] for just plain $$variable and it works. Thanks for your help! You saved me a bunch of experimentation.
Chris
foobar
Forum Regular
Posts: 613 Joined: Wed Sep 28, 2005 10:08 am
Post
by foobar » Mon Nov 07, 2005 10:25 am
siefkencp wrote: OK, nevermind the last one... I have register global's on so i swapped out $_POST[$$variable] for just plain $$variable and it works. Thanks for your help! You saved me a bunch of experimentation.
Chris
You should use $_POST[] instead of just $<post array key>. It's more elegant.
siefkencp
Forum Commoner
Posts: 69 Joined: Thu Dec 16, 2004 8:50 am
Post
by siefkencp » Mon Nov 07, 2005 10:46 am
Function over form man... I get paid to maximize resources including my time.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Mon Nov 07, 2005 7:29 pm
do not ignore the notices as foobar suggested.. fix the problem. Add checking to see if the index exists using
isset()
Ambush Commander
DevNet Master
Posts: 3698 Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US
Post
by Ambush Commander » Mon Nov 07, 2005 8:03 pm
It's
, strike the double dollarsigns. And you should never rely on register globals to propogate your user data.