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
calebsg
Forum Commoner
Posts: 28 Joined: Tue Jun 18, 2002 10:41 am
Post
by calebsg » Wed Sep 08, 2004 3:59 pm
How can I make a set of dynamic cases in a switch statement that react to a dynamic set of submit buttons? Eg:
Code: Select all
<?php
$ci_staff = query_bordervet("SELECT staff_code FROM STAFF");
for ($ci = 0; $ci < pg_num_rows($ci_staff); $ci++) {
case ("Change ".pg_fetch_result($ci_staff,$ci,0)):
update_staff_settings();
break;
}
?>
But all I get is:
Parse error: parse error, unexpected T_CASE in /home/atc/rundir/reports/configmenu.php on line 447
Which makes me think it doesn't permit cases inside for statements?
TIA
Caleb
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Wed Sep 08, 2004 4:19 pm
why should php accept a case outside a switch.. they are a component of switch statements only. all a case statement is is an if..
calebsg
Forum Commoner
Posts: 28 Joined: Tue Jun 18, 2002 10:41 am
Post
by calebsg » Wed Sep 08, 2004 4:27 pm
Good point. I forgot to mention that the for loop is in a switch.
So my code is actually:
Code: Select all
<?php
switch ($_POST['submitter']) {
$ci_staff = query_bordervet("SELECT staff_code FROM STAFF");
for ($ci = 0; $ci < pg_num_rows($ci_staff); $ci++) {
case ("Change ".pg_fetch_result($ci_staff,$ci,0)):
update_staff_settings();
break;
}
}
?>
"submitter" being the name common to each dynamic button.
Or am I still missing something?
Caleb
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Wed Sep 08, 2004 4:32 pm
why not just use an if?
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Wed Sep 08, 2004 8:02 pm
An iif() would make more sense in the example you gave. Not sure why you would want to do it this way, but maybe:
Code: Select all
<?php
$ci_staff = query_bordervet("SELECT staff_code FROM STAFF");
for ($ci = 0; $ci < pg_num_rows($ci_staff); $ci++) {
switch ($_POSTї'submitter']) {
case ("Change ".pg_fetch_result($ci_staff,$ci,0)):
update_staff_settings();
break;
}
}
?>
Maybe better would be something like:
Code: Select all
<?php
$submitter = preg_replace('/^a-zA-Z0-9/', '', $_POSTї'submitter']);
$ci_staff = query_bordervet("SELECT staff_code FROM staff where staff_code="$submitter"");
$row = pg_fetch_result($ci_staff,$ci,0)):
if ($rowї'staff_code'])
update_staff_settings($rowї'staff_code']);
}
?>
CoderGoblin
DevNet Resident
Posts: 1425 Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany
Post
by CoderGoblin » Thu Sep 09, 2004 3:03 am
OK off topic but open up a new avenue of thinking if you did not already know about it...
Rather than using a pg_fetch_result I find the command pg_fetch_assoc useful (I started php using pg_fetch_result but found it very constrictive):
Code: Select all
$result=@pg_query("SQL");
if (@pg_num_rows($result) >0 ) {
while ($row=pg_fetch_assoc($result) {
// process code here
// $row is an indexed array so $row['colname1']=value1, $row['colname2']=value2 etc...
}
} else {
echo("Oops we have an error!<br/>");
}
Using an indexed array means if you want to convert the column names to variables you can use the php command extract. I find it also helps the readability of the code in the long run.
As previously stated just an idea if you hadn't come across it before.