MySQL查询中的IF语句

时间:2013-06-07 10:53:47

标签: php mysql if-statement

我想写这样的东西:

<?php
$comment = mysql_query("SELECT * FROM `communication` WHERE `ID`='$index' order by `Date_add` desc");
echo "<div class=\"row\">";
while ($com = mysql_fetch_assoc($comment)) {
    $side = mysql_query("SELECT Type FROM `client` WHERE `ID`='$comtype'");

      if ($side[0]==2) {
       echo "<div class=\"left\">";     // and i want to execute this line only when the next value of $side is equal to 1 or 9                          
       echo "<div class=\"inside1\">"
        ...
       echo "</div>";
       echo "</div>";                   // same as above, close div only, when the next value of $side is equal to 1 or 9
      } else if ($side[0]==1 || $side[0]==9) {
       echo  "<div class=\"right\">";           // Same here i want to execute this line only when the next value of $side is equal to 2    
       echo "<div class=\"inside2\">";
        ...
       echo "</div>";
       echo "</div>";                 // same as above, close div only, when the next value of $side is equal to 2
  }
}
echo "</div>";

我需要执行整个代码,但我在div中有div,并且当$ side [0]的值不同时我想要并执行。例如:

Loop step 1:
$side[0]=2
so i want to execute: <div class=left> and everything in this div.
Loop step 2:
$side[0]=2 again
so i want to execute all in <div class=left> but i dont want to create another <div class=left>
Lopp step 3:
$side[0]=1
so previously $side[0] was equal 2, so now i want to create <div class=left> and everything in this div
Lopp step 4:
$side[0]=1 again
so i want to execute all in <div class=right> but i dont want to create another <div class=right>
    etc...

任何人都知道如何实现这样的效果?感谢您的建议帮助。

1 个答案:

答案 0 :(得分:0)

了解你的问题,你在while循环中有两个主要的din,在你想要根据特定列值显示结果的div中。我建议你根据查询的值分离数组的方式。然后迭代单个数组并相应地在let / right div上显示它。