dynamic switch cases

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

Post Reply
calebsg
Forum Commoner
Posts: 28
Joined: Tue Jun 18, 2002 10:41 am

dynamic switch cases

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
calebsg
Forum Commoner
Posts: 28
Joined: Tue Jun 18, 2002 10:41 am

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

why not just use an if?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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;
?>
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
Post Reply