SitePoint Sponsor

User Tag List

Results 1 to 19 of 19
  1. #1
    SitePoint Wizard spence_noodle's Avatar
    Join Date
    Jan 2004
    Location
    uk, Leeds (area)
    Posts
    1,264
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)

    Question split a large list into two

    hi guys,

    sorry, I don't have any code as of yet, but would like to know if it's possible.

    my question is: can a large list be split into two columns by php?

    for example: a list of 50 categories then split into two colums, so theres 25 on one side and 25 on the other.

    and, how would I go about it?

    cheers,

    spence
    "Don't you just love it when you solve a programming bug only to create another."

  2. #2
    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 2;



    echo 
    "<table>\n";

    for (
    $i 0$row mysql_fetch_assoc($result); $i++) {

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

        echo 
    "  <td>" $row['fieldname'] . "</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"

  3. #3
    SitePoint Wizard spence_noodle's Avatar
    Join Date
    Jan 2004
    Location
    uk, Leeds (area)
    Posts
    1,264
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    blimey! your fast lol

    cheers clamcrusher, will do.
    "Don't you just love it when you solve a programming bug only to create another."

  4. #4
    SitePoint Wizard spence_noodle's Avatar
    Join Date
    Jan 2004
    Location
    uk, Leeds (area)
    Posts
    1,264
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    sorry to be a pain.

    But how would I use this array:

    PHP Code:
    Array
    (
        [
    0] => Array
            (
                [
    ids] => 152
                
    [names] => COMPRESSORS
                
    [pars] => 0
            
    )

        [
    1] => Array
            (
                [
    ids] => 157
                
    [names] => 300 PSI
                
    [pars] => 152
            
    )

        [
    2] => Array
            (
                [
    ids] => 173
                
    [names] => TYRES
                
    [pars] => 157
            
    )

        [
    3] => Array
            (
                [
    ids] => 187
                
    [names] => A TEST CATE
                
    [pars] => 152
            
    )

        [
    4] => Array
            (
                [
    ids] => 190
                
    [names] => ONE
                
    [pars] => 187
            
    )

        [
    5] => Array
            (
                [
    ids] => 165
                
    [names] => AIR COMPRESSORS
                
    [pars] => 152
            
    )

        [
    6] => Array
            (
                [
    ids] => 171
                
    [names] => 110V
                
    [pars] => 165
            
    )

        [
    7] => Array
            (
                [
    ids] => 169
                
    [names] => 12V
                
    [pars] => 165
            
    )

        [
    8] => Array
            (
                [
    ids] => 172
                
    [names] => 240V
                
    [pars] => 165
            
    )

        [
    9] => Array
            (
                [
    ids] => 170
                
    [names] => 24V
                
    [pars] => 165
            
    )

        [
    10] => Array
            (
                [
    ids] => 168
                
    [names] => LARGE COMPRESSORS
                
    [pars] => 165
            
    )

        [
    11] => Array
            (
                [
    ids] => 167
                
    [names] => MEDIUM COMPRESSORS
                
    [pars] => 165
            
    )

        [
    12] => Array
            (
                [
    ids] => 166
                
    [names] => SMALL COMPRESSORS
                
    [pars] => 165
            
    )

        [
    13] => Array
            (
                [
    ids] => 184
                
    [names] => ANOTHER TEST
                
    [pars] => 152
            
    )

        [
    14] => Array
            (
                [
    ids] => 191
                
    [names] => ONE
                
    [pars] => 184
            
    )

        [
    15] => Array
            (
                [
    ids] => 186
                
    [names] => CATE TEST
                
    [pars] => 152
            
    )

        [
    16] => Array
            (
                [
    ids] => 189
                
    [names] => ONE
                
    [pars] => 186
            
    )

        [
    17] => Array
            (
                [
    ids] => 188
                
    [names] => LAST CATE
                
    [pars] => 152
            
    )

        [
    18] => Array
            (
                [
    ids] => 192
                
    [names] => ONE
                
    [pars] => 188
            
    )

        [
    19] => Array
            (
                [
    ids] => 181
                
    [names] => NEXT CATEGORY
                
    [pars] => 152
            
    )

        [
    20] => Array
            (
                [
    ids] => 182
                
    [names] => NEXT CATE
                
    [pars] => 181
            
    )

        [
    21] => Array
            (
                [
    ids] => 180
                
    [names] => TEST CATEGORY
                
    [pars] => 152
            
    )

        [
    22] => Array
            (
                [
    ids] => 183
                
    [names] => TEST CATE
                
    [pars] => 180
            
    )

        [
    23] => Array
            (
                [
    ids] => 185
                
    [names] => YET ANOTHER TEST
                
    [pars] => 152
            
    )

        [
    24] => Array
            (
                [
    ids] => 193
                
    [names] => ONE
                
    [pars] => 185
            
    )

        [
    25] => Array
            (
                [
    ids] => 153
                
    [names] => MACHINERY
                
    [pars] => 0
            
    )

        [
    26] => Array
            (
                [
    ids] => 174
                
    [names] => DRILL
                
    [pars] => 153
            
    )

        [
    27] => Array
            (
                [
    ids] => 175
                
    [names] => BITS
                
    [pars] => 174
            
    )

        [
    28] => Array
            (
                [
    ids] => 177
                
    [names] => CHUCKS
                
    [pars] => 174
            
    )

        [
    29] => Array
            (
                [
    ids] => 161
                
    [names] => PNEUMATICS
                
    [pars] => 0
            
    )

        [
    30] => Array
            (
                [
    ids] => 158
                
    [names] => TOOLS
                
    [pars] => 0
            
    )


    With the above code?

    Thankx again,

    spence
    "Don't you just love it when you solve a programming bug only to create another."

  5. #5
    SitePoint Wizard spence_noodle's Avatar
    Join Date
    Jan 2004
    Location
    uk, Leeds (area)
    Posts
    1,264
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    Ok guys, I've managed to figure it out.

    But I'm having problems trying to place the first sub categories in order. Some seem to be moving over to the other column with their sub categories still in the first column.

    Click here : to view what I'm going on about.

    The code:
    PHP Code:
    <?php
            
    include("config.inc.php");
                
            
    // array
            
    $allcats = array();
            
    $cidcats = array();
            
    $parscats = array();
            
            
    // count how many rows
            
    $rowcount 0;
            
    $row_count 0;
            
            
    // how many columns
            
    $columns 2;
                
            
    // category id
            
    $cid 152;
                
            
    // Category Listings
            
    if( $cid && empty($pid) )
            {                        
                
    // retrieve the main category name and id
                
    $resultcat mysql_query"SELECT category_id, category_name, category_parent, category_des FROM gallery_category ORDER BY category_des");
                         
                while(
    $maincatn mysql_fetch_array$resultcat ))
                {                                                            
                    
    $allcats[] = array('ids' => $maincatn['category_id'], 'names' => $maincatn['category_name'], 'pars' => $maincatn['category_parent']);
                    
                    if(
    $maincatn['category_id'] == $cid)
                    {
                        
    // display the main category
                        
    echo "<table valign='top' width='100%' border='0' cellspacing='0' cellpadding='0'>
          <tr>
            <td width='11' height='33' valign='top'><img src='./images/index_07.gif' width='11' height='33' /></td>
            <td width='100%' valign='middle' bgcolor='#BFD475' class='style5'>"
    .$maincatn['category_name']."</td>
          <td width='12' valign='top'><img src='./images/index_10.gif' width='12' height='33' /></td>
          </tr>
          </table>"
    ;
                    }
                                                            
                }                                                                                                
                    
    // get each first sub category's name and id
                    
    foreach($allcats as $turnip)
                    {        
                        if(
    $turnip['pars'] == $cid)
                        {
                            
    $cidcats[] = array('names' => $turnip['names'], 'ids' => $turnip['ids']);
                        }
                        
                    }
                    
                    
    // get each second sub category's name and id
                    
    foreach($allcats as $carrot)
                    {                            
                        for(
    $c 0$c <= $cidcats[$c]; $c++)
                        {
                            if(
    $carrot['pars'] == $cidcats[$c]['ids'])
                            {
                                
    $parscats[] = array('names' => $carrot['names'], 'ids' => $carrot['ids'], 'pars' => $carrot['pars']);
                            }
                        }
                    }
                                
                    
    // display the first sub category table
                    
    echo "<table valign='top' width='100%' border='0' cellspacing='1' cellpadding='0'>\n";
                    
                    for (
    $i 0$data1 $cidcats[$i]; $i++) 
                    {                            
                                        
                            if ((
    $i $columns) == 0
                            {
                                echo 
    "<tr>\n";
                            }
                            
                                echo 
    "<td height='20' valign='middle' bgcolor='#CDCB98' class='style5'>".$data1['names']."</td>\n";
                                                                                        
                            if ((
    $i $columns) == ($columns 1)) 
                            {
                                echo 
    "</tr>\n";
                            }
                            
                            for(
    $b 0$data2 $parscats[$b]; $b++)
                            {
                                if(
    $data1['ids'] == $data2['pars'])
                                {
                                    if ((
    $b $columns) == 0
                                    {
                                        echo 
    "<tr>\n";
                                    }
                            
                                    echo 
    "<td height='20' valign='middle' bgcolor='#f5f5eb' class='style5'>".$data2['names']."</td>\n";
                                    
                                    if ((
    $b $columns) == ($columns 1)) 
                                    {
                                        echo 
    "</tr>\n";
                                    }
                                }
                            }        
                    }    

                echo 
    "</table>\n";            
        }
    ?>
    Any help with this is very much appreciated.

    Cheers,

    spence
    Last edited by spence_noodle; Dec 18, 2006 at 15:44.
    "Don't you just love it when you solve a programming bug only to create another."

  6. #6
    SitePoint Wizard spence_noodle's Avatar
    Join Date
    Jan 2004
    Location
    uk, Leeds (area)
    Posts
    1,264
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    This is the correct sequence of categories: click here

    spence
    "Don't you just love it when you solve a programming bug only to create another."

  7. #7
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Since you work with arrays, you can use array_chunk(). Though to use it, you must have a single array first. Here it goes untested, so keep in mind there might be (and surely will be) some errors:
    PHP Code:
    <?php
            
    include("config.inc.php");
                
            
    // array
            
    $allcats = array();
            
    $cidcats = array();
            
            
    // count how many rows
            
    $rowcount 0;
            
    $row_count 0;
            
            
    // how many columns
            
    $columns 2;
                
            
    // category id
            
    $cid 152;
                
            
    // Category Listings
            
    if( $cid && empty($pid) )
            {                        
                
    // retrieve the main category name and id
                
    $resultcat mysql_query"SELECT category_id, category_name, category_parent, category_des FROM gallery_category ORDER BY category_des");
                         
                while(
    $maincatn mysql_fetch_array$resultcat ))
                {                                                            
                    
    $allcats[] = array('ids' => $maincatn['category_id'], 'names' => $maincatn['category_name'], 'pars' => $maincatn['category_parent']);
                    
                    if(
    $maincatn['category_id'] == $cid)
                    {
                        
    // display the main category
                        
    echo "<table valign='top' width='100%' border='0' cellspacing='0' cellpadding='0'>
          <tr>
            <td width='11' height='33' valign='top'><img src='./images/index_07.gif' width='11' height='33' /></td>
            <td width='100%' valign='middle' bgcolor='#BFD475' class='style5'>"
    .$maincatn['category_name']."</td>
          <td width='12' valign='top'><img src='./images/index_10.gif' width='12' height='33' /></td>
          </tr>
          </table>"
    ;
                    }
                                                            
                }                                                                                                
                    
    // get each first sub category's name and id
                    
    foreach($allcats as $turnip)
                    {        
                        if(
    $turnip['pars'] == $cid)
                        {
                            
    $findrow += 1;
                            
    $cidcats[] = array('names' => $turnip['names'], 'ids' => $turnip['ids']);
                            
    // get each second sub category's name and id
                            
    foreach($allcats as $carrot)
                            {                            
                                if(
    $carrot['pars'] == $turnip['ids'])
                                {
                                    
    $cidcats[] = array('names' => $carrot['names'], 'ids' => $carrot['ids'], 'pars' => $carrot['pars']);
                                }
                            }
                        }
                        
                    }
                                
                    
    // display the first sub category table
                    
    echo "<table valign='top' width='100%' border='0' cellspacing='1' cellpadding='0'>\n";
                    
                    
    $chunks=array_chunk($cidcats,ceil(count($cidcats)/$columns));
                    
    $c1=count($chunks);
                    
    $c2=count($cidcats);
                    
                    for (
    $i 0$i $c1$i++)
                    {
                      for (
    $j 0$j $c2$j++)
                      { 
                        echo 
    "<tr>\n";
                        
    $name=isset($chunks[$i][$j]['names'])?$chunks[$i][$j]['names']:"";
                        
    $bgcolor=isset($chunks[$i][$j]['pars'])?"#f5f5eb":"#CDCB98";
                        echo 
    "<td height='20' valign='middle' bgcolor='$bgcolor' class='style5'>$name</td>\n";
                        echo 
    "</tr>\n";
                      }
                                   
                    }    

                echo 
    "</table>\n";            
        }
    ?>
    Saul

  8. #8
    SitePoint Wizard spence_noodle's Avatar
    Join Date
    Jan 2004
    Location
    uk, Leeds (area)
    Posts
    1,264
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    hey thankx php_daemon.

    Here's what the output looks like: click here

    I didn't know of array_chunks, funny though I have seen it in the list but sadly took no notice of it

    spence
    "Don't you just love it when you solve a programming bug only to create another."

  9. #9
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yes, the <tr> tags aren't in place:
    PHP Code:
                    for ($i 0$i $c1$i++)
                    {
                      echo 
    "<tr>\n";
                      for (
    $j 0$j $c2$j++)
                      { 
                        
    $name=isset($chunks[$i][$j]['names'])?$chunks[$i][$j]['names']:"";
                        
    $bgcolor=isset($chunks[$i][$j]['pars'])?"#f5f5eb":"#CDCB98";
                        echo 
    "<td height='20' valign='middle' bgcolor='$bgcolor' class='style5'>$name</td>\n";
                      }
                      echo 
    "</tr>\n";
                                   
                    } 
    Saul

  10. #10
    SitePoint Wizard spence_noodle's Avatar
    Join Date
    Jan 2004
    Location
    uk, Leeds (area)
    Posts
    1,264
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    Again thankyou for your reply php_daemon. Moving the <tr> tags didn't do the trick. If you click on the link in the above post you will now see it's all on two lines.

    If you view it you will see what I mean.

    Again thank you for your time,


    spence
    "Don't you just love it when you solve a programming bug only to create another."

  11. #11
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    lol oops I mixed up rows with columns, let's switch it:
    PHP Code:
                    for ($j 0$j $c2$j++)
                    {
                      echo 
    "<tr>\n";
                      for (
    $i 0$i $c1$i++)
                      {
                        
    $name=isset($chunks[$i][$j]['names'])?$chunks[$i][$j]['names']:"";
                        
    $bgcolor=isset($chunks[$i][$j]['pars'])?"#f5f5eb":"#CDCB98";
                        echo 
    "<td height='20' valign='middle' bgcolor='$bgcolor' class='style5'>$name</td>\n";
                      }
                      echo 
    "</tr>\n";
                                   
                    } 
    Saul

  12. #12
    SitePoint Wizard spence_noodle's Avatar
    Join Date
    Jan 2004
    Location
    uk, Leeds (area)
    Posts
    1,264
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by php_daemon View Post
    lol oops I mixed up rows with columns, let's switch it:
    PHP Code:
                    for ($j 0$j $c2$j++)
                    {
                      echo 
    "<tr>\n";
                      for (
    $i 0$i $c1$i++)
                      {
                        
    $name=isset($chunks[$i][$j]['names'])?$chunks[$i][$j]['names']:"";
                        
    $bgcolor=isset($chunks[$i][$j]['pars'])?"#f5f5eb":"#CDCB98";
                        echo 
    "<td height='20' valign='middle' bgcolor='$bgcolor' class='style5'>$name</td>\n";
                      }
                      echo 
    "</tr>\n";
                                   
                    } 
    don't worry about it mate.

    Well you'd be please to know it works. Just has a load of unused lines which is strange, but it's getting their.

    Thankx again, you've been a very big help

    spence
    "Don't you just love it when you solve a programming bug only to create another."

  13. #13
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    print_r($cidcats);

    And see what does it look like.
    Saul

  14. #14
    SitePoint Wizard spence_noodle's Avatar
    Join Date
    Jan 2004
    Location
    uk, Leeds (area)
    Posts
    1,264
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    The array looks fine, very strange.

    I've managed to get it down to just one line.

    Here's the code:

    PHP Code:
    for ($j 0$j <= $c2$j++)
                    {
                          if (
    $j == 0
                          {
                            echo 
    "<tr>\n";
                          }
                          for (
    $i 0$i $c1$i++)
                          {
                            
    $name=isset($chunks[$i][$j]['names'])?$chunks[$i][$j]['names']:"";
                            
    $bgcolor=isset($chunks[$i][$j]['pars'])?"#f5f5eb":"#CDCB98";
                            echo 
    "<td height='20' valign='middle' bgcolor='$bgcolor' class='style5'>$name</td>\n";
                          }
                          if (
    $j $c3)
                          {
                            echo 
    "</tr>\n";
                          }
                                   
                    } 
    But it still shows one line dam thing.
    "Don't you just love it when you solve a programming bug only to create another."

  15. #15
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    oh god, in my defense I'm sleepy $c2 is a size of a wrong array

    PHP Code:
    $c2=count($chunks[0]); 
    Saul

  16. #16
    SitePoint Wizard spence_noodle's Avatar
    Join Date
    Jan 2004
    Location
    uk, Leeds (area)
    Posts
    1,264
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    uploaded, seems fine.

    If I don't reply, it's because I have gone to bed.

    I blame it on old age, poor eye sight and lack of caffine.

    cya

    spence
    "Don't you just love it when you solve a programming bug only to create another."

  17. #17
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    uh oh I've edited my last post

    Quote Originally Posted by spence_noodle View Post
    uploaded, seems fine.

    If I don't reply, it's because I have gone to bed.

    I blame it on old age, poor eye sight and lack of caffine.

    cya

    spence
    same here... well, maybe except the old age and poor eye sight
    Saul

  18. #18
    SitePoint Wizard spence_noodle's Avatar
    Join Date
    Jan 2004
    Location
    uk, Leeds (area)
    Posts
    1,264
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    Well, at least one out of three is not too bad. It's when you get three out of three then you start to worry lol.

    Anyway, I still have that one liner, I'll have a quick go of to see if this idea helps or not. If you can come up with something please let us all know
    "Don't you just love it when you solve a programming bug only to create another."

  19. #19
    SitePoint Wizard spence_noodle's Avatar
    Join Date
    Jan 2004
    Location
    uk, Leeds (area)
    Posts
    1,264
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    Got it. In the end it was an equal sign that did it.

    On this line:
    for ($j = 0; $j <= $c2; $j++)

    Code:
    PHP Code:
    $chunks=array_chunk($cidcats,ceil(count($cidcats)/$columns));
                    
    $c1=count($chunks);
                    
    $c2=count($chunks[0]);
                    
                    for (
    $j 0$j $c2$j++)
                    {
                          if (
    $j == 0
                          {
                            echo 
    "<tr>\n";
                          }
                          for (
    $i 0$i $c1$i++)
                          {
                            
    $name=isset($chunks[$i][$j]['names'])?$chunks[$i][$j]['names']:"";
                            
    $bgcolor=isset($chunks[$i][$j]['pars'])?"#f5f5eb":"#CDCB98";
                            echo 
    "<td height='20' valign='middle' bgcolor='$bgcolor' class='style5'>$name</td>\n";                    
                          }
                          if (
    $j $c2)
                          {
                            echo 
    "</tr>\n";
                          }
                                   
                    } 
    My fault, sorry.

    Again php_daemon, thank you for your help

    spence
    "Don't you just love it when you solve a programming bug only to create another."


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
  •