-1

I have an ordered list which is 19 entries long (but could change and be more or less). I'm listing it on a drop down menu but because of its length the column is dropping below the fold of the page.

I'd like to create a separate column (ul or div) to either divide the list into 2 or 3 equally, or have set list sizes e.g. max 7 per list.

Any ideas? Current code:

<div id="colour" class="dropmenudiv">
<?php
$sql = "select * from rug_colours where id <> 0 and active = 1 order by name";
$rs = $database->query($sql);
$index = 0;
foreach($rs as $v) {
    echo "<a href=\"//$base_url/?action=search&colour=".$v['id']."\" >".$v['name']."</a>";
}
?>

4 Answers4

1

Try something along the lines of:

    <div id="colour" class="dropmenudiv">
    <?php
    $sql = "select * from rug_colours where id <> 0 and active = 1 order by name";
    $rs = $database->query($sql);
    $column_height = 7;
    echo "<div class='column'>";
    foreach($rs as $idx => $v) {
        echo "<a href=\"//$base_url/?action=search&colour=".$v['id']."\" >".$v['name']."</a>";
        if($idx % $column_height) echo "</div><div class='column'>";
    }
    echo "</div>";
    ?>

and for equal split you might try this:

$max_column_height = 7;
$no_of_cols = ceil(count($rs) / $max_column_height);
$column_height = floor($count($rs) / $no_of_cols);
Jarek.D
  • 1,274
  • 1
  • 8
  • 18
0

You can create array of columns based on current index in foreach() loop like

$abc = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
$cols = [];
$perCol = 7;
foreach($abc as $index => $val) {
  $colInd = $index / $perCol;
  $cols[$colInd][] = $val;
}

print_r($cols);

This will split data in $abc into 3 coluns by 7 items per column.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
insider
  • 352
  • 2
  • 5
  • Passing float values as keys in deprecated as of PHP8.1. https://3v4l.org/EZjjH To split the input into rows of no more than 7 elements, `array_chunk()` would be more intuitive. – mickmackusa Jun 20 '22 at 21:04
0

You should use index variable to divide it into 2 or 3 div.

Following is example to make it in three parts:

$index = 0;
foreach($rs as $v) {
    if($index > 7){
       $index = 0; // reset to zero. You can also seperate it by any tag div or ul if you want
    }
    echo "<a href=\"//$base_url/?action=search&colour=".$v['id']."\" >".$v['name']."</a>";
 $index++;
}
Nikhil Parmar
  • 495
  • 3
  • 17
0

For an evenly spread distribution, first divide the number of elements by 7 (or whichever maximum rows you want to allow), rounding upwards. This gives the number of columns. Then divide the number of elements by the number of columns, rounding upwards: this gives you the actual number of rows you need.

I like array_chunk for this purpose:

$maxRowCount = 7;

$colCount = ceil(count($rs) / $maxRowCount);
$chunkSize = ceil(count($rs) / $colCount);

foreach(array_chunk($rs, $chunkSize) as $column) {
    echo "<div class='column'>\n";
    foreach($column as $v) {
        echo "<a href=\"//$base_url/?action=search&colour={$v['id']}\" >{$v['name']}</a>";
    }
    echo "</div>\n";
}
trincot
  • 317,000
  • 35
  • 244
  • 286