douhuan1380 2016-02-05 08:34
浏览 46
已采纳

只有当我有两个<li>标签时才会改变图像

Thanks for your timing in reading this post. Let me explain my requirement

Actually based on the IMAGENUM field value in the database, i need to display the number of images accordingly. My Updated code is as follows

while( $row = $result->fetch_assoc() ) {
            $NUM=intval($row["IMAGENUM"]);
            $imagesArr=array();
            //dynamically fill the imagesArr
            for($i=0; $i < $NUM ; $i++){
                $currKey = "IMG".($i+1);
                if(!empty($row[$currKey])){
                     array_push($imagesArr,$row[$currKey]);
                }
            } 
            $NAME= $row["NAME"];
            $QNTY=$row["QNTY"];
            $PRICE= $row["PRICE"];
            $DESC=$row["DESC"];
            $ADDDESC= $row["ADDDESC"];
            $IFLAG=$row["IFLAG"];
            $SFLAG= $row["SFLAG"];
            $CFLAG=$row["CFLAG"];
            $IMG1= $row["IMG1"];
            $IMG2=$row["IMG2"];
            $IMG3= $row["IMG3"];
            for($i = 0; $i < $NUM; $i++) {
            echo $imagesArr[$i];            
            echo '
                    <div class="single_grid">
                    <div class="grid images_3_of_2">
                        <ul id="etalage">
                            <li>
                                <a href="optionallink.html">
                                    <img class="etalage_thumb_image" src="', $imagesArr[$i], '" class="img-responsive" />
                                    <img class="etalage_source_image" src="', $imagesArr[$i], '" class="img-responsive" title="" />
                                </a>
                            </li>
                            <li>
                                <a href="optionallink.html">
                                    <img class="etalage_thumb_image" src="', $imagesArr[$i], '" class="img-responsive" />
                                    <img class="etalage_source_image" src="', $imagesArr[$i], '" class="img-responsive" title="" />
                                </a>
                            </li>
                        <ul>
                    </div>
                    </div>
            ';
            }
            }

When i echo $imagesArr[$i], i can see all the values but image is getting changed only when i have two list tags Please suggest. Thanks

  • 写回答

1条回答 默认 最新

  • doudong2149 2016-02-05 08:47
    关注

    You could push all images into an array and iterate this array afterwards.

    while ($row = mysql_fetch_assoc($result)) { 
                $NUM=intval($row["IMAGENUM"]);
                $imagesArr=array();
                //dynamically fill the imagesArr
                for($i=0; $i < $NUM ; $i++){
                    $currKey = "IMG".($i+1);
                    if(!empty($row[$currKey])){
                         array_push($imagesArr,$row[$currKey]);
                    }
                }          
    
                $NAME= $row["NAME"];
                $QNTY=$row["QNTY"];
                $PRICE= $row["PRICE"];
                $DESC=$row["DESC"];
                $ADDDESC= $row["ADDDESC"];
                $IFLAG=$row["IFLAG"];
                $SFLAG= $row["SFLAG"];
                $CFLAG=$row["CFLAG"];
    
                for($i = 0; $i < $NUM; $i++) {
                  echo '
                        <div class="single_grid">
                        <div class="grid images_3_of_2">
                            <ul id="etalage">
                                <li>
                                    <a href="optionallink.html">
                                        <img class="etalage_thumb_image" src="', $imagesArr[$i], '" class="img-responsive" />
                                        <img class="etalage_source_image" src="', $imagesArr[$i], '" class="img-responsive" title="" />
                                    </a>
                                </li>
                            <ul>
                        </div>
                        </div>
                ';
     }
    

    Note: Instead of using deprecated mysql_fetch_assoc(), please consider using PDO/mysqli, e.g.:

    $connection = new mysqli($host,$user,$pass,$db);
    
    $result = $connection->query("SELECT * FROM `Blogs`");
    
    
    while( $row = $result->fetch_assoc() ) {
       //do your stuff here for each row
    }
    

    Furthermore, your table structure is not ideal. It would be better to store the image links separately in one table with a foreign key to the product. Then you could easily fetch all images for the product and don't need to dynamically fill the images array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起