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

时间:2015-02-18 08:30:01

标签: php html mysql

我想在表格中制作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>

4 个答案:

答案 0 :(得分:0)

  

尝试这个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++;
            }
    ?>

希望这有帮助!

答案 1 :(得分:0)

  

您可以尝试此代码

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

答案 2 :(得分:0)

您可以将所有图像提取到一维数组中,然后使用函数array_chunk()。它会将数组拆分成您需要的更小的部分。 Here's手册页。

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

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

答案 3 :(得分:0)

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