基于php中行号的回显消息

时间:2014-04-04 12:27:24

标签: php html mysql

我有一个显示数据库信息的表。我添加了一个列,我想根据显示的行号显示一条消息。

<form name="afisare1" method="POST" >
    <input type="submit" name="opt1"  value="OK"/>
    <?php   
     if (isset($_POST['opt1'])) {
        $loc=mysql_query("SELECT loc FROM program WHERE den='Option'");
        //$loc result is a number

        $sql=mysql_query("SELECT Col1, Col2 
                          FROM date WHERE Opt_1='Option' OR Opt_2='Option' 
                          ORDER BY Col2 DESC");
    ?>
    <table>
        <thead>
            <tr>
              <th>Col1</th> <th>Col2</th> <th>OK/NOT OK</th>
            </tr>
        </thead>
        <?php
        $num_rows = 0;
        while ($row = mysql_fetch_assoc($sql)) {
            $num_rows++;
            if ($num_rows <= $loc) {
                echo"<tr>
                        <td>".$row['Col1']."</td><td >".$row['Col2']."</td><td>OK</td>
                     </tr>";
                break;
            } 
            if ($num_rows > $locuri_buget) { 

                //here i have a problem because i don't know how to display something like this : 
                //echo"<tr><td>".$row['Col1']."</td><td >".$row['Col2']."</td><td>NOT OK</td></tr>";   
            }
        }
    }  ?>
    </table>    
</form>

例如,如果$ loc = 2的结果我希望回显前2行OK,而对于额外的行我想要回显不行

3 个答案:

答案 0 :(得分:0)

你可以尝试跟随内部循环

if($num_rows<=$loc){
 echo"<tr><td>".$row['Col1']."</td><td >".$row['Col2']."</td><td>OK</td></tr>";
}else{ 
 echo"<tr><td>".$row['Col1']."</td><td >".$row['Col2']."</td><td>NOT OK</td></tr>";
}

答案 1 :(得分:0)

  $num_rows = 0;
  while($row = mysql_fetch_assoc($sql)){
    $num_rows++;
     if($num_rows<=$loc){
           echo"<tr><td>".$row['Col1']."</td><td >".$row['Col2']."</td><td>OK</td></tr>";
      }else {  
           echo"<tr><td>".$row['Col1']."</td><td >".$row['Col2']."</td><td>OK</td></tr>";
      }
 }  

我认为你的问题是休息,

  

break结束当前for,foreach,while,do-while或switch结构的执行。   Break

答案 2 :(得分:0)

要从MySQL检索loc,您的原始代码就是这样:

$loc = mysql_query("SELECT loc FROM program WHERE den='Option'");

但你可能想要改变它:

$loc = 0; # default value in case of error in a query
$result = mysql_query("SELECT loc FROM program WHERE den='Option'");
while($row = mysql_fetch_assoc($result)){
    $loc = $row['loc'];
}

或者,如果您只想从数据库中检索一条记录,您也可以像这样编写一个LIMIT的SQL:

$loc = 0; # default value in case of error in a query
$result = mysql_query("SELECT loc FROM program WHERE den='Option' LIMIT 1");
if($row = mysql_fetch_assoc($result)){
    $loc = $row['loc'];
}

希望这有帮助。