Link to home
Create AccountLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

foreach caselist as case

put a foreach within a switch

to loop through a list


<?php
switch $_POST['color'] {
foreach ($caselist as $case){
    case $case:
        break;
}
case another:
break;
  }                                          

Open in new window

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

In the code snippet, $caselist is undefined.  Please explain in some details what you're trying to achieve.  I have never seen anyone try to write a code construct like that.
Testing the script, after defining the undefined variables, results in a parse error.  After switch(), it appears that the only expected statements are case and default.
http://php.net/manual/en/control-structures.switch.php
Avatar of rgb192

ASKER

$caselist =array(condition1, conditionZ, conditionQ)

Open in new window

OK, now please explain what you want to do with that $caselist.  PHP will not let you do a foreach inside a switch.
Avatar of rgb192

ASKER

$caselist =array(condition1, conditionZ, conditionQ)


which would make

case condition1:
break;
case conditionZ:
break;
case condition Q:
break;

Open in new window


so php does not allow this?
Without getting too deep into computer science, it looks like what you're after may be a "facade" design.  A compact way of writing this sort of code is to package a function for each of the "caselist" values.  Then you can call an appropriate function.
http://php.net/manual/en/function.call-user-func.php

Example here:
http://www.laprbass.com/RAY_temp_rgb192f.php?action=sub&a=5&b=3

<?php // RAY_temp_rgb192f.php
error_reporting(E_ALL);


// A LIST OF DATA CONDITIONS AND FUNCTIONS TO BE CALLED
$caselist = array
( 'add'  => 'my_addition'
, 'mult' => 'my_multiply'
, 'sub'  => 'my_subtract'
)
;

// A SET OF FUNCTION DEFINITIONS
function my_addition($a, $b)
{
    echo "$a + $b = " . $a+$b;
}

function my_multiply($a, $b)
{
    echo "$a * $b = " . $a*$b;
}

function my_subtract($a, $b)
{
    echo "$a - $b = " . $a-$b;
}


// ANALYZE THE GET REQUEST USING THE KNOWN ELEMENTS OF THE CASELIST
foreach ($caselist as $key => $function)
{
    if ($key == $_GET['action']) call_user_func($function, $_GET['a'], $_GET['b']);
}

Open in new window

HTH, ~Ray
Avatar of rgb192

ASKER

i do not understand how this does case list
What part do you not understand?  The URL argument here is used to choose a function to run.  This is similar to using a URL argument to choose an element of a switch/case block.

If this is not making sense, please explain a little more about what you're trying to achieve, thanks.
Avatar of rgb192

ASKER

how could this be called

 if ($key == $_GET['action']) call_user_func($function, $_GET['a'], $_GET['b']);


in a case statement

switch ($var){
case 1:
do something;
break;
default:
do something;
break;
}
SMH.  I just don't understand your objective.  Why do you want a case statement, when the data you've described does not lend itself to using a case statement?  This is like using a hammer to drive nails -- it's the wrong tool and the results are likely to be suboptimal.
Avatar of rgb192

ASKER

<?php
switch $_POST['color'] {
foreach ($caselist as $case){
    case $case:
        break;
}
case another:
break;
case another2:
break;
...
case another100:
break;
  } 

Open in new window



there is a very large case statement and I want to add 20 more values without copy pasting each case
Let me try this one more time.  PHP will not let you put a foreach() iterator inside a switch() control structure.  You can't do it.   It's not part of the language.  It results in a parse error!  Get over it and move on to a different design.  A workable design exists, I am sure.

If you can give us a simple, non-technical, real-world explanation of what you're trying to do we can almost certainly suggest a design pattern or even a code structure that would make sense.
Avatar of rgb192

ASKER

>>PHP will not let you put a foreach() iterator inside a switch() control structure

so can I do foreach outside switch() and save it is $foreach

similar to
echo
vs
 $echo.=          
echo $echo
One last time:

give us a simple, non-technical, real-world explanation of what you're trying to do

Avatar of rgb192

ASKER

client is viewing a large mysql query in html

I want to iterate through all the column names (which mysql allows)

client can click on the column header which takes them to the same page with a different order by query

I am told to add new columns each individually to mysql, html, and order by case statement

client does not know what the final html should be
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of rgb192

ASKER

I can order by column as get and then do not even need switch case.  Thanks
Thanks for the points, it's a great question, ~Ray