douxiong0668
douxiong0668
2018-06-22 06:13
浏览 150
已采纳

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

i had 5 images path and name in mysql database.

now i want to display that in a slider in for loop

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 loop can display a list from database?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • doumen5895
    doumen5895 2018-06-22 07:36
    已采纳

    can you tri this

    $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>
    
    点赞 评论
  • dorbmd1177
    dorbmd1177 2018-06-22 06:48

    This is not a good habit to loop multiple times unnecessary. We should avoid unnecessary loops in our programs.

        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>';
    }
    
    点赞 评论
  • doukengsha9472
    doukengsha9472 2018-06-22 06:50
    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>
    

    点赞 评论
  • duanna1407
    duanna1407 2018-06-22 06:56

    Move the image part to inside the while loop. So what will happen is, all records retrieved by the while loop will be the list items for the sider/slider images

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

    Assuming your html for the slider works fine even when not taking images from database. It should be working now!

    点赞 评论
  • duanhun3273
    duanhun3273 2018-06-22 07:14

    Much faster approach:-

    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>
    

    点赞 评论

相关推荐