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 13 of 13
  1. #1
    Regular Coder
    Join Date
    Mar 2005
    Posts
    240
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Playing with rows and columns...

    Hi there
    Have a look at the attached code. This is basically a script calling some records from a table "id" and "description". This displays the records 1 per row... How would I modify this so that it displays, say 2 or 3 per row?

    Thanks!

    Code:
      
    
    <?php
    
        $q = mysql_query("select * from table");
        $r = mysql_query($q) or die();
        while($row = mysql_fetch_assoc($r)){
            print "<a href='product.php?id=$row[id]'>$row[description]</a><br/>";
        }
        ?>
    Say i wanted to display records 3 per row instead of 1 per row, how would this code be modified?

    Thanks

  • #2
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    PHP Code:
    <?php

        $q 
    mysql_query("select * from table LIMIT 3");
        
    $r mysql_query($q) or die();
        while(
    $row mysql_fetch_assoc($r)){
            print 
    "<a href='product.php?id=$row[id]'>$row[description]</a><br/>";
        }
        
    ?>

  • #3
    New Coder
    Join Date
    Apr 2006
    Location
    Planet Earth
    Posts
    59
    Thanks
    1
    Thanked 8 Times in 8 Posts
    You want to put 3 in the same row, like this:

    Link 1 - Link 2 - Link 3

    If this is what you whan then try this:

    PHP Code:
    $q mysql_query("select * from table");
        
    $r mysql_query($q) or die();
        
    $numrows 0;
            print 
    "<table><tr>";
        while(
    $row mysql_fetch_assoc($r)){
        print 
    "<td align=\"center\"><p align=\"center\"><a href='product.php?id=$row[id]'>$row[description]</a></p>";
            
    $numrows++;
            if(
    $numrows 2) {
            echo 
    "</td></tr><tr>";
            
    $numrows 0;
                } else {
             echo 
    "</td>";
                }
            }
            echo 
    "</tr></table>"

  • #4
    Regular Coder
    Join Date
    Mar 2006
    Location
    Connecticut, USA
    Posts
    400
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Something like this would work too:

    PHP Code:

    <?php

        $q 
    mysql_query("select * from table");
        
    $r mysql_query($q) or die();
        
    $i 1;
        while(
    $row mysql_fetch_assoc($r)){
            print 
    "<a href='product.php?id=$row[id]'>$row[description]</a>";
            if( 
    $i == ) {
                print 
    "<br/>";
            }
            
    $i++;
        }
    ?>
    This is known as MODULUS. It says "if $i is divisible by 3".
    Last edited by bustamelon; 04-28-2006 at 02:51 PM.

  • #5
    Regular Coder
    Join Date
    Mar 2005
    Posts
    240
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks gang... I'll play around with the variations and see which works for me.

    I appreciate your input

  • #6
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Modulo doesn't anwser the question if x is dividable by y (directly). It gives the remainder of dividong x by y. Modulo can return values from 0 to y - 1.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #7
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    PHP Code:
    if( $i ) {
        print 
    "<br/>";

    Should be:
    PHP Code:
    if($i%3==0) {
        print 
    "<br />";

    = is assignment, and will evaulate to true.

  • #8
    Regular Coder
    Join Date
    Mar 2005
    Posts
    240
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi gang... I liked the way this one worked:

    Code:
    $q = mysql_query("select * from table"); 
        $r = mysql_query($q) or die(); 
        $numrows = 0; 
            print "<table><tr>"; 
        while($row = mysql_fetch_assoc($r)){ 
        print "<td align=\"center\"><p align=\"center\"><a href='product.php?id=$row[id]'>$row[description]</a></p>"; 
            $numrows++; 
            if($numrows > 2) { 
            echo "</td></tr><tr>"; 
            $numrows = 0; 
                } else { 
             echo "</td>"; 
                } 
            } 
            echo "</tr></table>";
    So the script that zactanaz provided allows me to put 3 of these side by side:

    +-----------------+
    | name |
    +-----------------+

    However each of the items in my table owns 2 rows of data... Something like this:

    +-----------------+
    | name |
    +-----------------+
    | description |
    +-----------------+

    So how would I input another row in there so that you have the name and description 3 times, before it dumps it onto a second line?

    Thanks

  • #9
    Regular Coder
    Join Date
    Mar 2006
    Location
    Connecticut, USA
    Posts
    400
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by GJay
    PHP Code:
    if( $i ) {
        print 
    "<br/>";

    Should be:
    PHP Code:
    if($i%3==0) {
        print 
    "<br />";

    = is assignment, and will evaulate to true.

    Good catch. It was a typo.

    Thanks for the explanation though. Duly noted and post edited.
    Last edited by bustamelon; 04-28-2006 at 02:51 PM.

  • #10
    Regular Coder
    Join Date
    Mar 2005
    Posts
    240
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for that.. I was wondering what was up with that...

    But how do I add a second row to each record so that my php script displays 2 rows times 3 columns before it drops the record onto an additional row?

    See my previous post...

    Thanks

  • #11
    Regular Coder
    Join Date
    Mar 2006
    Location
    Connecticut, USA
    Posts
    400
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Just gotta play around with the cells. Something like this perhaps (not tested):

    PHP Code:
    $q mysql_query("select * from table"); 
    $r mysql_query($q) or die(); 
    $numcols 1
    $numrows 1
    print 
    "<table><tr>"
    while(
    $row mysql_fetch_assoc($r)){ 
        print 
    "<td align=\"center\"><p align=\"center\"><a href='product.php?id=$row[id]'>$row[description]</a></p>"
        
        if(
    $numcols 2) { 
            echo 
    "</td></tr><tr>"
            
    $numcols 1;
            
    $numrows++;
        } else { 
            echo 
    "</td>"
        }
        
    $numcols++;
        
        if(
    $numrows 1) { 
            echo 
    "</tr><tr>"
            
    $numrows 1;  
        }

    echo 
    "</tr></table>"
    Edit: oops, dunno what I was thinking... the next post has it right
    Last edited by bustamelon; 04-28-2006 at 08:45 PM.

  • #12
    New Coder
    Join Date
    Apr 2006
    Location
    Planet Earth
    Posts
    59
    Thanks
    1
    Thanked 8 Times in 8 Posts
    PHP Code:
    $q mysql_query("select * from table"); 
        
    $r mysql_query($q) or die(); 
        
    $numrows 0
            print 
    "<table><tr>"
        while(
    $row mysql_fetch_assoc($r)){ 
        print 
    "<td align=\"left\">";
        print 
    "<a href='product.php?id=$row[id]'>$row[name]</a><br>";  
            print 
    "<a href='product.php?id=$row[id]'>$row[description]</a>"
            
    $numrows++; 
            if(
    $numrows 2) { 
            echo 
    "</td></tr><tr>"
            
    $numrows 0
                } else { 
             echo 
    "</td>"
                } 
            } 
            echo 
    "</tr></table>"
    This should print something like this three times in a row:

    +--------------+
    | Name
    | Description
    +--------------+

  • #13
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by GJay
    PHP Code:
    if( $i ) {
        print 
    "<br/>";

    Should be:
    PHP Code:
    if($i%3==0) {
        print 
    "<br />";

    = is assignment, and will evaulate to true.
    Actually it will give a parse error as you can't assign anything to an int (modulo always returns an int).
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.


  •  

    Posting Permissions

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