【发布时间】: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>"." ₹ ".$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>"." ₹ ".$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 语句?谢谢