Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-19-2003, 09:47 AM   PM User | #1
BerlinIT
New Coder

 
Join Date: Jun 2003
Location: Melbourne, Australia
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
BerlinIT is an unknown quantity at this point
How to Step through DB, 2 rows at a time?

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"];

?>
<tr>
<td>
<p>
<a href="reviews_main.php?reviewid=<? echo $row["reviewid"]; ?>" target="main">
<? If ($row["imagefilename"] != "") {?>
<img src="reviews/images/<? echo $row["reviewid"]; ?>_thumb.<? echo substr($ImageName,-3); ?>" alt="" width="35" height="35" border="1" align="left">
<? } else { ?>
<img src="images/cover_na.gif" alt="" width="35" height="35" border="0" align="left">
<? } ?>
<? echo $row["artistname"]; ?> - <? echo $row["releasename"];?> <br>
<? echo $row["labelname"]; ?></a><br>
Review Date: <? echo $row["reviewdate"]; ?>
<br><br></p>

</td>
</tr>


<? }
mysql_free_result($result);
mysql_close($linkID);

}

?>





Thanks heaps in advance,
Greg
__________________
Berlin IT Services
- Website Design & Development
- Content Management Systems

Located: Melbourne, Australia
BerlinIT is offline   Reply With Quote
Old 10-19-2003, 10:28 AM   PM User | #2
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf will become famous soon enoughraf will become famous soon enough
You can use an incrementing variable and fmod(i,2)

For all even i's, you'll get fmod(i,2) = 0. Like
PHP Code:
0
while ($row mysql_fetch_assoc($result)) {
  if 
fmod(i,2) == {
    echo (
"<tr>") ;
  }
  echo (
"<td>") ;
  
rest of the code
  
echo ("</td>") ;
  if 
fmod(i,2) == {
    echo (
"</tr>") ;
  }
  
++

raf is offline   Reply With Quote
Old 10-19-2003, 10:47 AM   PM User | #3
BerlinIT
New Coder

 
Join Date: Jun 2003
Location: Melbourne, Australia
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
BerlinIT is an unknown quantity at this point
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>") ;
}
?>

<td>
<p>
<a href="reviews_main.php?reviewid=<? echo $row["reviewid"]; ?>" target="main">
<? If ($row["imagefilename"] != "") {?>
<img src="reviews/images/<? echo $row["reviewid"]; ?>_thumb.<? echo substr($ImageName,-3); ?>" alt="" width="35" height="35" border="1" align="left">
<? } else { ?>
<img src="images/cover_na.gif" alt="" width="35" height="35" border="0" align="left">
<? } ?>
<? echo $row["artistname"]; ?> - <? echo $row["releasename"];?> <br>
<? echo $row["labelname"]; ?></a><br>
Review Date: <? echo $row["reviewdate"]; ?>
<br><br></p>

</td>
<?
if fmod($i,2) == 0 {
echo ("</tr>") ;
}
$i ++ ;

}
mysql_free_result($result);
mysql_close($linkID);
}

?>

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
BerlinIT is offline   Reply With Quote
Old 10-19-2003, 01:00 PM   PM User | #4
BerlinIT
New Coder

 
Join Date: Jun 2003
Location: Melbourne, Australia
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
BerlinIT is an unknown quantity at this point
Yaaaay i got it to work with the following code:

$j = 0;

while ($row = mysql_fetch_assoc($result)) {
$ImageName = $row["imagefilename"];

$stype = gettype($j/2); //returns the type of the result.
if($stype == "integer"){
echo ("<tr>") ;
}
?>

<td>
<p>
<a href="reviews_main.php?reviewid=<? echo $row["reviewid"]; ?>" target="main">
<? If ($row["imagefilename"] != "") {?>
<img src="reviews/images/<? echo $row["reviewid"]; ?>_thumb.<? echo substr($ImageName,-3); ?>" alt="" width="35" height="35" border="1" align="left">
<? } else { ?>
<img src="images/cover_na.gif" alt="" width="35" height="35" border="0" align="left">
<? } ?>
<? echo $row["artistname"]; ?> - <? echo $row["releasename"];?> <br>
<? echo $row["labelname"]; ?></a><br>
Review Date: <? echo $row["reviewdate"]; ?>
<br><br></p>

</td>
<?
if($stype != "integer"){
echo ("</tr>") ;
}

$j = $j+1 ;




I guess fmod just doesnt work on my server.

thanks for the help raf.
__________________
Berlin IT Services
- Website Design & Development
- Content Management Systems

Located: Melbourne, Australia
BerlinIT is offline   Reply With Quote
Old 10-19-2003, 02:06 PM   PM User | #5
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf will become famous soon enoughraf will become famous soon enough
Hum. My bad, probably.
fmod() return the floating point mod, while i meant the modulus-operator. So the code i meant was
PHP Code:
0
while ($row mysql_fetch_assoc($result)) {
  if (
2) == {
    echo (
"<tr>") ;
  }
  echo (
"<td>") ;
  
rest of the code
  
echo ("</td>") ;
  if 
fmod(i,2) == {
    echo (
"</tr>") ;
  }
  
++

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.
raf is offline   Reply With Quote
Old 10-19-2003, 02:35 PM   PM User | #6
BerlinIT
New Coder

 
Join Date: Jun 2003
Location: Melbourne, Australia
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
BerlinIT is an unknown quantity at this point
I can see your solution is better, but whats different in the above code that would make fmod() work on my server, that wasnt there before ?

i'm not sure what you mean by 'modulus' too. me confused.

oh, and how do you paste your code with the colouring?? looks heaps better than my code pastes.
__________________
Berlin IT Services
- Website Design & Development
- Content Management Systems

Located: Melbourne, Australia
BerlinIT is offline   Reply With Quote
Old 10-19-2003, 03:34 PM   PM User | #7
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf will become famous soon enoughraf will become famous soon enough
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)
raf is offline   Reply With Quote
Old 10-20-2003, 12:21 AM   PM User | #8
BerlinIT
New Coder

 
Join Date: Jun 2003
Location: Melbourne, Australia
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
BerlinIT is an unknown quantity at this point
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
BerlinIT is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:25 AM.


Advertisement
Log in to turn off these ads.