如何在for循环php中显示图像?

时间:2018-06-22 06:13:53

标签: php html mysql

我在mysql数据库中有5张图像 path name

现在我要在for循环的滑块中显示

include ('conn.php');

$select_path="select * from image_table";

$var=mysqli_query($conn,$select_path);

while($row=mysqli_fetch_array($var))
{
 $image_name=$row["name"];
 $image_path=$row["path"];
 $url=$image_path.$image_name;
}



echo' <div id="ninja-slider">
    <div class="slider-inner">
        <ul>';
            for($i=0;$i < $url;$i++){
               echo'<li>';
               echo'  <a class="ns-img" href="'.$url[$i].'"></a>
                <div class="caption">image 1</div>
               </li>';}

        </ul>
        <div class="fs-icon" title="Expand/Close"></div>
    </div>
</div>

for循环可以显示数据库中的列表吗?

5 个答案:

答案 0 :(得分:0)

这不是循环多次不必要的好习惯。我们应该避免程序中不必要的循环。

    include ('conn.php');

    $select_path="select * from image_table";

    $var=mysqli_query($conn,$select_path);
if(mysql_num_rows($var) > 0){
 echo' <div id="ninja-slider"><div class="slider-inner"><ul>';
    while($row=mysqli_fetch_array($var))
    {
     $image_name=$row["name"];
     $image_path=$row["path"];
     $url=$image_path.$image_name;
     echo '<li>';
     echo '<a class="ns-img" href="'.$url.'"><img src="'.$url.'" /></a>';
     echo '<div class="caption">image 1</div></li>';
    }

echo '</ul><div class="fs-icon" title="Expand/Close"></div></div></div>';
}

答案 1 :(得分:0)

while($row=mysqli_fetch_array($var))
 {
  $image_name=$row["name"];
  $image_path=$row["path"];
  $url=$image_path.$image_name; // wrong . this will store only last image because all time it replace last one . add array for same like $url[$i] = image_path 
 }



echo' <div id="ninja-slider">
      <div class="slider-inner">
        <ul>';
         for($i=0;$i < $url;$i++){
             echo'<li>';
              echo'  <a class="ns-img" href="'.$url[$i].'"></a> // this work when loop properly set 
            <div class="caption">image 1</div>
           </li>';}

    </ul>
    <div class="fs-icon" title="Expand/Close"></div>
</div>

答案 2 :(得分:0)

将图像部分移动到while循环内。因此,将发生的是,while循环检索到的所有记录将成为sider / slider图像的列表项

<?php include ('conn.php');

$select_path="select * from image_table";
$var=mysqli_query($conn,$select_path); ?>

<div id="ninja-slider">
  <div class="slider-inner">
    <ul>

      <?php  while($row=mysqli_fetch_array($var)){
      $image_name=$row["name"];
      $image_path=$row["path"];
      $url=$image_path.$image_name; ?>
      <li>
        <a class="ns-img" href="<?php echo $url; ?>"></a>
        <div class="caption"><?php echo $image_name; ?></div>
      </li>
      <?php } ?>
    </ul>
    <div class="fs-icon" title="Expand/Close"></div>
  </div>
</div>

即使您没有从数据库中获取图像,假设滑块的html也可以正常工作。现在应该可以正常工作了!

答案 3 :(得分:0)

更快的方法:-

include ('conn.php');
$select_path="select * from image_table";
$var=mysqli_query($conn,$select_path);

<div id="ninja-slider">
    <div class="slider-inner">
        <ul>
            while($row=mysqli_fetch_array($var)){
            ?>
            <li><a class="ns-img" href="<?=$row["path"].$row["name"];?>"><img src="<?=$row["path"].$row["name"];?>" /></a><div class="caption"><?=$row["name"];?></div>
            </li>
            <?php } ?>
        </ul>
        <div class="fs-icon" title="Expand/Close"></div>
    </div>

答案 4 :(得分:0)

你能不能这样

$servername = 'localhost';
$username = 'root';
$password = '';
$dbname = 'image';
$conn = new mysqli($servername,$username,$password,$dbname);
if($conn->connect_error){
    die.("Connection failed : " .$conn->connect_error);
}
$select_path = 'select * from images';
$var = mysqli_query($conn,$select_path);
while ($row=mysqli_fetch_array($var))
{
    $image_name = $row['image_name'];
    $image_path = 'images';
    $url = "$image_path".'/'."$image_name";
    echo'<div id="ninja-slider">
      <div class="slider-inner">
      <ul>';
    echo'  <a class="ns-img" href="'.$url.'"></a>
           <div class="caption">'.$url.'</div>
    </li>';
}
?>
</ul>
        <div class="fs-icon" title="Expand/Close"></div>
    </div>
</div>