【问题标题】:Display query result in a table在表格中显示查询结果
【发布时间】:2021-09-01 12:46:54
【问题描述】:

我有一个包含 50 多个返回结果的 MySQL 查询。现在我需要在一个 3 行 3 列的表中显示结果。

类似这样的:

<table>
    <tr>
        <td>Content</td>                    
        <td>Content</td>                    
        <td>Content</td>                                        
    </tr>   
    <tr>
        <td>Content</td>                    
        <td>Content</td>                    
        <td>Content</td>                                        
    </tr>   
    <tr>
        <td>Content</td>                    
        <td>Content</td>                    
        <td>Content</td>                                        
    </tr>
</table>

我用 PHP 尝试过这样的:

$q = "SELECT name, address, content FROM mytable"; 
$r = mysqli_query($dbc, $q); 

while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
    $name   = $row['name'];
    $address = $row['address'];
    $content = $row['content'];

    //Create new output
    $output  = "<p>$name</p>";
    $output .= "<p>$address</p>";
    $output .= "<p>$content</p>";

    //Add output to array
    $mycontent[] = $output;     
}

然后我在我的表中打印内容数组,如下所示:

<tr>
    <td><?php echo $mycontent[0]; ?></td>                   
    <td><?php echo $mycontent[1]; ?></td>                   
    <td><?php echo $mycontent[2]; ?></td>                   
</tr>   
<tr>
    <td><?php echo $mycontent[3]; ?></td>                   
    <td><?php echo $mycontent[4]; ?></td>                   
    <td><?php echo $mycontent[5]; ?></td>                                       
</tr>   
<tr>
    <td><?php echo $mycontent[6]; ?></td>                   
    <td><?php echo $mycontent[7]; ?></td>                   
    <td><?php echo $mycontent[8]; ?></td>                                           
</tr>

使用此代码,我只能显示 9 个内容。我的问题是我想显示更多内容。我将使用pagination 来显示内容;像 0-9、10-18、19-27 等。

注意:我可以做分页部分。

我希望有人能给我正确的方向。

谢谢。

【问题讨论】:

  • 如果确实回答了您的问题,请考虑接受答案(点击左侧的勾号)

标签: php mysql


【解决方案1】:

尝试类似:

 echo "<table>";
 while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
 $name   = $row['name'];
 $address = $row['address'];
 $content = $row['content'];
 echo "<tr><td>".$name."</td><td>".$address."</td><td>".$content."</td></tr>";
 }
 echo "</table>";

此示例将在表中打印所有查询结果。 如果你只想限制某些结果,那么限制sql查询。 例如:

 $q = "SELECT name, address, content FROM mytable limit 50"; 

要获取 TD 中的每个内容、名称、地址,以及 TR 中的 mycontent(content, name, address) 中的 3 个,请尝试以下操作:

$c= mysql_query("select COUNT(name) from mytable");
$n=mysql_fetch_array($c);
$maxname= $n[0];
$i=0;
$tr=0;
echo "<table>";
while ($tr < $maxname)
{ 
echo "<tr>";
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC) and $i<$tr) {
$name   = $row['name'];
$address = $row['address'];
$content = $row['content'];
echo "<td>".$name." | ".$address." | ".$content."</td>";
$i++;
}
echo "</tr>";
$tr= $tr+3;
}
echo "</table>";

【讨论】:

  • 姓名、地址和内容应放在一个 td 中而不是 3 td 中
  • 你的意思是你的表是这样的: 中的每个客户,每个 中的 3 个客户?
  • @TNK 我编辑了我的答案,在 1 个 TD 中包含姓名、地址和内容。
【解决方案2】:

使用for-loop 来迭代您的$mycontent-array:

编辑:我知道$mycontent 的构建方式与我最初假设的不同:

$h = "<table>"; // we are going to build the table in $h

$maxcount = count($mycontent); // get the number of values within the array 

for ($i = 0;$i < $maxcount;$i++) { // counting $i up from 0 to $maxcount -1 ...

   $h.= "<tr><td>{$mycontent[$i]}</td></tr>"; // build row

}

$h.= "</table>"; // close the table
echo $h; // echo it 

--->现场演示:http://3v4l.org/g1E1T

【讨论】:

  • 我对你的代码有点困惑。你能解释一下吗?
  • 当您可以使用while 迭代结果时,为什么还要使用for 循环?
  • 这行不通,因为你错过了他如何构建 $mycontent... 它不是一个对象数组...它是一个文字字符串。即 $mycontent[0] = "

    John Doe

    101 Lost st

    something

    "
  • @AarolamaBluenk:我想继续使用 $mycontent。当然,您可以在while 中编写表格。
  • 使用您编辑的代码,我的输出是 1 行和 1 td。这不是我的预期结果
【解决方案3】:

如果你打算在函数中使用它,你可以将它存储在一个变量中,或者你可以直接打印它。不管怎样……

$q = "SELECT name, address, content FROM mytable"; 
$r = mysqli_query($dbc, $q); 

$str = "<table>";

while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
    $str .= "<tr>";
    $str .= "<td>" . $row['name'] . "</td>";
    $str .= "<td>" . $row['address'] . "</td>";
    $str .= "<td>" . $row['content'] . "</td>";
    $str .= "</tr>";
}

$str .= "</table>"

echo $str;

你去!

【讨论】:

  • 只要调用这段代码。它将为您创建整个表格。
  • 姓名、地址和内容应放在一个 td 中而不是 3 td 中
  • 好吧,你可以自己调整它..发挥创意..在这一点上它非常简单。你应该不再需要帮助了。
【解决方案4】:

试试这样的。它将您的值放在一个易于使用的关联数组中。然后你就循环过去。

<?php
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
    $values[] = array(
        'name' => $row['name'],
        'address' => $row['address'],
        'content' => $row['content']
    );
}
?>
<table>
<?php
foreach($values as $v){
    echo '
    <tr>
        <td>'.$v['name'].'</td>
        <td>'.$v['address'].'</td>
        <td>'.$v['content'].'</td>
    </tr>
    ';
}
?>
</table>

【讨论】:

    【解决方案5】:

    您可以参考此代码

                 <tbody>
                  <!-- <tr>
                    <td>Technology</td>
                    <td>Professor Abiola</td>
                    <td>wetech@school.com</td>
                    <td> Plot 2, Oriola road,</td>
                    <td>
                      <button type="button" class="btn btn-primary"> Edit</button>
                      <button type="button" class="btn btn-success">Update</button>
                      <button type="button" class="btn btn-danger">Delete</button>
                    </td>
                  </tr> -->
                  <?php 
                    while($rows = mysqli_fetch_array($response)){
                     $faculty = $rows['faculty'];
                      $hod = $rows['hod'];
                      $email = $rows['email']; 
                      // $location = $rows['location'];
                    echo "<tr>
                            <td>$faculty</td>
                            <td>$hod</td>
                            <td>$email</td>
                            <td>".$rows["location"]."</td>
                            <td>
                             <button type='button' class='btn btn-primary'>Edit</button>
                             <button type='button' class='btn btn-success'>Update</button>
                             <button type='button' class='btn btn-danger'>Delete</button>
                            </td>
    
                           </tr>";
                    }
                  
                  ?>
                  </tbody>
                </table>
    

    【讨论】:

    • 请解释这段代码有什么帮助
    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签