Page 1 of 1

Code not looping like planned

Posted: Tue Sep 26, 2006 4:58 pm
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.

Posted: Tue Sep 26, 2006 5:09 pm
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?

Posted: Tue Sep 26, 2006 5:16 pm
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?

Posted: Tue Sep 26, 2006 5:22 pm
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.

Posted: Tue Sep 26, 2006 5:27 pm
by waradmin
even with that line taken out, it still only retrives the first entry and none of the others.

Posted: Tue Sep 26, 2006 5:29 pm
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.

Posted: Tue Sep 26, 2006 5:37 pm
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

Posted: Tue Sep 26, 2006 5:44 pm
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?

Posted: Tue Sep 26, 2006 7:39 pm
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

Posted: Tue Sep 26, 2006 11:07 pm
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.