【问题标题】:run php statement in switch case [closed]在switch case中运行php语句[关闭]
【发布时间】:2013-07-09 09:27:35
【问题描述】:

这可能吗?如果是,那么代码有什么问题会导致它无法正常工作 else 建议另一种有效的方法,因为如果我使用 (if else) 语句,它将变成大约 24 到 26 个 if else 条件的混乱......以及另外 4 个 case 语句

重写代码

代码:

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

【问题讨论】:

  • 我真的很想用著名的 Picard meme 来回答问题,它可以缩写为“WTFITS”。
  • array($ans[2],$ans[2]) 是什么意思?
  • 你到底在做什么?我认为干燥会有所帮助...@STTLCU 也许我可以帮助weknowmemes.com/wp-content/uploads/2012/02/wtf-is-this-shit.jpg
  • 你可以在 switch 块中运行 SQL 查询,但是你想做什么?
  • 告诉我是否可以像我尝试的那样将多个值传递给 switch 语句?谢谢

标签: php sql arrays


【解决方案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 * 后跟 $and[6]。想象一下,如果有人更改了数据库并在其中添加了一个新列,并确定它的逻辑位置在第 3 列中,那么这段代码会发生什么。你所有的代码都会以一种非常严重的方式崩溃。 想想那个得到修理工作的可怜的懒汉!!可能是 6 个月后的你。这家伙想用什么领域??? 在最坏的情况下使用 $ans['column_name'] 或最好仍然将结果作为对象获取,然后您可以通过使用 $table_name->Column_Name 寻址值来进行自我记录代码。

【讨论】:

  • +1 表示“大脑重启”
  • @RiggsFolly 感谢您的建议.. 我不知道
【解决方案2】:

【讨论】:

    最近更新 更多