dozug64282 2014-04-10 00:49
浏览 96
已采纳

PHP计算for循环中if条件语句的值

I have a PHP code to see if one or many pictures exist. If the picture exist I could like to count them and echo the answer. This is my code:

<?php
//Start Pictures section - dictates that if there are pictures this section is shown - if not this section is now shown.
for ($x=1; $x<=21; $x++)  {
    if($x<=9) {
        $picValue = 'picture0'.$x;
    }
    else {
        $picValue = 'picture' . $x;
    }

    $imageURLpixel = ABSOLUTE_URL_IMG.$code.'/pixel/'. $picValue .'.jpg';

    //Check if the image exists or not
    $pictureCount = 1;
    if (@fopen($imageURLpixel,'r')) {
    $pictureCount++;    
    $pictureCounter = count($pictureCount);
    }

 echo $pictureCounter;

} 

?>

I have 3 pictures in my exampe and it is being output as 111111111111111111111 - I would the output to be as 3. I am not getting any errors in my error log.

  • 写回答

3条回答 默认 最新

  • dousi1875 2014-04-10 01:03
    关注

    Just to make it clear. The solutions are until this one are all fixing "some issues" with the code, but not all together.

    Here is my approach to make it clear, understandable and readable - maybe some learning curve etc.

    $baseUrl = ABSOLUTE_URL_IMG.$code.'/pixel/';
    $pictureCount = 0;
    
    // for the first 20 pictues
    for ($x=0; $x<21; $x++)  {
          // make it more readable and practical - see "sprintf"-documentation.
          $filename = sprintf('picture%02d.jpg', $x+1); // < is "one-based index"
    
          $fileUrl = $baseUrl . $filename;
    
          // if url exists, increase counter;
          if (@fopen($fileUrl,'r')) 
                $pictureCount++;
    
     }
     // total count of existing images.
     echo $pictureCount; 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)