mercredi 25 novembre 2015

php query if statement within while statement

I have the following code which with the following variables set numberofColumns = 2 and numberArticles = 10, will create 2 columns for articles, the article order is from left to right (column1 to column2) going down the page.

Id just like to add some code </div><div class="whatever"> after every 2nd article.

Any help would be much appreciated.

        if ($numberColumns >= 1) {
            $columnArticles = intval(($numberArticles + $numberK2Articles) / $numberColumns);
        }

        $columns = array();
        for($columnIndex = 0; $columnIndex < $numberColumns; $columnIndex++) {
            $columns[$columnIndex] = '<div class="column col-' . ($columnIndex + 1) . '">';
        }

        $articleIndex = 0;
        while($articleIndex < count($articles)) {
            foreach ($columns as $columnIndex => $column) {
                if (isset($articles[$articleIndex])) {
                    $columns[$columnIndex] .= modCTRandomArticleHelper::getArticleHtml($params, $articles, $articleIndex);
                    $articleIndex++;
                }
            }
        }

        for($columnIndex = 0; $columnIndex < $numberColumns; $columnIndex++) {
            echo $columns[$columnIndex] . '</div>';
        }

Aucun commentaire:

Enregistrer un commentaire