在switch case中运行php语句

在switch case中运行php语句,php,sql,arrays,Php,Sql,Arrays,这可能吗??如果是,那么代码有什么问题导致它不工作 else建议另一种有效的方法,因为如果我使用(if-else)语句,它将变成大约24到26个if-else条件的混乱。。。。还有4个案例陈述 重写的代码 代码: <?php $c = mysql_connect("localhost", "abc", "xyz"); mysql_select_db("root"); $bodytype = $_GET["name"]; //from

这可能吗??如果是,那么代码有什么问题导致它不工作 else建议另一种有效的方法,因为如果我使用(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;
?>
a=1;
    b=2;

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

请尝试先阅读以下链接:


也许更具建设性的一点是,让我们看看你在这里真正想要实现什么,然后重新启动一下大脑

您似乎正在尝试决定是否执行两个查询中的一个,一旦完成,输出似乎非常相似

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";
}
附言 尽量不要使用select*后跟$和[6]。想象一下,如果有人更改了数据库并在其中添加了一个新列,并确定该列的逻辑位置在第3列中,该代码会发生什么情况。你所有的代码都会以一种非常严重的方式崩溃。 想想那个可怜的懒汉,他得到了修理它的工作!!6个月后可能是你。这家伙想用什么字段???
使用$ans['column\u name']最坏或更好,仍然可以将结果作为对象获取,然后通过使用$table\u name->column\u name对值进行寻址,您就有了自文档代码。

我真的很想用著名的Picard meme来回答,它可以缩写为“WTFITS”。数组($ans[2],$ans[2])是什么意思mean?你到底在那里干什么?我想干燥会有帮助@STTLCU也许我可以帮助您在switch块中运行SQL查询,但您正在尝试执行wtf吗?告诉我是否可以按我尝试的方式将多个值传递给switch语句?感谢+1的“大脑重启”@riggsfully谢谢你的建议。。我不知道