在switch case中运行php语句

时间:2013-07-09 09:27:35

标签: php sql arrays

这可能吗?如果是,那么代码的错误导致它无法正常工作 否则建议另一个有效的方法因为如果我使用(if else)语句它会变得混乱大约24到26 if else条件....还有4个案例陈述

重写代码

代码:

    <?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;
?>

是否可以在switch语句中包含多个控制变量?

示例:

a=1;
    b=2;

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

2 个答案:

答案 0 :(得分:1)

可能在一个更具建设性的说明中,让我们看看你实际上想要在这里实现什么,并做一些大脑重启。

您似乎正在尝试决定是否执行2个查询之一,一旦完成,输出看起来非常相似。

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

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

这不能简单地实现如下:

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

尽量不要使用select *后跟$和[6]。想象一下,如果有人更改了数据库并添加了一个新列并确定了它的逻辑位置在第3列中,那么该代码会发生什么。所有代码都会以非常严肃的方式发生。 想想那可以解决问题的可怜的邋!!!可能是你6个月后。这个家伙要用哪些字段??? 最坏或更好地使用$ ans ['column_name']仍然可以将结果作为对象获取,然后通过使用$ table_name-&gt; Column_Name寻址值来自我记录代码。

答案 1 :(得分:0)