Question

I have list of values that I want to fetch from my database. List could be long so I want to divide into two columns if it has over 15 items and to three columns if it has over 30. How can I break it in 3 columns. Eg..

01 | 12 | 23

02 | 13 | 24

03 | 14 | 25

04 | 15 | 26

05 | 16 | 27

06 | 17 | 28

07 | 18 | 29

08 | 19 | 30

09 | 20 | 31

10 | 21 |

11 | 22 |

For now i'm using tables and it has huge if-nest at the beginning of every loop

    $result = mysql_query("SELECT namecount FROM table WHERE name='$myvar'");

        if (!$result)
            echo 'MySQL Error: ' . mysql_error();
        else
        {
            while ($row = mysql_fetch_array($result))
            {
                $namecount= $row['namecount'];
                for($i=1;$i<=$namecount;$i++)
                {
                    if($namecount>15)
                    {
                        if($i==1)
                            echo "\n<table><tr><td width=\"200px\">";
                            
                        if($namecount%2==0)
                        {
                            if($i==$namecount/2)
                                echo "</td>\n<td>"; 
                        }
                        else
                        {
                            if($i==($sailiot+3)/2)
                                echo "</td>\n<td>";
                        }
                    }

                //Print content here
                
                if($namecount>15)
                    {
                        if($namecount%2!=0 && $i==$namecount)
                            echo "<h3>&nbsp;</h3><p>&nbsp;<br>&nbsp;</p>"; //if last item and count is odd, print empty item
                            
                        if($i==$namecount)
                            echo "\n</td></tr></table>\n";                  
                    }
                }
            }
        }

This (kinda) works with two columns, but what about three?

Was it helpful?

Solution

I would extract <tr>, </tr> out of the inner loop, this saves additional ifs. For splitting into multiple columns, you can calculate the number of items in a column and introduce a second intra column variable to keep track of:

while ($row = mysql_fetch_array($result)) {
    $namecount = $row['namecount'];

    // calculate items per column
    $columns = 1;
    if ($namecount > 15)
        $columns = 2;

    if ($namecount > 30)
        $columns = 3;

    $items = $namecount / $columns;
    if ($namecount % $columns > 0)
        ++$items;

    echo "\n<table><tr><td width=\"200px\">";
    for ($i = 1, $j = 1; $i <= $namecount; $i++, $j++) {
        if ($j > $items) {
            echo "</td>\n<td>";
            $j = 1; // reset for new column
        }

        // Print content here
    }

    if ($namecount % 2 != 0) {
         // if count is odd, print empty item
        echo "<h3>&nbsp;</h3><p>&nbsp;<br>&nbsp;</p>";
    }

    echo "\n</td></tr></table>\n";
}

OTHER TIPS

you maybe can try this

  $result = mysql_query("SELECT namecount FROM table WHERE name='$myvar'");

    if (!$result)
        echo 'MySQL Error: ' . mysql_error();
    else
    {
        while ($row = mysql_fetch_array($result))
        {
         $namecount= $row['namecount'];

      for($i=1;$i<=$namecount;$i++)
            {
              if($namecount<=15){
                echo "<table><thead><tr><th>".$your_variable_data."</th></tr></thead>";}
   else if ($namecount >15 and $namecount <=30){
     echo "<thead><tr><th>".$your_variable_data."</th></tr></thead></table>";

       }
          }
       }

its not tested but it should work for 3 columns if you want more just add else if . here a demo how it will be http://jsfiddle.net/TCJjj/107/

use css3 multi column property

<style>
.namecount {
  -moz-column-count:3; /* Firefox */
  -webkit-column-count:3; /* Safari and Chrome */
  column-count:3;
}
</style>

<div class="namecount">
Long Text
</div>

I found that CSS3 multi-columns was horribly inconsistent between browsers, and very buggy (elements with float:left, white-space:nowrap, etc. cause it to break).

So I wrote the following brute-force collator that retains key=>value pairs.

// vars

    $list = array('a','b','c','d','e','f','g','h','i','j','k','l','m',
                    'n','o','p','q','r','s','t','u','v','w','x','y','z');
    $columns = 3;

// sort it

    $count = count($list);
    $base = ceil($count/$columns);

    $keys = array_keys($list);
    for ($i=0;$i<$columns;$i++) {
        for ($j=0;$j<$base;$j++) {
            if (!empty($keys)) {
                $col[$i][] = array_shift( $keys );
            }
        }
    }

    $sorted = array();
    for ($j=0;$j<$base;$j++) {
        for ($i=0;$i<$columns;$i++) {
            if (isset($col[$i][$j])) {
                $sorted[$col[$i][$j]] = $list[$col[$i][$j]];
            }
        }
    }

// check output

    echo '<div style="float:left;margin-right:20px"><h3>ORIGINAL</h3>';
    foreach ($list as $k=>$v) echo $k .' = '. $v.'<br>';
    echo '</div>';

    echo '<div style="float:left"><h3>SORTED</h3>';
    foreach ($sorted as $k=>$v) echo $k .' = '. $v .'<br>';
    echo '</div>';
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top