I'm trying to amend the following code, so that it retrieves one row, writes the results to a table cell (<td> ... </td>), then retrieves the next row, and writes to the next table cell (<td> ... </td>), then ends the row and starts a new one ( </tr><tr> ) and then does it all again... so in effect i'm trying to have to columns of data displayed.
Not quite sure how to go about doing this.. Here's the code i've made for 1 column of data from the db. I'm just not sure how to iterate the loop (ie: add $row+1) in between cell 1 and cell 2.
Hoping somebody can help solve this simple problem.
Here's the current working code, with only 1 column:
Function GetReviewIntros() {
$sqlGetReviews = "SELECT * FROM reviews ORDER BY 'reviewdate' DESC";
include("mysqlconnect.php");
if (mysql_select_db("DC_Content") != TRUE)
{ print "DC_Content could not be opened"; }
else
{ $result = mysql_query($sqlGetReviews); }
while ($row = mysql_fetch_assoc($result)) {
$ImageName = $row["imagefilename"];
Ahh yeah, that's good logic. I understand it, but for some reason it wont work for me.
When i run it, i just get a blank screen. can you see anything wrong with my code, cos i cant. Its definitely the
if fmod($i,2) == 0 { echo ("<tr>") ; }
lines that are causing problems, cos when i comment them out it works fine.
Here's my code:
<?
Function GetReviewIntros() {
$sqlGetReviews = "SELECT * FROM reviews ORDER BY 'reviewdate' DESC"; // "SELECT * FROM reviews WHERE new = '1' ORDER BY 'reviewdate' DESC"
include("mysqlconnect.php");
if (mysql_select_db("DC_Content") != TRUE)
{ print "DC_Content could not be opened"; }
else
{ $result = mysql_query($sqlGetReviews); }
$i = 0;
while ($row = mysql_fetch_assoc($result)) {
$ImageName = $row["imagefilename"];
if fmod($i,2) == 0 {
echo ("<tr>") ;
}
?>
When i run it, i just get a blank screen. can you see anything wrong with my code, cos i cant. Its definitely the
if fmod($i,2) == 0 {
echo ("</tr>") ;
}
lines that are causing problems, cos when i comment them out it works fine.
__________________ Berlin IT Services
- Website Design & Development
- Content Management Systems
Located: Melbourne, Australia
Hum. My bad, probably.
fmod() return the floating point mod, while i meant the modulus-operator. So the code i meant was
PHP Code:
i = 0
while ($row = mysql_fetch_assoc($result)) {
if (i % 2) == 0 {
echo ("<tr>") ;
}
echo ("<td>") ;
rest of the code
echo ("</td>") ;
if fmod(i,2) == 0 {
echo ("</tr>") ;
}
i ++
}
Your method also works, but if you want to have more complicated stuff, like each even row this and each row that can be devided by 3 then etc, you better use the modulus.
The modulus is what is left after deviding a by b
say a = 60 and b = 12, then the modulus is 0. If a = 61, then the modulus is 1 --> 5 X 12 = 60 so there is 1 remaining. (so you substract the maximum number of b's from a, and what is left, is the modulus. The modulus will always be smaller then b)
In your code, the modulus will be
i = 0, modulus = 0
i= 1, modulus = 1
i = 2, modulus = 0
i = 3, modulus = 1
etc
MOD or modulus is an operator. Like -,+, /, * (In ASP, it's a MOD b. In PHP, you have a % b)
fmod() is a function
The code-coloring --> place '['p'h'p'] before and '['/'p'h'p'] after the code (buth without the quotes, of course)
Ahh yeah, okay. I understood how the modulus thing worked.. just didnt know it was called 'modulus'. its been a while since my last maths class. hehehe
and thanks for the code tip.. i'll try it out next time.
cheers
Greg
__________________ Berlin IT Services
- Website Design & Development
- Content Management Systems
Located: Melbourne, Australia