I want to arrange fetched rows to two side. For example,
This is the rows from MySQL table
---------------------
| 1 | Apple |
---------------------
| 2 | Banana |
---------------------
| 3 | Cinnamon |
---------------------
| 4 | Donkey |
---------------------
I want to fetch it to :
<div class="col-md-6" >
<ul>
<li>Apple</li>
<li>Banana</li>
</ul>
</dil>
<div class="col-md-6" >
<ul>
<li>Cinnamon/li>
<li>Donkey</li>
</ul>
</dil>
I need to divide the rows equally to two html table columns. Can anyone help? Thank you !
How about this
$elements = <get elements from mysql server>;
$elementCount = count($elements);
echo "<div class=\"col-md-6\" >
<ul>"
for ($i = 0; $i < $elementCount/2; $i++) { // start at the beginning, go to the halfway point
echo '<li>' . $elements[$i] . '</li>';
}
echo '</div>'
echo "<div class=\"col-md-6\" >
<ul>"
for ($i = $elementCount/2; $i < $elementCount; $i++) { // start at halfway point, go to the end
echo '<li>' . $elements[$i] . '</li>';
}
echo '</div>'
This echos the list in two separate halves at different times by getting the number of elements in the list and dividing my two, then using that as the point at which to stop one list and start the other.
if $rows
- its your array of rows in php code, you can get to needed arrays this way:
$first_row = array();
$second_row = array();
foreach($rows as $num=>$row){
if($num<=count($rows)/2)$first_row[] = $row;
else{
$second_row[]=$row
}
}
Admit that it works absolutely correctly only if you have total count of $rows as pair. Another way you ll get a little difference of counts of elements in arrays $first_row
and $second_row
Code will be the next:
//$rows = ... (get your rows with sql query)
<?php
$first_row = array();
$second_row = array();
foreach($rows as $num=>$row){
if($num<=count($rows)/2)$first_row[] = $row;
else{
$second_row[]=$row
}
}
?>
<div class="col-md-6" >
<ul>
<? foreach($first_row as $f_row){ ?>
<li><?php=$f_row ?></li>
<?php } ?>
</ul>
</div>
<div class="col-md-6" >
<ul>
<? foreach($second_row as $s_row){ ?>
<li><?php=$s_row ?></li>
<?php}?>
</ul>
</div>