0

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
  • count() gives you the number of elements in an array. In this code, you're using count on an integer. That won't give you the result you want. Instead, $pictureCount already contains the data you need. Commented Apr 10, 2014 at 0:51
  • This would give me 222111112111111111111 Commented Apr 10, 2014 at 0:54
  • Not sure if it makes a big difference, but there is a function file_exists() that might be better suited for this. Commented Apr 10, 2014 at 1:07

3 Answers 3

2

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; 
Sign up to request clarification or add additional context in comments.

1 Comment

Great it worked. Basically I had to define $pictureCount out of the for loop. Silly me. Thank you.
0
$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;

3 Comments

The out put is 111000001000000000000
It works but I get Undefined variable: pictureCount in my error_log. If I set $pictureCount to either 1 or 0 - the output will be either 1 or 0;
Are you sure it's from this code? It is clearly defined here.
0

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; 

3 Comments

I tried it and the output is 1 despite the fact that I have 3 files.
For sure it is! Steve Sets the "$pictureCont" in EVERY run of the For-Loop to "one"... correct answer will follow.
Fixed. user3173207, you need to avoid declaring variables in loops.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.