SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Evangelist IJoeR's Avatar
    Join Date
    Feb 2003
    Location
    Somewhere in, MD
    Posts
    400
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    3 column layout - expandable

    maybe someone can help me get my head around this little question i have.

    I have a website that has a 3 column layout. I have the homepage coded and working properly. The homepage has 3 columns, column 1 has 19 links in it, column 2 has another 19 links in it, and column 3 has 18 links in it.

    All of these links are contained in a mySQL database.

    I have code like $a==18 then close this div and open a new div. ($a refers to the record #.) So column 1 displays 0-17, column 2 displays 18-35, and column3 displays 36 til whenever results stop. I'm trying to have column1 and 2 be even and column3 be the short column.

    So i have an interior page that shortens the list based on what you section you go into, so there might 10 links displayed or there might 20 or only 1 link displayed.

    What i want to do is this...Let's say there are 3 results, the result should be 3 columns. 1 link in each column.

    Let's say there are 6 results, 2 in each column and the lists have to be in alphabetical order vertically.

    Let's say there are 18 results, 6 links in each column, alphabetical order vertically.

    Anyone got any suggestions on how to do that? I don;t want to hard code all of these different pages. I'm php5.

    Thanks in advance!
    Rob

  2. #2
    SitePoint Evangelist catweasel's Avatar
    Join Date
    Apr 2007
    Location
    Goldfields, VIC, Australia
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by IJoeR View Post
    Let's say there are 6 results, 2 in each column and the lists have to be in alphabetical order vertically.
    A little bit similar to a question yesterday regarding table columns generated dynamically.. except you want to sort these columns vertically.

    Divide the number of results you have with the number of columns you want then use array_chunk to split the result into their columns. Use css to layout the columns.
    PHP Code:
    <?php
    DEFINE
    ("NUMCOLS",3);
    DEFINE("NUMRESULTS",91);

    $testArray = array();
    for (
    $i=0;$i<NUMRESULTS;$i++) {
        
    $testArray[$i] = "Item ".($i+1);
    }

    $colLength ceil(NUMRESULTS/NUMCOLS);
    $testArray array_chunk($testArray,$colLength,true);
    foreach (
    $testArray as $column) {
        
    $new_array[] = sprintf("<div class='column'>%s</div>\n",implode("<br>\n",$column));
    }
    ?>

    <html>
    <head>
    <style type='text/css'>
        .column {
            width:auto;
            float:left;
            padding:5px;
            margin:5px;
            background-color:#ccc;
        }
    </style>
    </head>
    <body>
    <?php
    echo implode('',$new_array);
    ?>
    <div style='clear:both;'></div>
    </body>

  3. #3
    SitePoint Evangelist IJoeR's Avatar
    Join Date
    Feb 2003
    Location
    Somewhere in, MD
    Posts
    400
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cool, i got it to make the 3 columns. It put the word "item ", like 1 to 6. How can i throw a variable in there to pull categories from my database?

  4. #4
    SitePoint Evangelist catweasel's Avatar
    Join Date
    Apr 2007
    Location
    Goldfields, VIC, Australia
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by IJoeR View Post
    cool, i got it to make the 3 columns. It put the word "item ", like 1 to 6. How can i throw a variable in there to pull categories from my database?
    this bit -
    PHP Code:
    $testArray = array(); 
    for (
    $i=0;$i<NUMRESULTS;$i++) { 
        
    $testArray[$i] = "Item ".($i+1); 

    was just to set up an example array.. you'd replace that with your loop through the database result making an array with your database values instead of item 1,item 2 ..

  5. #5
    SitePoint Evangelist IJoeR's Avatar
    Join Date
    Feb 2003
    Location
    Somewhere in, MD
    Posts
    400
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    its been a little while since i programmed in php, so I'm looking at some other sites to try to figure that out -- how to add my fields in the array...maybe i been staring at the computer too long.

  6. #6
    SitePoint Evangelist IJoeR's Avatar
    Join Date
    Feb 2003
    Location
    Somewhere in, MD
    Posts
    400
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation what am i doing wrong here?

    PHP Code:
    $sql mysql_query("SELECT * FROM tbl_categories WHERE locationType = '$mylocType' ORDER BY categories");
     
    // count the rows
    $num_rows mysql_num_rows($sql);
    // define the number of columns and number of total results
    DEFINE("NUMCOLS",3); 
    DEFINE("NUMRESULTS""$num_rows");
    while (
    $generic mysql_fetch_array($sql)) {
       
    $Vcat $generic["category"];
     
      for (
    $i=0;$i<NUMRESULTS;$i++) { 
      
    $testArray = Array("$Vcat");
      
    $colLength ceil(NUMRESULTS/NUMCOLS); 
      
    $testArray array_chunk($testArray,$colLength,true);
       }
      
       foreach (
    $testArray as $column) { 
          
    $new_array[] = sprintf("<div class='column'>%s</div>\n",implode("<br>\n",$column)); 
       }
     } 
    The output of the code shows all of my categories, but they aren't listed vertically. They go across the screen horizontally.

    I'm just wondering what am i doing wrong here?

  7. #7
    SitePoint Evangelist catweasel's Avatar
    Join Date
    Apr 2007
    Location
    Goldfields, VIC, Australia
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    EDIT: actually you don't need so many loops-
    PHP Code:
    $sql mysql_query("SELECT * FROM tbl_categories WHERE locationType = '$mylocType' ORDER BY categories");

    DEFINE("NUMCOLS",3); 
    DEFINE("NUMRESULTS"mysql_num_rows($sql));


    while (
    $generic mysql_fetch_array($sql)) {
        
    $Vcat $generic["category"];
        
    $testArray[] = $Vcat;
    }

    colLength ceil(NUMRESULTS/NUMCOLS); 
    $testArray array_chunk($testArray,$colLength,true);

    foreach (
    $testArray as $column) { 
        
    $new_array[] = sprintf("<div class='column'>%s</div>\n",implode("<br>\n",$column)); 


  8. #8
    SitePoint Evangelist IJoeR's Avatar
    Join Date
    Feb 2003
    Location
    Somewhere in, MD
    Posts
    400
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah some of the results of different attempts on my page were pretty horrifying!

    I'm going to test this out! Thanks for all of your help!

    you d'man!

    BTW, how long have you been a php programmer?

  9. #9
    SitePoint Evangelist IJoeR's Avatar
    Join Date
    Feb 2003
    Location
    Somewhere in, MD
    Posts
    400
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here's a dumb question about the code.

    How do you know what the value of &#37;s is going to be? I think thats a special variable that works with sprintf right? I mean how do you know which %letter or number to use?

    Say i had more then the categories to out, say i had a thumbnail to go with them, how could that work?

    Thanks! Above code worked great!


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
  •