SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do I split results into 3 columns??

    The script below works great, however it displays 396 thumbnails. What I would like to do is organize the look and feel better. I want o be able to display the thumbnails in 3 columns rather then scrolling down for ever looking at all the pictures. So how would I make it to break into 3 columns??


    PHP Code:
    <html>
    <head>
        <title>Browse</title>
        </head>
        <body>
        <h1>Browsing</h1>
        <?
        $current_url 
    "http://www.domain.com/pics/thumb/";
        
    $current_dir "/home/account/public_html/pics/thumb/";
        
    $dir opendir($current_dir);
        
    $i 0;
        
        while (
    $file readdir($dir))
        
        
        {
        
    $i++;
        
    $newFile preg_replace("/tn_/i",'',$file);
        echo 
    "<a href=http://www.domain.com/pics/$newFile><img src=$current_url$file /></a>";
        echo 
    "<br>$file<br>";
        }
        
        echo 
    "TOTAL FILES: $i";
        echo 
    "<hr><br>";
        
    closedir($dir);
        
    ?>
        </body>
        </html>

  2. #2
    SitePoint Member
    Join Date
    Feb 2006
    Location
    Miami
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can use a table...instead of echoing links just echo a new tr with 3 tds... then make ur table code outside the while loop or else u end up with many tables ^_^ but you may already know this just reminding u :P

  3. #3
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    im cutting and pasting some code, you will need to adapt it.

    PHP Code:
    // how many columns we want our table to have
    $columns 3;



    echo 
    "<table>\n";

    for (
    $i 0$file readdir($dir); $i++) {

        if ((
    $i $columns) == 0) {
            echo 
    " <tr>\n";
        }

        echo 
    "  <td>" $file "</td>\n";

        if ((
    $i $columns) == ($columns 1)) {
            echo 
    " </tr>\n";
        }

    }

    // find out how many empty <td></td> we need to output now
    // to make the html correct and well formed
    $remainder = ($columns - ($i $columns)) % $columns;

    // do we have remainders?
    if ($remainder 0) {
        echo 
    str_repeat("  <td></td>\n"$remainder);
        
    // or alternatively
        // echo "  <td colspan=\"$remainder\"></td>\n";
        
    echo " </tr>\n";
    }
    echo 
    "</table>\n"

  4. #4
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    clamcrusher, I've seen your generic code that you paste into many threads when someone asks about multiple column results -- but I'm a super-super-noob and don't understand it.

    I have a SELECT that returns exactly what I want and in hyperlink format just like I want it -- but it's a long single column. Would you please explain exactly which lines in your code need to be modified in order for me to use it and turn my SELECT into 2 or more columns?

    Here is my query:
    Code:
    SELECT `pagetitle`,CONCAT('index.php?q=', alias, '.html') as 'id' FROM `site_content` WHERE `pagetitle` LIKE 'l%' AND `published` = '1' AND `searchable` = '1' ORDER BY `pagetitle` ASC LIMIT 40;
    All I want is two columns of 20 each instead of one long 40. I've tried over a dozen 'solutions' from forums but I not sure what to change as they never work. THANKS!

  5. #5
    SitePoint Addict
    Join Date
    Jul 2005
    Posts
    215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This May Help:You can modify these Codes as you wish. I got these Code from someone a while back.

    This Displays 3 Records Per Row
    PHP Code:
    <?
    include 'library/config.php';
    include 
    'library/opendb.php';
       
    $query="SELECT partlist_id, gif_image FROM `csa_partlist` ORDER BY partlist_id";
       
    $result=mysql_query($query);
    include 
    'library/closedb.php';

    // DISPLAY RECORDS FROM THE TABLE IN ROWS (3 RECORD PER ROW)
    $i 0
    while (
    $num MySQL_Fetch_Array($result)) 
    {     
       
    $i++; 
       {
          echo 
    $num['partlist_id'];
          echo 
    "<img src='../Project/CSA_img/" .$num['gif_image']. "'>";
       }
       if ( 
    $i == ) echo "<br><br>";
    }
    ?>
    Display All The Images From The Directory In Table (3 Records Per Row):
    PHP Code:
    <?
    $read 
    '../Project/CSA_img/';    
    $myDirectory opendir($read);    
            
    echo 
    '<TABLE width=700 align=center border=1 BGCOLOR=#CCCCCC><TR>';
       
    $i 0;
       while(
    $entryName readdir($myDirectory))
       { 
          if (
    strtolower(substr($entryName, -3)) == "gif")
          { 
             
    $dirArray[]=$entryName
             
    $i++;

             echo 
    '<TD align=center valign=center>';
             echo 
    $entryName '<br>';
             echo 
    '<img src=' .$read.$entryName'>';
             echo 
    '</TD>';

             if ( 
    $i == 
             { echo 
    '</TR>'; }
          } 
       } 
    echo 
    '</TABLE>';
            
    closedir($myDirectory);
    ?>

  6. #6
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks jjdave. I just grabbed clamcrushers most recent post with his code without noticing that this was a question about looping images. Not what I need -- and too darn weary with this to repost.

    The looping code is the same -- but I don't want to lose the hyperlink format I get from my SELECT results -- and have no clue how to loop and still display that. Thanks anyway.

  7. #7
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bullrat
    All I want is two columns of 20 each instead of one long 40. I've tried over a dozen 'solutions' from forums but I not sure what to change as they never work. THANKS!

    you would just basically use this instead
    PHP Code:
    for ($i 0$row mysql_fetch_assoc($result); $i++) { 
    $reslult is the db result, as returned from mysql_query()

  8. #8
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want a fluid dynamic layout don't use tables. Instead use a list. Example! Try resizing the window!

  9. #9
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by clamcrusher
    you would just basically use this instead
    PHP Code:
    for ($i 0$row mysql_fetch_assoc($result); $i++) { 
    $reslult is the db result, as returned from mysql_query()
    I don't see another FOR statement in your original code and the new line you provided ends with "{". Forgive my stupidity (I write content -- not program) but the line of code you provided -- does it replace another line or is it inserted somewhere or what? I wasn't kidding when I said super-super-noob. This is practically all that's holding me up to launch my site and its driving me nuts. Thanks.

  10. #10
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bokehman
    If you want a fluid dynamic layout don't use tables. Instead use a list. Example! Try resizing the window!
    bokehman, I hear ya. My site is all ems and percentages so scales up nicely when IE, FF or Safari increase font size. Drove me crazy but I'm very happy for taking the pains.

    I don't have any tables but if tables allow me to have two columns.... then aaaaiiiiiiihhhhh ...I'll gladly take them. It's awful to be as ignorant as I am of coding as to not be able to simply take my SELECT statement and make that display in a two column div that I could tune with CSS -- but I don't have a clue how to do that.

  11. #11
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You still don't need tables. Just fix the width of your list to a (gasp) pixel value.

  12. #12
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Never mind, clamcrusher. I figured out how to format my SELECT results into 2 columns using CCS. Now I'll try to tackle pagination now. Oy! Thanks!

  13. #13
    SitePoint Wizard frank1's Avatar
    Join Date
    Oct 2005
    Posts
    1,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    rather than this how can be this done
    if query returned 20 rows then
    1 11
    2 12
    3 13
    ......
    10 20
    how to format in this way in table..the returned rows out of query

  14. #14
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could use a single table row with two columns. Assuming you get the result into an array, just do a for loop on 1-10 for the first cell and 11-20 for the second.

    HTML Code:
    <table>
    <tr>
    <td>col 1</td>
    <td>col 2</td>
    </tr>
    </table>

  15. #15
    SitePoint Evangelist IJoeR's Avatar
    Join Date
    Feb 2003
    Location
    Somewhere in, MD
    Posts
    400
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hey bullrat, would you mind posting the solution?


Bookmarks

Posting Permissions

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