doudao0660 2013-11-18 14:24
浏览 273
已采纳

Php使用数组显示图片

I'm trying to show images from some directory using foreach.. But the problem is it's showing results in array, so if i want to print out first image I have to use $imag['0']..

Is there any way that I can bypass this number in this brackets?

Here's my code...

<?php
$domena = $_SERVER['HTTP_HOST'];
$galerija = $_POST['naziv'];
$galerija = mysql_real_escape_string($galerija);

define('IMAGEPATH', 'galleries/'.$galerija.'/');

foreach(glob(IMAGEPATH.'*') as $filename){
    $imag[] =  basename($filename);
?>
<img src="http://<?php echo $domena; ?>/galerija/galleries/<?php echo $galerija; ?>/<?php echo $imag['0']; ?>">
  • 写回答

2条回答 默认 最新

  • duan051347 2013-11-18 14:28
    关注

    Well you could first not create the array in the foreach statement and instead just print the img:

    echo '<img src="', $domena ,'/galerija/galleries/', $galerija ,'/', $filename,'">';
    

    Or you could iterate the array.

    foreach($imag as $img): ?>
        <img src="http://<?php echo $domena; ?>/galerija/galleries/<?php echo $galerija; ?>/<?php echo $img ?>">
    <?php endforeach; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图