views:

143

answers:

3

In PHP, While using a switch case loop, can i use the for loop for iterating the cases? for example

switch .....

foreach($xyz as $abc)

{
 CASE:$abc
}

default;

UPDATE

I am fetching the value from DB, this value is name of table, by using "case" I want to execute a particular query according to the table name..

Is this possible?

+1  A: 

You probably just want to put the switch statement into the foreach?

foreach ($tables as $table) {
    switch ($table) {
        case 'table_one' :
            // do something here
            break;
        case 'table_two' :
            // do something here
            break;
        case 'table_three' :
            // do something here
            break;
        default :
            // do some error handling here
            break;
    }
}

Alternatively, a switch isn't that easy to read, consider going away from a switch and using an array-powered if, especially if you could dynamically create what you want to do each case:

$tables = array('table_one', 'table_two', 'table_three');
if (in_array($table, $tables)) {
    // do something here
} else {
    // do some error handling here
}

That's a lot more readable, even if your array has a lot of elements.

pinkgothic
He is asking if he can dynamically generate the CASE statements.
Finbarr
U got me right, thats what i want to do,this could be done but I have bunch of around 32 tables, they are all Joomla tables u see they have lots of tables, so the solution u have suggested could be very lenghty u see... any thing else u can suggest for this???
OM The Eternity
@Finbarr: Ooh, now I see it. Nope. But *usually* (probably not always), you can fix the underlying problem by changing the loops around. Thanks, though.
pinkgothic
@Parth: Give me a second.
pinkgothic
@Parth: Edited my answer, hopefully that helps. You could embed that in your `foreach` with `$table` as the variable you're pulling if that's still a necessary construct.
pinkgothic
@pinkgothic Hmmm True Very... I will do this.. Thanks Gothic... :) God Bless You
OM The Eternity
+1  A: 

I do not think that you can dynamically generate CASE declarations in PHP switch statements in this way.

What you are talking about doing is getting all of your tables in an array, then looping through them inside a switch statement to automatically declare your CASE statements. You cannot do this.

It generates:

Parse error: syntax error, unexpected T_FOREACH, expecting T_CASE or T_DEFAULT

Finbarr
pls elaborate what do mean by dynamically generation here in my question..may u are getting me wrong here..
OM The Eternity
See my edit, you cannot do what you wish.
Finbarr
+1 Thanks Mate I got your point.. actually i am new to programming terminology....
OM The Eternity
+1  A: 

EDIT: as per the OP's comments , i am writing this answer

   $query=null;
    switch($tableName):
    {
    case "table1":
    $query="...";
    break;
    case "table2":
    $query="...";
    break;
    case "table3":
    $query="...";
    break;
    }
    ....

here goes  logic to  execute that query

If you have too many tables then put those querys in array check like this ,

$arr = array("table1"=>"query1","table2"=>"query2","table3"=>"query3",....);
$query  = $arr[$tableName];

here goes the logic to execute query

Srinivas Reddy Thatiparthy
@reddy Saahab This could be very lengthy in case of Joomla 32 table hote hai usme.. :) lamba ho jayegaa.. Koi aur idea do plsss
OM The Eternity
@Parth , there is no other way i can think of...
Srinivas Reddy Thatiparthy