There is a 700px wide table: 5 rows, 10 cells (two columns of 5 news items each). If the news in the database is less than 5 or 5, then we fill in the news only the left column, if more, for example, 6, then the right column begins to fill. How to implement it? Pictures for clarity

5 news

alt text

6 news

alt text

7 news

alt text

  • List item

    4 answers 4

    I understand that you need to unravel the news inside the table - <table>

    $news = array( 'Новость 1', 'Новость 2', 'Новость 3', 'Новость 4', 'Новость 5', 'Новость 6', 'Новость 7', 'Новость 8', 'Новость 9', 'Новость 10', 'Новость 11' ); $newsArr = array_chunk(array_reverse($news),5); $tbl = '<table border="1"><tr>'; for($i = 0; $i < 5; $i++){ for($k = 0; $k < count($newsArr); $k++){ $tbl .= '<td>'.$newsArr[$k][$i].'</td>'; } $tbl .= '</tr><tr>'; } $tbl .= '</tr></table>'; echo $tbl; 

    PS Function array_reverse () to you, I think that is not needed, because you pull out records from a DB already with DESC sorting.

    And I forgot to add that for the purity of the process, you can “align” the size of the array, where the elements will be less than five, but this is up to you. In general, in the end, we get here such cakes with kittens . (clicking on the link, press F9 )

      We receive an array of news, depending on the number of render partial

      • foreach ($ reversed_news_list as $ news) {if (counter <5) {left_block; ... if there is more news, then left_block it will not fill up - vinnie
      • not much is wrong, we transfer an array from the controller, in the view we look at the number and render the template depending on the number, and in the templates only the markup with the cycle is pavelbel

      In css3 there is a nice property: column-count . But for browsers that do not support this property, you will have to write a compatibility script.

        There are a couple of options in memory:

        1. Give up the table in the layout and do something like this:

         <div class='container'> <div class='left'> <div>новость 6</div> <div>новость 5</div> <div>новость 4</div> <div>новость 3</div> <div>новость 2</div> </div> <div class='right'> <div>новость 1</div> </div> </div> 

        and fill accordingly approximately as follows:

         $counter=0; foreach($reversed_news_list as $news){ if (counter<5){ left_block;... }else{ right_block;.. } } 

        2. Beforehand, break the list of messages into 2 parts (left and right column):
        and also output through foreach

        • And then how?)) - vinnie