2

我有超过 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 个内容。我的问题是我想显示更多内容。我将使用分页来显示内容;像 0-9、10-18、19-27 等。

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

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

谢谢你。

4

5 回答 5

3

尝试类似:

 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>";
于 2013-03-16T14:21:43.450 回答
1

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

<?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>
于 2013-03-16T16:18:35.663 回答
0

使用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

于 2013-03-16T14:22:26.813 回答
0

如果您打算在函数中使用它,您可以将其存储在变量中,或者您可以打印它。无论哪种方式...

$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;

给你!

于 2013-03-16T14:40:43.037 回答
0

您可以参考此代码

             <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>
于 2021-11-17T13:39:56.333 回答