Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
327 views
in Technique[技术] by (71.8m points)

php - how to display 3 column news layout

I have 11 news then i want to display like

  1. first column need only one news
  2. second column need 5 news
  3. third column need the rest of 5 news

I tried this but no luck, it shows first column and second column and the rest outside column

$rows = array(
                            'Title1',
                            'Title2',
                            'Title3',
                            'Title4',
                            'Title5',
                            'Title6',
                            'Title7',
                            'Title8',
                            'Title9',
                            'Title10',
                            'Title11',
                        );

                        $total_rows = count($rows);
                        $total_cols  = $total_rows - 1;// remove first one for the first column
                        $left_column = ceil($total_cols / 2);
                        $right_column = $total_cols - $left_column;

                        $i = 0;
echo "<div class='row'>";
                        foreach ($rows as $row) {
                            $i++;
                            if ($i == 1) {
                                $class = "primary_post";
                                echo "<div class='col-md-4 main'>";
                            } elseif ($i <= $left_column) {
                                $class = "other_post";
                                echo "<div class='col-md-4 left'>";
                            } elseif ($i == $right_column) {
                                $class = "other_post";
                                echo "<div class='col-md-4 right'>";
                            } else {
                                $class = "other_post";
                            }

                            echo "<div class='card {$class}'>$i</div>";
                            if ($i == 1 || $i == $left_column || $i == $right_column) {
                                echo "</div>";
                            } else {
                                echo "";
                            }
                        }
                        echo "</div>";

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The last echo "</div>"; supposed to be in the FOR loop not outside of it.

Also, you can split the array into chunks;

$firstColumn = array_splice($rows,0,1);
$secondColumn = array_splice($rows,0,5);

Rest are in the $rows variable.

So instead of dealing with ifs in for loops, you can use 3 different loops and list items in desired HTML objects.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...