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>';
        }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