A
A
Alexey Nikolaev2015-11-09 17:46:58
PHP
Alexey Nikolaev, 2015-11-09 17:46:58

How to display blocks in a column?

Now the score goes like this
1 2 3
4 5 6
It should be like this:
1 3 5
2 4 6

<?php if ($categories) { ?>
    <div class="list">
    <?php foreach ($categories as $category) { ?>
     <ul>
     <li>             
     <a href="<?php echo $category['href']; ?>">
     <img src="<?php echo $category['thumb']; ?>" title="<?php echo $category['name']; ?>" alt="<?php echo $category['name']; ?>" />
     <span><?php echo $category['name']; ?></span>
     </a>
     </li>
    <div class="clear"></div>
    </div>
    <?php } ?>
    <?php } ?>

li tag object

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey, 2015-11-09
@megrel90

Because the algorithm is not very clear, but judging by the comments, it should be displayed in vertical blocks. I can't check, but something like this.
And try to ask the right questions.

<?php if ($categories) : ?>
    <div class="list">
    <ul><li>
    <?php 
    $cols = 2;  // число колонок
    $view = ceil( count($categories) / $cols );
    $counter = 0;
    foreach ($categories as $category) : ?>
      <div>
        <a href="<?php echo $category['href']; ?>">
          <img src="<?php echo $category['thumb']; ?>" title="<?php echo $category['name']; ?>" alt="<?php echo $category['name']; ?>" />
          <span><?php echo $category['name']; ?></span>
        </a>
      </div>
      <?php 
        $counter++;
        if( $counter == $view) { 
          echo "</li><li>";
          $counter = 0;
      ?>
    <?php endforeach; ?>
    </li></ul>
    </div>
<?php endif; ?>

The essence of the code is to create the required number of inline columns, with vertical filling with divs. And of course, it's all worth doing in CSS.

M
Maxim Nepritimov, 2015-11-09
@nepritimov_m

Fill in the first line with a cycle from 1 with a step of 2, the second - from 2 with a step of 2.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question