在HTML

时间:2016-04-08 20:19:29

标签: php html mysql

具有以下while循环,它将输出在PHP中执行的SQL查询的所有结果:

while ($row = mysqli_fetch_array($result)) 
{
    $restaurant_name = $row['restaurant_name'];
    $cusine = $row['cusine'];
    $wait_time = $row['wait_time'];
    $output = "Resturant Name: $restaurant_name <br /> Cusine: $cusine <br /> Average Wait Time: $wait_time";
    echo "$output";
}

您可以假设$result将包含我一次在行中阅读的表格。上面的代码工作得很好但是当我尝试在HTML中显示信息时(实际上它看起来很好)我遇到了问题

如果我执行以下操作:

<html>              
<section id="main" class="container 75%">

    <header>
        <h2>Here Are Some Places You'd Like To Eat</h2>
    </header>

    <div class="box">
    <?php echo $output; ?>
    </div>
</section>
</html>

它只会显示最后的手段。我知道发生这种情况是因为$output一次只能容纳一个字符串,但我不知道在HTML上显示信息的任何其他方式。我虽然可能使用数组来存储所有字符串,但文档没有告诉我如何设置动态数组。

有没有人知道如何在HTML页面中显示搜索查询的所有结果?

4 个答案:

答案 0 :(得分:0)

您可以将php和html代码组合在一起,如下所示:

font = self.menuBar().font()
font.setPointSize(20)
self.menuBar().setFont(font)

这样您就不必使用数组来显示结果。

答案 1 :(得分:0)

<header>
    <h2>Here Are Some Places You'd Like To Eat</h2>
</header>

<div class="box">
<?php
  while ($row = mysqli_fetch_array($result)) {
    $row = mysqli_fetch_array($result); 
    echo 'Restaurant name: '.$row['restaurant_name'];
    echo 'Cusine: '.$row['cusine'];
    // ...and so on
  }
?>
</div>

答案 2 :(得分:0)

您可以让循环使用表格在div中生成HTML。我不确定你的box课程的CSS是什么样的,但这肯定会让你在打印出所有结果时看起来更好。

<header>
  <h2>Here Are Some Places You'd Like To Eat</h2>
</header>
    <div class="box">
      <table>
        <tr>
          <td>Restaurant</td>
          <td>Cusine</td>
          <td>Wait Time</td>
        </tr>
while ($row = mysqli_fetch_array($result)) 
      {
        $restaurant_name = $row['restaurant_name'];
        $cusine = $row['cusine'];
        $wait_time = $row['wait_time'];

        echo "<tr><td>". $restaurant_name . "</td><td>" . $cusine . "</td><td>" . $wait_time . "</td></tr>"; 
      }
     </table>
   </div>

答案 3 :(得分:0)

您在每个循环中覆盖$ output变量,然后尝试稍后回显$ output;它只包含上次迭代的数据。

相反,将$ output作为包含数据的数组,然后将其循环以显示:

$output=array();
while ($row = mysqli_fetch_array($result)) :
    $output[]=$row;
endwhile

然后你可以遍历$ output数组来为每个餐厅构建一个输出:

foreach($output as $value):
    $returnDetails='<div class="box">';
    $returnDetails.='Resturant Name: '.$output['restaurant_name'].'<br />';
    // etc. for all details
    $returnDetails.='</div>';
    echo $returnDetails; 
endforeach;

以这种方式增加了好处:如果您想访问循环上下文之外的单行,则可以访问$ output数组