Page 1 of 1
Having problems starting a new line
Posted: Thu Jul 19, 2007 8:36 am
by thefreebielife
Code: Select all
<?
$ref = $_GET['r'];
if ($_GET['r'] != "") {
$rid = $_GET['r'];
}
//select the table
$result = mysql_query("select * from gifts order by gId");
$i = 1;
//grab all the content
while($r=mysql_fetch_array($result))
{
$gid=$r["gId"];
$gname=$r["gName"];
$gdescription=$r["gDescription"];
$gimage=$r["gImage"];
$gprice=$r["gPrice"];
?>
<Td>
<Table style="width:200px "><tr><td><div class="top"><div class="gimage" align="center"><img src="<? echo "$gimage"; ?>" border="0"></div></td></tr>
<tr><td align="center"><div class="gname"><? echo "$gname"; ?></div></td></tr>
<tr><td><div class="gdescription"><? echo stripslashes($gdescription); ?></div></td></tr>
<tr><td><div class="gprice" align="left"><font color="#000000">Price:</font> $<? echo "$gprice"; ?><font color="#000000"> <a href="register.php?gid=<? echo "$gid"; ?>&&r=<? echo "$rid"; ?>">Get It Free</a></font><font color="#FF9900" style="font-weight:bold ">>></font> </div></td></tr>
</Table>
</Td>
<?
if ($i % 2 == 0) { echo "</tr><tr>"; }
$i++;
}
?>
At the bottom I would think that $i would create a new line every time 2 tables get posted in a row, but it is not working correctly. I have used this code before but cannot figure out why it won't work now.
Posted: Thu Jul 19, 2007 9:22 am
by Chalks
What exactly isn't working correctly? Is $i not incrementing? Are you getting an error message?
is "$r=mysql_fetch_array($result)" really a valid argument for an if statement? It seems a little odd.
Posted: Thu Jul 19, 2007 11:27 am
by thefreebielife
Well $i doesn't really increment, the table displays images and I want it to start a new line after 2 are on a row.
Posted: Thu Jul 19, 2007 11:38 am
by Chalks
actually, if I'm reading it correctly, $i should increment every time you go through the while loop. The "$i % 2" isn't actually storing the result in $i, so $i just keeps going up. Also, $i is never reset within the while loop, so...
Not that it really matters. Even if $i was 501... 501 mod 2 is still 1, and 502 mod 2 is still 0.
Ah, I think I found the answer:
from
here:
Typer85 wrote:Please be advised that the resource result that you pass to this function can be thought of as being passed by reference because a resource is simply a pointer to a memory location.
Because of this, you can not loop through a resource result twice in the same script before resetting the pointer back to the start position.
For example:
Code: Select all
<?php
// Assume We Already Queried Our Database.
// Loop Through Result Set.
while( $queryContent = mysql_fetch_row( $queryResult ) {
// Display.
echo $queryContent[ 0 ];
}
// We looped through the resource result already so the
// the pointer is no longer pointing at any rows.
// If we decide to loop through the same resource result
// again, the function will always return false because it
// will assume there are no more rows.
// So the following code, if executed after the previous code
// segment will not work.
while( $queryContent = mysql_fetch_row( $queryResult ) {
// Display.
echo $queryContent[ 0 ];
}
// Because $queryContent is now equal to FALSE, the loop
// will not be entered.
?>
----------------
The only solution to this is to reset the pointer to make it point at the first row again before the second code segment, so now the complete code will look as follows:
----------------
Code: Select all
<?php
// Assume We Already Queried Our Database.
// Loop Through Result Set.
while( $queryContent = mysql_fetch_row( $queryResult ) {
// Display.
echo $queryContent[ 0 ];
}
// Reset Our Pointer.
mysql_data_seek( $queryResult );
// Loop Again.
while( $queryContent = mysql_fetch_row( $queryResult ) {
// Display.
echo $queryContent[ 0 ];
}
?>
----------------
Of course you would have to do extra checks to make sure that the number of rows in the result is not 0 or else mysql_data_seek itself will return false and an error will be raised.
Also please note that this applies to all functions that fetch result sets, including mysql_fetch_row, mysql_fetch_assos, and mysql_fetch_array.
So... your loop isn't working for the above reason (I think).
Edit: actually, now that I read this again... maybe not. Hmm. *looks around for a different answer*.
Posted: Thu Jul 19, 2007 11:48 am
by thefreebielife
Yeah i tried that but didn't fix the problem. This worked before, but I don't why not now...
Posted: Thu Jul 19, 2007 11:57 am
by Chalks
What _exactly_ is the problem?
Are you getting an error message?
Is it only printing one table?
Is it printing 50 tables, but no extra lines?
Is it not entering the while loop?
Posted: Thu Jul 19, 2007 12:25 pm
by thefreebielife
No error messages, I have 4 images in a vertical row, when i want 2 in a row, then 2 in the next.
Posted: Thu Jul 19, 2007 12:40 pm
by Chalks
ok, got it now. It's really simple:
You're missing the initial <table> tags.
Code: Select all
<?
$ref = $_GET['r'];
if ($_GET['r'] != "") {
$rid = $_GET['r'];
}
//select the table
$result = mysql_query("select * from gifts order by gId");
$i = 1;
//grab all the content
?>
<table>
<?php
while($r=mysql_fetch_array($result))
{
$gid=$r["gId"];
$gname=$r["gName"];
$gdescription=$r["gDescription"];
$gimage=$r["gImage"];
$gprice=$r["gPrice"];
?>
<Td>
<Table style="width:200px "><tr><td><div class="top"><div class="gimage" align="center"><img src="<? echo "$gimage"; ?>" border="0"></div></td></tr>
<tr><td align="center"><div class="gname"><? echo "$gname"; ?></div></td></tr>
<tr><td><div class="gdescription"><? echo stripslashes($gdescription); ?></div></td></tr>
<tr><td><div class="gprice" align="left"><font color="#000000">Price:</font> $<? echo "$gprice"; ?><font color="#000000"> <a href="register.php?gid=<? echo "$gid"; ?>&&r=<? echo "$rid"; ?>">Get It Free</a></font><font color="#FF9900" style="font-weight:bold ">>></font> </div></td></tr>
</Table>
</Td>
<?
if ($i % 2 == 0) { echo "</tr><tr>"; }
$i++;
}
?>
</table>
<?php
//rest of code
?>
Posted: Thu Jul 19, 2007 12:48 pm
by thefreebielife
So I am! What a stupid mistake.
Well thank you so much for your help!
Posted: Thu Jul 19, 2007 12:55 pm
by RobertGonzalez
You never open the row that you are trying to close. Do a view source of the output and see just how jacked up your markup is at the moment.