在开关大小写中运行 PHP 语句


run php statement in switch case

这可能吗?? 如果是,则代码出了什么问题导致其无法正常工作否则建议另一种有效的方法,因为如果我使用(如果其他(语句,如果其他条件,它将变得大约 24 到 26 的混乱......还有 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;               
            } 

可能从一个更有建设性的角度来看,让我们看看你在这里实际想要实现的目标,并进行一些大脑重启。

您似乎正在尝试决定是否执行 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";
}

附言

尽量不要使用选择*后跟$and[6]。想象一下,如果有人更改了数据库并在其中放置了一个新列,并确定它的逻辑位置在第 3 列中,那么这段代码会发生什么。你所有的代码都会以一种非常严肃的方式爆炸。想想那个可怜的懒汉,得到修复它的工作!!6个月后可能是你。这家伙想用什么领域???在最坏的情况下使用 $ans['column_name'] 或更好的仍然是将结果作为对象获取,然后通过使用 $table_name->Column_Name 寻址值来自我记录代码。

首先尝试阅读此链接:

示例 #1 交换机结构

将数组传递到 switch 语句中...