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!
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:
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?
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.
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:
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?
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?
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!
<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;"> </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
$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
{