html - display li items column-wise under title characters in php

I have a php code as shown below in which through Line B, I have counted the title characters used while grouping array results in alphabetical order. Line A prints 4. DEMO
php code:
<?php
$beta_lists = array (
'ГЂpple' => 'http://www.abc.mno/apple/',
'Ball' => 'http://www.abc.mno/ball/',
'Builders' => 'http://www.abc.mno/builders/',
'Bowling' => 'http://www.abc.mno/bowling/',
'Correct' => 'http://www.abc.mno/correct/',
'Campaign' => 'http://www.abc.mno/compain/',
'Direct' => 'http://www.abc.mno/direct/',
'Degree' => 'http://www.abc.mno/degree/'
);
ksort($beta_lists);
$groups = array();
foreach ($beta_lists as $title => $value) {
$groups[remove_accents(mb_substr($title, 0, 1))][$title] = $value;
}
ksort($groups);
print_r($groups);
echo count($groups); // Line A
if(count($groups)%2==0) {
} else {
}
Problem Statement:
I am wondering what changes I should make in the php code above so that it prints the list items column-wise (in 2 columns as shown below) under title characters.
The 2nd column should always start with a title character not with a list item. For even numbers of title characters, 50% of the title characters with their list items should be on the left hand side and 50% of the title characters with their list items should be on the right hand side.
In case if we have odd number of title characters (let's say 7) then 4 title characters should be on the 1st column and 3 title characters should be on 2nd column without any list being orphaned in the 2nd column.
A C
Apple Correct
B Compaingn
Ball D
Builders Direct
Bowling Degree
This is what I have tried:
The following html code needs to be integrated with the php code above for the lists to appear column-wise under title characters.
html code:
<ul class="shows-list">
<li class="shows-list__letter">
<h1 class="shows-list__letter-title"><?php echo esc_html( $title_character ) ?></h1> <!-- For title character -->
<a class="shows-list__link" href="<?php echo esc_url( $permalink ); ?>"><h2 class="shows-list__title"><?php echo esc_html( $title ); ?></h2></a>
</li>
</ul>
Here $title is Apple, Ball, Builders, Bowling, Correct, Compaign, Direct and Degree.
Answer
Solution:
One possible solution would be extracting left and right columns into the separate arrays.
PHP:
<?php
$beta_lists = [
'Apple' => 'http://www.abc.mno/apple/',
'Ball' => 'http://www.abc.mno/ball/',
'Builders' => 'http://www.abc.mno/builders/',
'Bowling' => 'http://www.abc.mno/bowling/',
'Correct' => 'http://www.abc.mno/correct/',
'Campaign' => 'http://www.abc.mno/compain/',
'Direct' => 'http://www.abc.mno/direct/',
'Degree' => 'http://www.abc.mno/degree/',
//'Example' => 'http://www.abc.mno/example/',
];
ksort($beta_lists);
$groups = [];
foreach ($beta_lists as $title => $value) {
$groups[remove_accents(mb_substr($title, 0, 1))][$title] = $value;
}
ksort($groups);
$left = array_slice($groups, 0, ceil(count($groups) / 2));
$right = array_slice($groups, count($left));
HTML:
<ul class="shows-list">
<?php foreach ($left as $title_character => $meta): ?>
<li class="shows-list__letter">
<h1 class="shows-list__letter-title"><?= esc_html( $title_character ) ?></h1> <!-- For title character -->
<?php foreach ($meta as $title => $permalink): ?>
<a class="shows-list__link" href="<?= esc_url( $permalink ); ?>"><h2 class="shows-list__title"><?= esc_html( $title ); ?></h2></a>
<?php endforeach; ?>
</li>
<?php endforeach; ?>
</ul>
And print the right column this way too.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: method illuminate\database\eloquent\collection::paginate does not exist.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.