Page 1 of 1

dynamic switch cases

Posted: Wed Sep 08, 2004 3:59 pm
by calebsg
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

Posted: Wed Sep 08, 2004 4:19 pm
by feyd
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..

Posted: Wed Sep 08, 2004 4:27 pm
by calebsg
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

Posted: Wed Sep 08, 2004 4:32 pm
by feyd
why not just use an if?

Posted: Wed Sep 08, 2004 8:02 pm
by Christopher
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++) &#123;  
  switch ($_POST&#1111;'submitter']) &#123;
  case ("Change ".pg_fetch_result($ci_staff,$ci,0)):
  update_staff_settings();
  break;
  &#125;
&#125;
?>
Maybe better would be something like:

Code: Select all

<?php
$submitter = preg_replace('/^a-zA-Z0-9/', '', $_POST&#1111;'submitter']);
$ci_staff = query_bordervet("SELECT staff_code FROM staff where staff_code="$submitter"");
$row = pg_fetch_result($ci_staff,$ci,0)):
if ($row&#1111;'staff_code']) 
  update_staff_settings($row&#1111;'staff_code']);
&#125;
?>

Posted: Thu Sep 09, 2004 3:03 am
by CoderGoblin
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.