display html li elements column-wise under their alphabets in php

让人想犯罪 __ 提交于 2020-05-23 21:15:14

问题


I have a php code as shown below which displays list of items (listings) under their alphabets column-wise (show in the fiddle below).

php code:

if ( is_array( $beta_lists ) && ! empty( $beta_lists ) ) :
    $before_title_character  = '';
    echo "<pre>"; print_r($beta_lists); echo "</pre>";   // Line A
    ?>
    <ul id="programs-list" class="programs-list js-list-active">
        <?php foreach ( $beta_lists as $title => $permalink ) :
            $character_title=substr(transliterator_transliterate('Any-Latin;Latin-ASCII;', $title),0,1);
             ?>
            <li class="shows-list__letter">
                <?php if ( $character_title !== $before_title_character ) : $before_title_character = $character_title; ?>
                    <h1 class="shows-list__letter-title"><?php echo esc_html( $character_title ) ?></h1>
                <?php  endif; ?>
                 <a class="shows-list__link" href="<?php echo esc_url( $permalink ); ?>"> <h2 class="shows-list__title"><?php echo esc_html( $title ); ?></h2>
                 </a>
            </li>
        <?php
        endforeach;
        ?>
    </ul>
<?php endif; ?>

I tried my best to replicate the DOM in the fiddle coming through php. https://jsfiddle.net/s4x1zf7L/3/ The CSS present in the fiddle is exactly what I am using currently.

Problem Statement:

Although list is displaying column-wise under their alphabets, the issue in the fiddle is that,

1. The 2nd column starts will a listing not with an alphabet. (In the fiddle, Bachelor listing starts from another column. It should be in 1st column only)

2. Adding a new list under alphabets often results another list being orphaned at the top of the 2nd column. 

I am wondering what changes I should make in the php code above so that in the fiddle 2 alphabets should be in one column and 2 alphabets in another without any list being orphaned in the 2nd column.

In case if we add 7 (odd) number of alphabets then 4 alphabets should be in left column and 3 alphabets should be in right column without any list being orphaned in the 2nd column.

(The 2nd column should always start with an alphabet with the lists below it.)

Line A in the php code above prints the following array:

Array
(
    [Apple] => http://www.abc.mno/apple/
    [Ball] => http://www.abc.mno/ball/
    [Builders] => http://www.abc.mno/builders/
    [Bowling] => http://www.abc.mno/bowling/
    [Batting] => http://www.abc.mno/batting/
    [Bone] => http://www.abc.mno/bone/
    [Bachelor] => http://www.abc.mno/bachelor/
    [Correct] => http://www.abc.mno/correct/
    [Campaign] => http://www.abc.mno/compain/
    [Direct] => http://www.abc.mno/direct/
    [Degree] => http://www.abc.mno/degree/
)

The number of elements in the array can change depending upon the values presen

This is what I have tried:

I tried avoiding orphanage of elements using css by adding margin in the 2nd column but everytime when new list gets added, I have to modify the css values which I believe is not a long term solution.

Note: The DOM is not in my control. It's only the php which is in my control.


回答1:


You need to remove the multiples <li> between the same letter elements. This is your code:

<li class="shows-list__letter">
  <h1 class="shows-list__letter-title">B</h1>
  <a class="shows-list__link" href="http://www.abc.mno/ball/">
     <h2 class="shows-list__title">Ball</h2>
  </a>
</li>
<li class="shows-list__letter">
  <a class="shows-list__link" href="http://www.abc.mno/builders/">
     <h2 class="shows-list__title">Builders</h2>
  </a>
</li>
...

This is how it should be:

<li class="shows-list__letter">
  <h1 class="shows-list__letter-title">B</h1>
  <a class="shows-list__link" href="http://www.abc.mno/ball/">
     <h2 class="shows-list__title">Ball</h2>
  </a>
  <a class="shows-list__link" href="http://www.abc.mno/builders/">
     <h2 class="shows-list__title">Builders</h2>
  </a>
  <a class="shows-list__link" href="http://www.abc.mno/bowling/">
     <h2 class="shows-list__title">Bowling</h2>
  </a>
 ....

and then add this to avoid page break between the elements:

.shows-list__letter {
    -webkit-column-break-inside: avoid;
    page-break-inside: avoid;
    break-inside: avoid;
}

Result here: https://jsfiddle.net/0ndajurv/

update

In your PHP change

<li class="shows-list__letter">
    <?php if ( $character_title !== $before_title_character ) : $before_title_character = $character_title; ?>
        <h1 class="shows-list__letter-title"><?php echo esc_html( $character_title ) ?></h1>
    <?php  endif; ?>
    <a class="shows-list__link" href="<?php echo esc_url( $permalink ); ?>">
        <h2 class="shows-list__title"><?php echo esc_html( $title ); ?></h2>
    </a>
</li>

with

<?php if ( $character_title !== $before_title_character ) : ?>
    <li class="shows-list__letter">
        <h1 class="shows-list__letter-title"><?php echo esc_html( $character_title ) ?></h1>
<?php  endif; ?>
        <a class="shows-list__link" href="<?php echo esc_url( $permalink ); ?>">
            <h2 class="shows-list__title"><?php echo esc_html( $title ); ?></h2>
        </a>
<?php if ( $character_title !== $before_title_character ) : 
        $before_title_character = $character_title; ?>
    </li>
<?php  endif; ?>


来源:https://stackoverflow.com/questions/61758018/display-html-li-elements-column-wise-under-their-alphabets-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!