Thread: Results per row

    #1
  1. No Profile Picture
    The Dude Abides
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2000
    Location
    grass valley,ca
    Posts
    1,062
    Rep Power
    16
    I'm trying to get 4 results per row, and then start a new row. I searched this forum and came up with several code snippets, but none are working.
    Here is the closest:

    print "<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0><TR>n";
    $loop=0;
    while ($data = mysql_fetch_array($result))
    {
    // print and loop


    print "<td>n";
    print "<A HREF="".$data["imglink"]."">n";
    print "<IMG SRC="".$data["thimgpath"]."" border=0></a>n";
    print "</td>n";
    if (++$loop4==4)
    print "</tr><TR>n";
    }
    print "</tr>n";

    It works for the first row of results, but then stops after the second. It then just prints all the results on the second line, the html code shows it stops the </TR>.

    I've tried variations on the loop
    if (++$loop%4==4)
    if (is_int($loop/4)) {
    but none are working.

    Kind of a crunch situation, so help is greatly appreciated!!

    thanx
    chris
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2000
    Location
    Ontario, Canada
    Posts
    498
    Rep Power
    18
    Try using a for() loop:

    for($x=0;$x<4;$x++) {
    $row = mysql_fetch_array($result);
    ...
    }


    ------------------
    To alcohol! The cause of, and solution to, all of life's problems. -- Homer Simpson

    [This message has been edited by cka (edited September 19, 2000).]
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    Perth West Australia
    Posts
    757
    Rep Power
    15
    you can use a nested loop...
    <? $top=40;?>
    <table>
    <? for($y=0;$y<=40;$y++){?>
    <tr>
    <? for($x=0;$x<4;$x++){$y++;?>
    <? if($y<=$top){ echo "<td>$y</td>";}?>
    <? } ?>
    </tr>
    <? }?>
    </table>

    or use % - for a single loop

    <table><tr>
    <? for($y=1;$y<=43;$y++){
    echo "<td>$y</td>";
    if($y%4==0){echo "</tr><tr>";}
    }?>
    </tr></table>

    the second way is better but the first has its uses. - of course $y would be $yourow[$y] or whatever

    ------------------
    Simon Wheeler
    FirePages -DHTML/PHP/MySQL
  6. #4
  7. No Profile Picture
    The Dude Abides
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2000
    Location
    grass valley,ca
    Posts
    1,062
    Rep Power
    16
    Simon,
    That has it the closest. Now the only problem I'm having is the results are printing the same result all 20 times on the first page and the next result 20 times on the second page.....

    It's actually printing 4 rows with 5 results per row, but right now that is just fine ; )

    You'll see two select statements, the first is for the next links.

    I've been staring at this for too long and it just ain't registering. thanks for the help

    <?
    // Start of Prev 123 Next code

    $limit=20; // rows to return

    $numresults=mysql_query("SELECT * ".
    "FROM mydb WHERE table LIKE '$variable%' ");
    $numrows=mysql_num_rows($numresults);

    // next determine if offset has been passed to script, if not use 0

    if (empty($offset))
    {
    $offset=0;
    }

    // get results

    $result=mysql_query("select * ".
    "FROM db WHERE table LIKE '$variable' ".
    "limit $offset,$limit ");


    //BEGIN PICTURE DISPLAY

    print "<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0>n";
    ?>

    <tr>
    <?
    {
    $data = mysql_fetch_array($result);
    {
    for($y=1;$y<=20;$y++){
    echo "<A HREF="".$data["imglink"]."">n";
    echo "<IMG SRC="".$data["thimgpath"]."" border=0></a>n";
    if($y%4==0){
    echo "</tr><tr>";}
    }?>
    </tr></table>

    [This message has been edited by thedude (edited September 20, 2000).]
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2000
    Posts
    669
    Rep Power
    15
    Here is the solution to your dilenma:
    $itemsperrow = 4;
    $i = 1;
    print '<table><tr>';
    while ($row = mysql_fetch_array($result)) {
    if (is_int($i / $itemsperrow)) {
    print '<td>your data....</td></tr><tr>';
    }
    else {
    print '<td>your data....</td>';
    }
    $i++;
    }
    print '</tr></table>';
  10. #6
  11. No Profile Picture
    The Dude Abides
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2000
    Location
    grass valley,ca
    Posts
    1,062
    Rep Power
    16
    mmmm can't believe I missed that one.
    thanks it worked like a charm

Similar Threads

  1. div style="overflow: scroll;"
    By slydero in forum HTML Programming
    Replies: 8
    Last Post: October 26th, 2004, 09:54 AM
  2. absolutely perplexed about missing row in results!
    By ralphuk100 in forum MySQL Help
    Replies: 5
    Last Post: March 2nd, 2004, 11:32 AM
  3. Replies: 4
    Last Post: October 31st, 2003, 03:20 AM
  4. <THEAD> not printing across multiple pages
    By allanmcc in forum HTML Programming
    Replies: 2
    Last Post: September 29th, 2003, 08:47 PM

IMN logo majestic logo threadwatch logo seochat tools logo