drt12345678 2018-07-20 12:48
浏览 39
已采纳

PHP将目录中的图像导入2列HTML行

I have found some PHP that I am trying to adapt onto my page but can not seem to get it to work. Here is the code:

<?php

$pagetitle = "Test Gallery";
$dirname = "img/folder_1/";
$images = glob($dirname."*.jpg");

?>


<?php $i=0; foreach($images as $image): ?>
    <?php if($i%2 === 0) echo '<div class="row">' ?>
        <div class="col-sm-6">
            <img class="lazy store-img" src="IMAGE_SOURCE" alt="" title="">
            <h3>IMAGE_NAME</h3>
            <button href="#" class="btn btn-primary">Purchase Image</button>
        </div>
    <?php if($i%2 === 0) echo '</div>' ?>
<?php $i++; endforeach ?>

I am trying to get the images to be pulled from a directory and each image is wrapped in a .col-sm-6 div then each 2 are wrapped in a .row div, then if there is an odd number of the files the last one would just be one .col-sm-6 div on its own. Below is the structure that I am trying to achieve:

<div class="row">
    <div class="col-sm-6">
       <img class="lazy store-img" src="IMAGE_SOURCE" alt="" title="">
       <h3>IMAGE_NAME</h3>
       <button href="#" class="btn btn-primary">Purchase Image</button>
    </div>
   <div class="col-sm-6">
       <img class="lazy store-img" src="IMAGE_SOURCE" alt="" title="">
       <h3>IMAGE_NAME</h3>
       <button href="#" class="btn btn-primary">Purchase Image</button>
    </div>
 </div>

<div class="row">
    <div class="col-sm-6">
       <img class="lazy store-img" src="IMAGE_SOURCE" alt="" title="">
       <h3>IMAGE_NAME</h3>
       <button href="#" class="btn btn-primary">Purchase Image</button>
    </div>
 </div>

The IMAGE_NAME I would add something to pull through the imported filename so it can be displayed on the page and the IMAGE_SOURCE would be the images URL so for now please ignore those bits of text as I don't think it will affect this?

I am new to PHP and just can't seem to find why nothing is showing on the page unless this code is not suited to my needs?

P.s. if any of my syntax's are incorrect I apologies in advance.

  • 写回答

1条回答 默认 最新

  • doumeng06063991 2018-07-20 18:40
    关注

    $images will be filled by your golb

    So basically you just edit the line

    <?php if($i%2 === 0) echo '</div>' ?>
    

    To

    <?php if($i%2 === 1) echo '</div>' ?>
    

    Here a more readable example:

    <?php
        $images = [
            'https://via.placeholder.com/10x50','https://via.placeholder.com/20x50','https://via.placeholder.com/30x50','https://via.placeholder.com/40x50',
            'https://via.placeholder.com/50x50','https://via.placeholder.com/60x50','https://via.placeholder.com/70x50',
        ];
    ?>
    
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css"/>
    <?php
        $cols = 2; // dividable by 12 please
        for( $i = 0; $i < count($images) && $image = $images[$i]; $i++) {
            if( $i%$cols == 0 ) { echo '<div class="row">'; }
    
            echo '<div class="col-sm-' . 12/$cols . '">';
            echo '<img class="lazy store-img" src="' . $image . '" alt="" title="">';
            echo '<h3>IMAGE_NAME</h3>';
            echo '<button href="#" class="btn btn-primary">Purchase Image</button>';
            echo '</div>';
    
    
            if( $i%$cols == $cols-1 ) { echo '</div>'; } /* here comes the magic :) */
        }
        if( count( $images )%$cols > 0 ) {
            echo '</div>';
        }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?