Solved

php while loop counter not incrementing

Posted on 2010-08-29
10
378 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 83

Accepted Solution

by:
Dave Baldwin earned 100 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Assisted Solution

by:Snarfles
Snarfles earned 100 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 100 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 83

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 100 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 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JSON decode 5 46
Which is best Image Resizing Web service 11 42
Ajax success not firing alert 6 45
foreach loop to only capture results that are greater than 0 16 42
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

752 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