Code not looping like planned

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Code not looping like planned

Post by waradmin »

So I have 2 entries in a database, and I want each one displayed in a table. Here is the code I have, but it only display's the first entry and none of the ones that follow. Heres the code, I know its messy, but it is what it is:

Code: Select all

<table width="100%" border="0" cellspacing="0" cellpadding="5">
					<?php
					$result = mysql_query("SELECT * FROM wall WHERE global_id='$_GET[id]'") or die(mysql_error());
					$row_count = mysql_num_rows( $result );
					if($row_count > 0) {
						$initial_count = 0;
						while($initial_count < $row_count)
						{
					?>
                      <tr>
                        <td width="16%" rowspan="3" valign="top"><div align="center">
						<?php
						$result = mysql_query("SELECT * FROM wall WHERE global_id='$_GET[id]'") or die(mysql_error());
						$row = mysql_fetch_array( $result );
						$message = $row['message'];
						$to_id = $row['global_id'];
						$from_id = $row['poster_global_id'];
						$when = $row['when'];
						$result = mysql_query("SELECT * FROM profile_photo WHERE global_id='$_GET[id]'") or die(mysql_error());
						$row = mysql_fetch_array( $result );
						$picture = $row['profile_photo_url'];
						include('functions.php');
						$scale = imageScale($picture, 50, -1);
						$width = $scale[0];
						$height = $scale[1];
						echo "<img src=\"$picture\" width=\"$width\" height=\"$height\">";
						?></div></td>
                        <td width="84%" style="border-top: 1px solid #3b5998; border-bottom: 1px solid #d8dfea; background: #f7f7f7;"><span class="style2">
						<?php
						$result = mysql_query("SELECT * FROM loginphp WHERE global_id='$from_id' ORDER BY id desc") or die(mysql_error());
						$row = mysql_fetch_array( $result );
						echo $row['Fname'];
						echo " ";
						echo $row['Lname'];
						?></span>
						<?php
						echo " at " . $when . "";
						?></td>
                      </tr>
                      <tr>
                        <td><?php echo $message; ?></td>
                      </tr>
                      <tr>
                        <td style="border-bottom: 1px solid #d8dfea;">&nbsp;</td>
                      </tr>
					  <?php
					 	 	$initial_count++;
					  	 }
					  } ?>
                    </table>
Any idea why it wont repeat for each entry in the db and quits after it finds the first one?

Much thanks in advance.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Within each while($initial_count < $row_count) loop a new query
$result = mysql_query("SELECT * FROM wall WHERE global_id='$_GET[id]'") or die(mysql_error());
is sent and then the first record of the result is fetched - over and over again. What for?
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post by waradmin »

Well, the problem I believe I am running into is the fact that $row is assigned with the data from the query on the wall table. So then the variables $to_id, $from_id, etc are set with that query and $row tied to that.

But as the code that is executed for each entry continues, it takes $row and assigns it to different querys, thus once it returns to the top, $row['to_id'] for example will not be tied to the query its supposed to be linked to (the initial one outside of the while loop), right?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

in each iteration of the loop
result = mysql_query("SELECT * FROM wall WHERE global_id='$_GET[id]'") or die(mysql_error());
is executed. Therefore $result is each time a new result set. Therefore $row = mysql_fetch_array( $result ); always fetches the first record of that result set.
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post by waradmin »

even with that line taken out, it still only retrives the first entry and none of the others.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

did you check for similar errors with the other querries as well and and corrected them if necessary? Please do so, then post your current code.
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post by waradmin »

Say I were to just change each $result and then $row assigned to the new query to $result2=, $row2=, $result 3,= $row3=, will the script know to take the next values in the query and assign them to the variables:

Code: Select all

$message = $row['message'];
$to_id = $row['global_id'];
$from_id = $row['poster_global_id'];
$when = $row['when'];
If it will know to still use the query outside the if and while statement for those values I will be able to figure it out. So my question, does the query outside of the if and while loop remain active so I can grab its values each time around and assign them to the variables?

Thanks for all the help
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

waradmin wrote:So my question, does the query outside of the if and while loop remain active so I can grab its values each time around and assign them to the variables?
Haven't read the other text, but: yes.

Your current code is?
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post by waradmin »

So now i have reassigned every value of $row and $result to $row# and $result# to keep the initial $result query set to use $row:
Problem SOLVED! Turns out you cant include the function inside of the while loop, when I move the function include to the outside, it runs great!

Code: Select all

<table width="100%" border="0" cellspacing="0" cellpadding="5">
<?php
include('functions.php');
$result = mysql_query("SELECT * FROM wall WHERE global_id='$_GET[id]'") or die(mysql_error()); //get entry's to read values
$row_count = mysql_num_rows( $result ); //count rows
$row = mysql_fetch_array( $result );
if($row_count > 0) { //if row's is more than 0 do this:
       $initial_count = 0; //create variable named initial_count and set to 0
       while($initial_count < $row_count) //wile initial_count (which is 0 to begin with) is LESS than the row_count value, do this
       {
	?>
<tr>
<td width="16%" rowspan="3" valign="top"><div align="center">
<?php

$message = $row['message']; //pull this row from the query above
$to_id = $row['global_id']; //pull this row from the query above
$from_id = $row['poster_global_id']; //pull this row from the query above
$when = $row['when']; //pull this row from the query above
$result2 = mysql_query("SELECT * FROM profile_photo WHERE global_id='$_GET[id]'") or die(mysql_error()); //new query, result2
$row2 = mysql_fetch_array( $result2 ); //set row2 to use result2 query
$picture = $row2['profile_photo_url'];
$scale = imageScale($picture, 50, -1);
$width = $scale[0];
$height = $scale[1];
echo "<img src=\"$picture\" width=\"$width\" height=\"$height\">";
?></div></td>
<td width="84%" style="border-top: 1px solid #3b5998; border-bottom: 1px solid #d8dfea; background: #f7f7f7;"><span class="style2">
<?php
$result3 = mysql_query("SELECT * FROM loginphp WHERE global_id='$from_id' ORDER BY id desc") or die(mysql_error());
$row3 = mysql_fetch_array( $result3 );
echo $row3['Fname'];
echo " ";
echo $row3['Lname'];
?></span>
<?php
echo " at " . $when . "";
?></td>
</tr>
<tr>
<td><?php echo $message; ?></td>
</tr>
<tr>
<td style="border-bottom: 1px solid #d8dfea;">&nbsp;</td>
</tr>
<?php
$initial_count++; //add 1 to the count of initial_count, then go back to top
     } //end of while
} //end of if ?>
</table>
But still, not working, its only displaying the first entry, and none others
shneoh
Forum Commoner
Posts: 38
Joined: Mon Sep 25, 2006 2:23 am
Location: Malaysia

Post by shneoh »

Just a share with you, the loop can form like this as well do the right job:

Existing:

Code: Select all

$row = mysql_fetch_array( $result );
if($row_count > 0) { //if row's is more than 0 do this:
       $initial_count = 0; //create variable named initial_count and set to 0
       while($initial_count < $row_count) //wile initial_count (which is 0 to begin with) is LESS than the row_count value, do this
       {
Another option:

Code: Select all

if($row_count >0){  // if row's is more than 0 do this:
    while($row=mysql_fetch_array($result)){  // loop until end of record set.
Post Reply