SWITCH-CASE block fails to execute

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
User avatar
phpPete
Forum Commoner
Posts: 97
Joined: Sun Aug 18, 2002 4:40 pm
Location: New Jersey

SWITCH-CASE block fails to execute

Post by phpPete »

First...sorry to post all the code here, but I think it will help, normally I wouldn't do it..

And any suggestions as to doing this better are greatly appreciated...!!!

The user is presented with a form containing 3 select lists.
Each list has associated with it a submit button.
The user selects from one list, clicks and the switch-case block should determine the item selected based on which submit is posted.

The funky behavior is: when I print_r(), I can see which select is chosen, however, the switch-case block code doesn't execute.

Results of clicking first submit:

Code: Select all

Array
(
    їitemName] => Array
        (
            ї0] => id3d5293043:Cheesecake
            ї1] => id3d4bd781c:Jalapeno Poppers
            ї2] => id3cf3fc332:test soup
        )

    їsubmit] => Array
        (
            їshowItem] => Array
                (
                    ї0] => Show Item
                )

        )

)
Here's the entirety of the code, followed by the function definition for the function that generates the select list boxes

Code: Select all

switch ( $_POSTї'submit']ї'showItem'])
{
    case $_POSTї'submit']ї'showItem']ї0]:
        $code = explode(':', $_POSTї'itemName']ї0]);
        $recCode = $codeї0];
        echo $recCode;
        break;
    case $_POSTї'submit']ї'showItem']ї1]:
        $code = explode(":", $_POSTї'itemName']ї1]);
        $recCode = $codeї0];
        echo $recCode;
        break;
    case $_POSTї'submit']ї'showItem']ї2]:
        $code = explode(":", $_POSTї'itemName']ї2]);
        $recCode = $codeї0];
        echo $recCode;
        break;
 }
include("../../include/functions/functions.inc");
include("../../include/phpClasses/MyConnect.php");
new MyConnect();
$box = array();
$query = "SELECT item_name, rec_code FROM menu_item WHERE item_name BETWEEN 'A' AND 'G' ";
$arr = getQueryResults( $query );
$boxї] = createDropDownBox2("itemNameї]", $arr);
$query = "SELECT item_name, rec_code FROM menu_item WHERE item_name BETWEEN 'H' AND 'P' ";
$arr = getQueryResults( $query );
$boxї] = createDropDownBox2("itemNameї]", $arr);
$query = "SELECT item_name, rec_code FROM menu_item WHERE item_name BETWEEN 'Q' AND 'Z' ";
$arr = getQueryResults( $query );
$boxї] = createDropDownBox2("itemNameї]", $arr);

?>
<form action="<?php echo $_SERVER&#1111;'PHP_SELF'] ?>" method="post" name="frmMenuItemSelect" id="frmMenuItemSelect">
<table align="left" bgcolor="#FCECB6" cellpadding="2" cellspacing="2">
<th align="center" colspan="3">Select Menu Item</th>
<tr>
	<td><font color="blue" size="+1">A - G</font></td>
	<td><?php echo $box&#1111;0] ?></td>
    <td><input type="submit" name="submit&#1111;showItem]&#1111;0]" value="Show Item"></td>
</tr>
<tr>
	<td><font color="blue" size="+1">H - P</font></td>
	<td><?php echo $box&#1111;1] ?></td>
    <td><input type="submit" name="submit&#1111;showItem]&#1111;1]" value="Show Item"></td>
</tr>
<tr>
	<td><font color="blue" size="+1">Q - Z</font></td>
	<td><?php echo $box&#1111;2] ?></td>
    <td><input type="submit" name="submit&#1111;showItem]&#1111;2]" value="Show Item"></td>
</tr>
<tr>
	<td align="center"></td>
    <td><font color="red" size="+1">Make only 1 Choice!!</font></td>
	<td></td>
</tr>
</table>
</form>
Function definition for createDropDownBox2()

Code: Select all

function createDropDownBox2($name,$ar)
&#123;
 $len = sizeof($ar);
 $tags = "<SELECT name='" . $name . "'>\n";
 for($i=0; $i<$len; $i++) 
 &#123;
  $tags .= "<OPTION value='" .$ar&#1111;$i]&#1111;1]  .":" . $ar&#1111;$i]&#1111;0] . "'>" . $ar&#1111;$i]&#1111;0] . "</OPTION>\n" ;
 &#125;
 $tags .= "</SELECT>";
 return $tags;
&#125;
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

ahh interesting :)

The problem is that you are slightly misusing the switch/case syntax.

switch/case is syntactic sugar for if elseif
ie

Code: Select all

switch ($variable)
&#123;
 case "val1": stuff1; break;
 case "val2":stuff2; break;
 default: stuff3; break;
&#125;
is the same
as

Code: Select all

if ("val1"==$variable)
&#123; stuff1;&#125;
elseif ("val2"==$variable)
&#123; stuff2;&#125;
else
&#123; stuff3;&#125;
In your case your switched variable "_POST['submit']['showItem']" never equals the values you are testing it against. Try expanding the switch/case to the if else if to understand why.

If this doesn't help, let me know.

PS. OT: this also explains why you can't use include() to provide case statements within a switch (stumped me for ages)
User avatar
phpPete
Forum Commoner
Posts: 97
Joined: Sun Aug 18, 2002 4:40 pm
Location: New Jersey

Post by phpPete »

Making this change I get the behavior I'm looking for:

Code: Select all

switch (( $_POST&#1111;'submit']&#1111;'showItem']&#1111;0] ) || ( $_POST&#1111;'submit']&#1111;'showItem']&#1111;1])||( $_POST&#1111;'submit']&#1111;'showItem']&#1111;2]))
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Here's another option which doesn't need a switch.

Code: Select all

$indices = array_keys($_POST&#1111;"submit']&#1111;'showItem']);
$index = $indices&#1111;0];
$code = explode(":",$_POST&#1111;'itemName']&#1111;$index]);
$recCode = $code&#1111;0];
echo $recCode;
User avatar
phpPete
Forum Commoner
Posts: 97
Joined: Sun Aug 18, 2002 4:40 pm
Location: New Jersey

Post by phpPete »

Now that I LIKE!!!

Thanks!
Post Reply