使用php获取行/列中的数据


fetch data in rows/column using php

我想在表中创建4列。在代码中,所有图像都是单行和单列的。但我想要一行包含4列和4个图像(从数据库中提取的图像),然后创建另一行并自动添加接下来的4个图像&等等。我不知道我是怎么做的。有人能建议我怎么做吗。

  <form name="form">
    <select id="sorting" style="width:140px" onChange="optionCheck()">
      <option id="s">---Sort By----</option>
      <option value="bydate">Sort By Date</option>
      <option value="bytopic">Sort By Topic</option>
    </select>
  </form>
  <br />
</div>
<?php include 'connection.php'; ?>
<div id="showByDefault">
  <table style="width:60%">
  <tr>
    <?php   include 'connection.php';  ?>
<div id="showByDefault">
<!--<table style="width:60%"><tr>-->

 <?php
      $sql1=mysqli_query($con,"select * from `insert-n-retrive-pdf` ORDER BY date DESC") or die(mysqli_error($con));
      $i=0;
      echo "<table><tr>";
  while($row=mysqli_fetch_array($sql1))
  { 
   if($i != 0 && $i%4 == 0) {
        echo '<tr></tr>';
    }
    ?> <td style="padding:20px;">

     <a href="<?php echo $row["path"]; ?>" target="_blank"><img src="<?php echo $row["thumbnails"]; ?>" /></a></td><?php
        echo '</tr>';
    $i++;
  }   
?></tr></table>
</div>
<div id="hideall">
    <div id="topic1">
      <?php include 'pdf-sort-by-topic.php'; ?>
    </div>
    <div id="topic">
      <?php include 'pdf-sort-by-date.php'; ?>
    </div>
</div>

试试这个100%有效:漂亮又简单。

    <?php
            $sql1=mysqli_query($con,"select * from `insert-n-retrive-pdf` ORDER BY date DESC") or die(mysqli_error($con));
            $i = 0;
            echo "<tr>";
            while($row=mysqli_fetch_array($sql1)) { 
                if($i != 0 && $i%4 == 0) {
                    echo "</tr><tr>";
                }
    ?>          
                <td style="padding:20px;"><a href="<?php echo $row["path"]; ?>" target="_blank"><img src="<?php echo $row["thumbnails"]; ?>" /></a></td>
    <?php       
                $i++;
            }
    ?>

希望这能有所帮助!

你可以试试这个代码

$query = mysql_query("SELECT * FROM insert-n-retrive-pdf ORDER BY date DESC");
echo '<table width="960">';
$i = 0; //first, i set a counter 
while($fetch = mysql_fetch_assoc($query)){
//counter is zero then we are start new row  
    if ($i==0){
        echo '<tr>';
    }
    //here we creating normal cells <td></td>
    $image_name = $fetch['thumbnails'];
    $image_location = $fetch['path'];
    echo '<td>'.'<img src="'.$image_location.'" alt="'.$image_name.'"/>'.'</td>';   
    //there is a magic - if our counter is greater then 4 we set counter to zero and close tr tag  
    if ($i>4){
        $i=0;
        echo '</tr>';
    };  
    $i++; //$i = $i + 1 - counter + 1
}
echo '</table>'; 

您可以将所有图像提取到一维数组中,然后使用函数array_chunk()。它会将阵列拆分为您需要的较小部分。这是手册页。

事实上,你可以得到这样的东西:

<?php
$images = array();
while($row=mysqli_fetch_array($sql1))
{
    $images[] = $row;
}
$images = array_chunk($images, 4);
?>
<?php foreach($images as $imagesChunk): ?>
    <tr>
        <?php foreach ($imagesChunk as $image): ?>
            <td style="padding:20px;">
                <a href="<?=$image["path"];?>" target="_blank">
                    <img src="<?=$image["thumbnails"];?>" />
                </a>
            </td>
        <?php endforeach; ?>
    </tr>
<?php endforeach; ?>
<table style="width:60%">
<tr>
<?php
$counter = 0;
$sql1 = mysqli_query($con, "select * from `insert-n-retrive-pdf` ORDER BY date DESC"
  ) or die(mysqli_error($con));
  while($row=mysqli_fetch_array($sql1))
  {
?>
  <td style="padding:20px;">
    <a href="<?php echo $row["path"]; ?>" target="_blank">
      <img src="<?php echo $row["thumbnails"]; ?>" />
    </a>
  </td>
<?php
    if($counter == 4)
    {
       echo "</tr>";
       echo "<tr>";
       $counter = 0;
   }
   $counter++;
}
?>
 </tr>
 </table>