douhui1957 2017-03-07 15:19
浏览 30
已采纳

替代图像在PHP中不起作用

I'm trying to show an image if the intended image cannot be found. The images are taken from an RSS feed. The original code is last.

Ive tried a few things. But doesnt seem to be working. Its only showing the backup images: blank_newprod.png.

New code:

foreach($channel->item as $item){
            //if($i==6){break;}
            if($item->prodimg=="~"){break;}
            if($item->prodpage=="~"){break;}
            $i += 1;
            echo '<span ';
            switch ($i) {
                case 1: echo 'style="margin-right:18px;">'; break;
                case 2: echo 'style="margin-right:18px;">'; break;
                case 3: echo '>'; break;
                case 4: echo 'style="margin-right:18px;">'; break;
                case 5: echo 'style="margin-right:18px;">'; break;
                case 6: echo '>'; $i = 0; break;
            }
            $filename = '.$item->prodimg.';

            if (file_exists($filename)) {
            echo '<a href="'.$item->prodpage.'" title="'.$item->$title.'"><img src="'.$item->prodimg.'" width="181" height="100" alt="'.$item->$title.'" /></a></span>';
            } else {
            echo '<img src="../includes/php/blank_newprod.png"/>';
            }
        }

Original code:

foreach($channel->item as $item){
            //if($i==6){break;}
            if($item->prodimg=="~"){break;}
            if($item->prodpage=="~"){break;}
            $i += 1;
            echo '<span ';
            switch ($i) {
                case 1: echo 'style="margin-right:18px;">'; break;
                case 2: echo 'style="margin-right:18px;">'; break;
                case 3: echo '>'; break;
                case 4: echo 'style="margin-right:18px;">'; break;
                case 5: echo 'style="margin-right:18px;">'; break;
                case 6: echo '>'; $i = 0; break;
            }
            echo '<a href="'.$item->prodpage.'" title="'.$item->$title.'"><img src="'.$item->prodimg.'" width="181" height="100" alt="'.$item->$title.'" /></a></span>';
        }
  • 写回答

1条回答 默认 最新

  • duanmeng9336 2017-03-07 15:26
    关注

    This doesn't look like a valid file name:

    $filename = '.$item->prodimg.';
    

    Surely you meant to just use the value itself?:

    $filename = $item->prodimg;
    

    Of course, if that value is a URL (as implied by its later use) then to check for the corresponding file you'd need to convert it to a file system path. Fortunately, that's been asked already.

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

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历