简体   繁体   中英

How to display data in PHP from a database where every 4 data that appears then the other data is placed below

Currently I'm making a PHP application which displays data from a database by script as below:

<!DOCTYPE html>
<html>
  <head>
    <style>
      table{
      border-collapse:separate;
      border-spacing: 10px 15px;
      }
      th,td{
      width: 150px;
      text-align: center;
      border: 1px solid black;
      padding: 5px;
      }
    </style>
  </head>


<body>

<table border="1">

<?php
$conn = mysqli_connect('localhost','root','qwerty','example');
$data = mysqli_query($conn,"select letter from office");
while($d = mysqli_fetch_array($data)){
?>

<td><?php echo $d['letter']; ?></td>

<?php
}
?>
</table>
</body>

The result of which is as follows:

ABCDEFGHIJKL

原始结果

I want the result is like the following:

ABCD

EFGH

IJKL 预期结果

where there are only there are 4 data in each row.

If there is more than 4 data, the data placed in the below.

how do I do this?

<!DOCTYPE html>
<html>
  <head>
    <style>
      table{
      border-collapse:separate;
      border-spacing: 10px 15px;
      }
      th,td{
      width: 150px;
      text-align: center;
      border: 1px solid black;
      padding: 5px;
      }
    </style>
  </head>


<body>

<table border="1">

<?php
$counter=1;
$conn = mysqli_connect('localhost','root','qwerty','example');
$data = mysqli_query($conn,"select letter from office");
while($d = mysqli_fetch_array($data)){
?>
if($counter <= 4){
  <td><?php echo $d['letter']; ?></td>
}else{
  echo"\n";
  $counter=0;
 }
<?php
}
?>
</table>
</body>

I am not sure about the code running but t think this will help you. and you can use
tag also an echo section.

You can run a if condition to achieve that like below. What you are trying to do is to break the line after each four records. So there are lot of ways to accomplish this. But I think using modules operator you can achieve this easily. For more about these operators check w3c here

<?php
$i = 1;
$conn = mysqli_connect('localhost','root','qwerty','example');
$data = mysqli_query($conn,"select letter from office");
while($d = mysqli_fetch_array($data)){
?>

<td>
<?php

if ($i%4 ==0) { 
echo '<br>'; 
}else{
 echo $d['letter'];} ?></td>
}
$i++;
<?php
}
?>
<!DOCTYPE html>
    <html>
      <head>
        <style>
          table{
          border-collapse:separate;
          border-spacing: 10px 15px;
          }
          th,td{
          width: 150px;
          text-align: center;
          border: 1px solid black;
          padding: 5px;
          }
        </style>
      </head>


    <body>

    <table border="1">
    <tr>
    <?php
    $conn = mysqli_connect('localhost','root','qwerty','example');
    $data = mysqli_query($conn,"select letter from office");
    $i=1;
    while($d = mysqli_fetch_array($data)){
    ?>

    <td><?php echo $d['letter']; ?></td>
    <?php
    if($i==4){
    echo '</tr><tr>';
    $i=0;
    }
    $i++;
    }
    ?>
    </tr>
    </table>
    </body>

So in your while loop you're telling it just to print td html tags. Which are the table data tags.

To add rows you need to use the tr tags also. tr, is table row. It is like an excel sheet. tr is the row and td is the individual cells.

So going back to your code.

$data = mysqli_fetch_array($data)

foreach ($data as $id->$d) {
    if($id == 0) {
        echo '<tr><td>' . $d['letter'] . '</td>'; 
    } 
    else if($id%4 == 0) {
        echo '</tr><tr><td>' . $d['letter'] . '</td>'; 
    }
    else {
        echo '<td>' . $d['letter'] . '</td>'; 
    }
}
echo '</tr>'; 
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM