Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Jun 2003
    Location
    Melbourne, Australia
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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>") ;
      }
      
    ++


  • #3
    New Coder
    Join Date
    Jun 2003
    Location
    Melbourne, Australia
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    New Coder
    Join Date
    Jun 2003
    Location
    Melbourne, Australia
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #5
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #6
    New Coder
    Join Date
    Jun 2003
    Location
    Melbourne, Australia
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #7
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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)

  • #8
    New Coder
    Join Date
    Jun 2003
    Location
    Melbourne, Australia
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •