Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

php while loop counter not incrementing

Posted on 2010-08-29
10
Medium Priority
?
381 Views
Last Modified: 2012-05-10
Hello,

I'm trying to use the $counter variable as a counter so that it keeps track of how many times the while loop gets executed and then uses that value in my sql statement.  $counter is always 1 in both the sql statement and when I echo "pre=".$counter;.  I know the while loop is getting executed 4 times.  Any ideas?

if ($handle = opendir($upload_directory_from)){
			$counter = 1;
			$orig_w = 450;
			while (false !== ($file = readdir($handle))){
				if($file != '.' && $file != '..'){
					$fileparts  = array();
					$fileparts = explode(".",$file);
					$fileext = $fileparts[1];
					list($width, $height) = getimagesize($upload_directory_from.$file);
					$orig_h = ($height/$width)*$orig_w;
					$src = '';
					if (strtolower($fileext) == 'png') {
						$src = imagecreatefrompng($upload_directory_from.$file);
					}
					else if(strtolower($fileext) == 'gif'){
						$src = imagecreatefromgif($upload_directory_from.$file);
					}
					else if(strtolower($fileext) == 'jpg'){
						$src = imagecreatefromjpeg($upload_directory_from.$file);
					}
					else{
						echo "File type not supported, .gif, .jpg or .png required!";
					}	
					$tmp = imagecreatetruecolor($orig_w, $orig_h);
					imagecopyresampled($tmp, $src,                  0,0,0,0,$orig_w,$orig_h,$width,$height);
					if (strtolower($fileext) == 'png') {
						imagepng($tmp,$upload_directory_to.$file,100);
					}
					else if(strtolower($fileext) == 'gif'){
						imagegif($tmp,$upload_directory_to.$file,100);
					}
					else if(strtolower($fileext) == 'jpg'){
						imagejpeg($tmp,$upload_directory_to.$file,100);
					}
					else{
						echo "File type not supported, .gif, .jpg or .png required!";
					}						
					imagedestroy($tmp);
					imagedestroy($src);
					unlink($upload_directory_from.$file);
					$con = mysql_connect('localhost', 'musicopus1', 'octopus1');
					if (!$con){
					  die('Could not connect: ' . mysql_error());
					}
					mysql_select_db("dmname", $con);
					mysql_query("INSERT INTO GALLERY_IMAGES (IMAGE_NAME, CAPTION, ALBUM_COVER, ALBUM_ID, ORDER_ID)
						VALUES ('$file', 'TEST', 'N', 1, ".$counter.")");
					mysql_close($con);
					echo "pre=".$counter;
					$counter = $counter + 1;
					echo "postr=".$counter;					
				}
			}
				closedir($handle);
			}
		
	

Open in new window

0
Comment
Question by:cgray1223
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 

Author Comment

by:cgray1223
ID: 33555631
lines of special attention are 2 and 46-51
0
 
LVL 84

Accepted Solution

by:
Dave Baldwin earned 400 total points
ID: 33555653
You have it inside the 'if' statement, move it down below the '}' so it is only in the 'while'.
}
echo "pre=".$counter;
$counter = $counter + 1;
echo "postr=".$counter;					
			}

Open in new window

0
 

Author Comment

by:cgray1223
ID: 33555671
The counter incrementor code is inside an if statement which is inside the while statement, so that should be ok correct?.  the echo statements get echoed out multiple times so I know it's hitting the $counter = $counter + 1; code but when it gets to the echo "pre=".$counter; its always 1.  The echo "postr=".$counter;       always shows 2.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 9

Assisted Solution

by:Snarfles
Snarfles earned 400 total points
ID: 33555717
Looks pretty clean to me... :/

Try changing the variable name and see if something is interfering with $counter specifically... eg change to $counter1
0
 

Author Comment

by:cgray1223
ID: 33555727
that didn't make a difference either....
0
 
LVL 58

Assisted Solution

by:cyberkiwi
cyberkiwi earned 400 total points
ID: 33555742
Can you try two things?

