Forum Moderators: coopster

Message Too Old, No Replies

Help with while loop inside function

loops, function,php

         

rodriguez1804

2:06 pm on Sep 19, 2009 (gmt 0)

10+ Year Member



Hey guys, I have the following function to get the user's pictures. It first sees how many pictures the user has and then retrieves each one and displays it to browser.

Problem is, for a user of only 1 picture, it outputs 2 pictures (the same picture twice). Any ideas as to what I am doing wrong?

Here's the code:


function getUserPicName($myusername) //get user pics
{
$dbConn = new dbConnection();
$dbConn->connect();

$username = '';
$picName = '';
$picCount = 0;
$value = '';

$username = mysql_real_escape_string(stripslashes($myusername));
$picCount = $dbConn->getUserPicCount($username);

if($picCount == 0){//Don't do anything if picCount is = 0
}else if($picCount >0){

$sql = "SELECT picName FROM userpictures WHERE username='$username'";
$result = mysql_query($sql) or die(mysql_error());

$count=mysql_num_rows($result) or die("Could not get picName.");

if(!empty($count)){

while($info=mysql_fetch_array($result))
{
$myProfilePic = "<img src='(path to user pic)'/>";

$this->value .= '

<div id="imageContainer">
<div id="selectImage" class="smallTxt"><p>'.$picName.'</p></div>

<div id="showImage">
<p>'.$myProfilePic.'</p>
</div>
</div>';
}

return $this->value;

}else{die("Could not find a picName value");}

}

}


$this->value is just putting all that stuff in a buffer so that it will display properly when called from the middle of the page.

By the way, I also tried using a for loop, but same the thing happened.
Thanks for the help!

Tommybs

3:38 pm on Sep 19, 2009 (gmt 0)

10+ Year Member



Are you definitely sure there's only 1 pic? I know this sounds stupid but I've made mistakes like this before where I've added something twice and not realised. Try displaying the value of count or running your sql query in your mysql admin interface and see what is returned

rodriguez1804

4:05 pm on Sep 19, 2009 (gmt 0)

10+ Year Member



Thanks for the tip on running the query in myslq admin tommybs, I had not thought about that.

Well, I just checked the mysql query and for a user with 2 pictures. It returned just that (2 pictures).

I think I am screwing up on the logic somewhere. I am going to try and rewrite the whole thing from scratch and see if I catch/fix the error. In the meantime, if anybody sees my error, please point it out.

Tommybs

4:17 pm on Sep 19, 2009 (gmt 0)

10+ Year Member



Have you tried commenting out the display code at various points and seeing whats happening? Are both the pics being displayed in the <div id="showImage"> or is there something else on the page that is being called perhaps?

idfer

5:07 pm on Sep 19, 2009 (gmt 0)

10+ Year Member



Well among other potential bugs, you're appending the HTML code to $this->value in every call, so if you call that function twice, $this->value will end up having two copies of the same code.

Maybe you meant to set

$this->value = '';

near the top of the function instead of

$value = '';

Hope this helps.

rodriguez1804

5:17 pm on Sep 19, 2009 (gmt 0)

10+ Year Member



wow! lol, you saved the idfer. Thanks!

By the way, care to point out those "other potential bugs" in my code. I am up for some criticism:)Thanks.

idfer

7:18 pm on Sep 19, 2009 (gmt 0)

10+ Year Member



OK you asked for it :) Most are styling stuff that could introduce bugs when you change the code, either inside the function or in the way you call it:

- $picName, you seem to initialize to '' and never set it again. But then you use it inside the loop.

- $picCount, you're checking the number of pics twice + fetching all the pics in the loop. Best is to calculate picCount in only one way so you don't confuse yourself. Personally, i'd forget about the picCount, get rid of all the if statements, and just fetch the rows:

[pre]$this->value = '';
$picCount = 0; // only if you need to use it later.
$sql = "SELECT picName FROM userpictures WHERE username='$username'";
$result = mysql_query($sql) or die(mysql_error());
while($info=mysql_fetch_array($result)) {
$picCount++; // only if you need to use it later.
...
$this->value .= 'whatever';
}[/pre]

Return value, you return something when there are pics, nothing when there aren't.

- HTML code, you enclose some attribute values in single-quotes (src=), and others in double-quotes. Pick one style and use it everywhere. Personally, i use double-quotes inside HTML because both htmlspecialchars() and htmlentities() encode that one without any extra flags.

- mysql_real_escape_string(stripslashes(...)), that's a big warning bell for me. If you need to strip anything inside such a specific function, you're in trouble. It means that the passed-in value has already been encoded (escaped) for a specific format and you need to undo that to carry on. It's best to pass around values in their rawest format and encode them for specific output formats at the last moment. If $myusername has escaped characters because of magic_quotes or some other reason, it's best to make the calling code undo that. Personally, i would simply do this:

[pre]$sql = "SELECT picName FROM userpictures "
. " WHERE username='".mysql_real_escape_string($myusername)."'";[/pre]

Hope that wasn't too harsh. :)

rodriguez1804

7:39 pm on Sep 19, 2009 (gmt 0)

10+ Year Member



not the least; rather, thank you so much!

Criticism for me = learning opportunity, so thanks for all the feedback. I ll try and implement these suggestions you gave me in my code from now on. Thanks again!