在switch case中运行php语句[关闭]

is this possible?? if yes than whats wrong with the code cause its not working else suggest another efficient method cause if i use (if else) statements it will become mess of about 24 to 26 if else conditions.... and 4 more case statements there

rewritten code

Code:

    <?php
        $c = mysql_connect("localhost", "abc", "xyz");
        mysql_select_db("root");
        $bodytype = $_GET["name"]; //from another page through ajax
        $company  = $_GET["name2"]; //from another page through ajax    
        $Array    = array($bodytype,$company );
        $q="select * from product";
                $qc=mysql_query($q);
                $ans=mysql_fetch_array($qc);
                $ans[6];
                $ans[1];
    switch ($Array)
    {
        case array($ans[6],$ans[1]):
                        $q="select * from product where bodytype='$bodytype'&& Companyname='$company' GROUP BY modelname";
                        $qc=mysql_query($q);
                        $ans=mysql_fetch_array($qc);
                        $count=0;
                        while($ans=mysql_fetch_array($qc))
                        {
                                if ($count == 0 || $count == 1 || $count == 2)
                                {

                                $title=ucwords($ans[1]." ".$ans[2]);

                                print "<div class='img-wrap'>
                                        <img id='display_img' src='products/$ans[8]' width=300 height=200 title='$title'>
                                        <div class='img-overlay'>
                                        <input type='checkbox' id='compare_pro' /> Add to compare
                                        <h4>".$title."</h4>
                                        <p>".nl2br($ans[9])."</p>
                                        <p>"."<b>Versions:</b> ".$ans[3]."</p>
                                        <p>"."<b>Starting Price:</b>"." &#x20B9 ".$ans[4]."</p>
                                        </div>
                                        </div>";
                                }
                                $count++;
                                if($count==3)
                                {
                                    print "<br />";
                                    $count = 0;
                                }
                        }
      break;
    case array($ans[6],'not'):
                        $q="select * from product where bodytype='$bodytype' GROUP BY modelname";
                        $qc=mysql_query($q);
                        $count=0;
                        while($ans=mysql_fetch_array($qc))
                        {
                                if ($count == 0 || $count == 1 || $count == 2)
                                {

                                $title=ucwords($ans[1]." ".$ans[2]);

                                print "<div class='img-wrap'>
                                        <img id='display_img' src='products/$ans[8]' width=300 height=200 title='$title'>
                                        <div class='img-overlay'>
                                        <input type='checkbox' id='compare_pro' /> Add to compare
                                        <h4>".$title."</h4>
                                        <p>".nl2br($ans[9])."</p>
                                        <p>"."<b>Versions:</b> ".$ans[3]."</p>
                                        <p>"."<b>Starting Price:</b>"." &#x20B9 ".$ans[4]."</p>
                                        </div>
                                        </div>";
                                }
                                $count++;
                                if($count==3)
                                {
                                    print "<br />";
                                    $count = 0;
                                }
                        }   
      break;
?>

Is it possible to have multiple control variables in a switch statement?

example :

a=1;
    b=2;

        switch(a , b)
            {
                   case(1,2): print "true";
                   break;
                   case(2,1): print "false";
                   break;               
            } 

Possibly on a more constructive note, lets see what you are actually trying to achieve here and do a bit of a brain reboot.

You appear the be trying to decide whether to do one of 2 queries, once they are done the output appears to be very similiar.

Query one:
"select * from product where bodytype='$bodytype' && Companyname='$company' GROUP BY modelname";

Query two:
"select * from product where bodytype='$bodytype' GROUP BY modelname";

Could this not be achieved so much more simply as follows:

if ( ! empty( $_GET['name2'] ) ) {
    $query = "select * from product where bodytype='$bodytype' && Companyname='$company' GROUP BY modelname";
} else {
    $query = "select * from product where bodytype='$bodytype' GROUP BY modelname";
}

PS

Try not to use select * followed by $and[6]. Imagine what woudd happen to this code if someone changed the database and put a new column in and decided the logical place for it was in column 3. All your code would go bang in a very serious way. Think about the poor slob that gets the job of fixing it!! It may be you 6 months later. What fields was this guy tring to use??? Use $ans['column_name'] at worst or better still fetch the result as an object and then you have self documenting code by addressing values using $table_name->Column_Name.