drsxzut183207938 2013-07-09 09:27
浏览 25
已采纳

在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;               
            } 
  • 写回答

2条回答 默认 最新

  • 普通网友 2013-07-09 10:27
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么