简体   繁体   中英

PHP count values of an if conditional statement within a for loop

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.

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; 
$pictureCount++;    
$pictureCounter = count($pictureCount);

The first line above contains the number of pictures found. So you don't need to do anything else to get that count which makes the next line unnecessary. And that's important because you're using count() incorrectly. count() is to be used for counting the number of elements in an array. $pictureCount is not an array.

Also, you should initialize $pictureCount to zero unless you know you already have one image accounted for. Otherwise your total will be inflated by one.

Also, you initialize $pictureCount and echo it inside your loop. Both parts need to be outside of your loop.

Corrected code:

$pictureCount = 0;
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
    if (@fopen($imageURLpixel,'r')) {
      $pictureCount++;    
    }
} 
echo $pictureCount;

1, see my comment on your question. You do not want to count() $pictureCount .

2, you are echoing in your for loop. count($pictureCount) always outputs 1, but in your case, every time your for loop iterates. Try code more like this:

$pictureCount = 1;
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
    if (@fopen($imageURLpixel,'r')) {
        $pictureCount++;
    }
}

echo $pictureCount; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM