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 10 of 10
  1. #1
    Regular Coder levani's Avatar
    Join Date
    Mar 2009
    Location
    Georgia
    Posts
    183
    Thanks
    29
    Thanked 1 Time in 1 Post

    Foreach items and list in several columns

    I'm trying to foreach items got from database in three columns but without any success... How can I do something like this:

    Item 1________Item 5________Item 9
    Item 2________Item 6________Item 10
    Item 3________Item 7________Item 11
    Item 4________Item 8________Item 12


    Can anyone please help?

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    can you post your code?

  • #3
    Regular Coder levani's Avatar
    Join Date
    Mar 2009
    Location
    Georgia
    Posts
    183
    Thanks
    29
    Thanked 1 Time in 1 Post
    Here it is:

    PHP Code:
    <?php

    $items 
    $wpdb->get_results("SELECT * FROM wp_mwarm"); 
    foreach (
    $items as $list) {

        echo 
    '<li><a href="?id_type=' $list->ID '">' $list->name '</a>';

    }

    ?>

  • #4
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    are there only three records being called, if so you could do it like this;
    PHP Code:
    <?php
    echo "<table><tr>";
    $items $wpdb->get_results("SELECT * FROM wp_mwarm"); 
    foreach (
    $items as $list) {

        echo 
    '<td><a href="?id_type=' $list->ID '">' $list->name '</a></td>';

    }
    echo 
    "</tr></table>";
    ?>
    or are they're several records and you want several rows with 3 columns per row?

  • #5
    Regular Coder levani's Avatar
    Join Date
    Mar 2009
    Location
    Georgia
    Posts
    183
    Thanks
    29
    Thanked 1 Time in 1 Post
    The records are retrieved from database and I don't know the number in advance as they are changed dynamically. I want to list them in thee columns. For example if there are total 30 records they should be divided into three parts. The first 10 records goes to first column, records from 10 to 20 into second column and so on...

    Here I'll post an image:



    Assuming the records are the car models...

  • #6
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    something like this should work ( untested, might be a bit of tweaking )

    PHP Code:
    $result mysql_query("SELECT * FROM wp_mwarm"); 
    $NumberOfResults mysql_num_rows($result);
    $RecordsPerCol   round$NumberOfResults 3);

    $Counter 0;
    echo 
    "<table border='1'><tr>";

    while(
    $row mysql_fetch_array($result)){

        
    $Counter++;
        if(
    $Counter == 1){
            echo 
    "<td>";
        }

        echo 
    $row['name'] . "<br />";

        if(
    $Counter == $RecordsPerCol){
            echo 
    "</td>";
            
    $Counter 0;
        }
    }
    echo 
    "</tr></table>"

  • Users who have thanked angst for this post:

    levani (03-29-2010)

  • #7
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,865
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    It'd better for you to consider as three unordered list. Find the total number of records and then divide it by 3 to get the number of items to be added to each list. Then use 3 seperate loops to echo the list items under each list.
    After that you may set float:left;width:33%; to the list to align them side by side.

    @angst: The data doesn't look like tabular, and hence using a table wouldn't be semantic.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #8
    Regular Coder levani's Avatar
    Join Date
    Mar 2009
    Location
    Georgia
    Posts
    183
    Thanks
    29
    Thanked 1 Time in 1 Post
    PHP Code:
    $result mysql_query("SELECT * FROM wp_mwarm"); 
    $NumberOfResults mysql_num_rows($result);
    $RecordsPerCol   round$NumberOfResults 3);

    $Counter 0;
    echo 
    "<table border='1'><tr>";

    while(
    $row mysql_fetch_array($result)){

        
    $Counter++;
        if(
    $Counter == 1){
            echo 
    "<td>";
        }

        echo 
    $row['name'] . "<br />";

        if(
    $Counter == $RecordsPerCol){
            echo 
    "</td>";
            
    $Counter 0;
        }
    }
    echo 
    "</tr></table>"
    This code works fine. Thanks!!!

  • #9
    Regular Coder sitNsmile's Avatar
    Join Date
    Dec 2009
    Location
    Charlotte, NC
    Posts
    358
    Thanks
    19
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by angst View Post
    something like this should work ( untested, might be a bit of tweaking )

    PHP Code:
    $result mysql_query("SELECT * FROM wp_mwarm"); 
    $NumberOfResults mysql_num_rows($result);
    $RecordsPerCol   round$NumberOfResults 3);

    $Counter 0;
    echo 
    "<table border='1'><tr>";

    while(
    $row mysql_fetch_array($result)){

        
    $Counter++;
        if(
    $Counter == 1){
            echo 
    "<td>";
        }

        echo 
    $row['name'] . "<br />";

        if(
    $Counter == $RecordsPerCol){
            echo 
    "</td>";
            
    $Counter 0;
        }
    }
    echo 
    "</tr></table>"
    by doing this way, you've just ruined the function of OO style coding.

  • #10
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    Quote Originally Posted by sitNsmile View Post
    by doing this way, you've just ruined the function of OO style coding.
    excuse me? I offered an example of how it could be done. it wasn't my intention to write the code for him exactly how they needed the end result to be.

    your more then welcome to write it out again and save your rude comments for someone else.


  •  

    Tags for this Thread

    Posting Permissions

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