OK,
so I have a foreach loop in PHP.
$i = 0
$c = count($cars);
foreach ( $cars as $car ) {
// if less than 10 items
if ( $c < 10 ) {
echo '<div class="column-full-width">';
echo $i . '. ' . $car->name;
echo '</div>';
$i++;
}
// if 10 items or more
if ( $c > 10 ) {
if ( $c <= 5 ) {
echo '<div class="column-left">';
echo $i . '. ' . $car->name;
echo '</div>';
$i++;
}
if ( $c > 5 ) {
echo '<div class="column-right">';
echo $i . '. ' . $car->name;
echo '</div>';
$i++;
}
}
}
And I need to create 2 columns if the items in the array are 10 or more (if the are 9 max, 1 column is ok), however, the columns should be dynamically even in numbers of items that they contain... I mean if I have 10 items both columns will contain 5 items, if I have 11 items the left column will have 6 items and the right will have 5 items, if I will have 12 items both will have 6 items, if I will have 13 items, the left will have 7 and the right column will have 6 items.
How to do such condition in PHP?
What about this:
$c = count($cars);
$i = 0;
if ($c < 10) {
echo '<div class="column-full-width">';
foreach ($cars as $car) {
echo ($i++) . '. ' . $car->name . '<br />';
}
echo '</div>';
} else {
$lc = ceil($c / 2);
$act = reset($cars);
echo '<div class="column-left">';
while ($i < $lc) {
echo ($i++) . '. ' . $act->name . '<br />';
$act = next($cars);
}
echo '</div>';
echo '<div class="column-right">';
while ($i < $c) {
echo ($i++) . '. ' . $act->name . '<br />';
$act = next($cars);
}
echo '</div>';
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.