1, Rename it.  Instead of counter, make it just $c.
You might have $counter updated elsewhere since $counter is globally visible.

2, Instead of the line

echo "pre=".$counter;

use

var_dump($counter);

var_dump shows the variable type as well as the value.
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 33556039
I suggested moving the counter code to see if the 'if' statement wasn't being executed.  If you move it and get a different answer, then 'if' part isn't executing.
0
 
LVL 5

Assisted Solution

by:innotionent
innotionent earned 400 total points
ID: 33557839
If it was me i'd do this.

take
$counter = $counter + 1;
and do
$counter++;
then echo
echo $counter;

give that  a try.
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 400 total points
ID: 33561094
Wow, this would be a lot easier to debug if there were consistently indented control structures and some comments in the code.

Here is what I would suggest.  Add several echo or var_dump statements to the code so that you can see the steps as they are executed.

Add error_reporting(E_ALL) to the top of your script so you can see if there are any places that depend on undefined variables.

Test the mysql() functions for success and report the errors to the browser output stream.

You wrote, "I know the while loop is getting executed 4 times.  Any ideas?" -- but how do you know that?  What tells us that?

Also, I would recommend getting the connect and select out of the loop, but that should not be hurting your counter (unless these statements are failing and you are looking in the data base to find the counter).
if ($handle = opendir($upload_directory_from))
{
    $counter = 1;
    $orig_w = 450;
    while (false !== ($file = readdir($handle)))
    {
        if($file != '.' && $file != '..')
        {
            $fileparts  = array();
            $fileparts = explode(".",$file);
            $fileext = $fileparts[1];
            list($width, $height) = getimagesize($upload_directory_from.$file);
            $orig_h = ($height/$width)*$orig_w;
            $src = '';
            if (strtolower($fileext) == 'png')
            {
                $src = imagecreatefrompng($upload_directory_from.$file);
            }
            else if(strtolower($fileext) == 'gif')
            {
                $src = imagecreatefromgif($upload_directory_from.$file);
            }
            else if(strtolower($fileext) == 'jpg')
            {
                $src = imagecreatefromjpeg($upload_directory_from.$file);
            }
            else
            {
                echo "File type not supported, .gif, .jpg or .png required!";
            }
            
            $tmp = imagecreatetruecolor($orig_w, $orig_h);
            imagecopyresampled($tmp, $src, 0,0,0,0,$orig_w,$orig_h,$width,$height);
            
            if (strtolower($fileext) == 'png')
            {
                imagepng($tmp,$upload_directory_to.$file,100);
            }
            else if(strtolower($fileext) == 'gif')
            {
                imagegif($tmp,$upload_directory_to.$file,100);
            }
            else if(strtolower($fileext) == 'jpg')
            {
                imagejpeg($tmp,$upload_directory_to.$file,100);
            }
            else
            {
                echo "File type not supported, .gif, .jpg or .png required!";
            }
            
            imagedestroy($tmp);
            imagedestroy($src);
            unlink($upload_directory_from.$file);

            $con = mysql_connect('localhost', 'musicopus1', 'octopus1');
            if (!$con)
            {
                die('Could not connect: ' . mysql_error());
            }
            mysql_select_db("dmname", $con);
            mysql_query("INSERT INTO GALLERY_IMAGES (IMAGE_NAME, CAPTION, ALBUM_COVER, ALBUM_ID, ORDER_ID) VALUES ('$file', 'TEST', 'N', 1, ".$counter.")");
            mysql_close($con);
            echo "pre=".$counter;
            $counter = $counter + 1;
            echo "postr=".$counter;     
        }
    }
    closedir($handle);
}

Open in new window

0
 

Author Comment

by:cgray1223
ID: 33564260
There was no problem with the counter as it was functioning as it should.  The php script that I was calling is getting executed per file being uploaded.  This script is called once per file being uploaded instead of processing the file upload queue together, thus the counter is always 1.  Appreciate the help!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question