按字母将MySQL数据划分为四个相等的引导程序行顺序

我有一个类别表,我想将类别数据动态显示为4个相等的自举4行,并且数据必须按字母顺序显示。


首先,我计算数据的数量并除以4,然后运行4个查询以找到前25个数据,并在第二个查询中查找下一个25个数据,依此类推,但是正在寻找更好的解决方案。


$catCount = Category::all()->count();


$inOneRow = intval( $catCount / 4);

我想要的是

http://img2.mukewang.com/60b09d090001956910170532.jpg

大话西游666
浏览 147回答 1
1回答

饮歌长啸

假设类别是类别名称的表-您可以一次选择所有类别,然后在查询中使用ORDER BY子句:$query = 'SELECT name FROM db.categories ORDER BY name ASC;';您也可以在结果集上使用sort:$query = 'SELECT name FROM db.categories;';/* execute query here... */sort($resultSet);假设您这样做了,那么使用引导网格就可以轻松进行样式设置(注意小提琴中的模拟结果集):<?phpconst NUM_COLS = 4;$numResults = count($dummyResult);$numPerCol = round($numResults / NUM_COLS);?><div class="container">&nbsp; &nbsp; <div class="row">&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for($i = 0, $col = 0; $col < NUM_COLS; $col++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<div class="col">';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for($colLimit = min($numResults, $i + $numPerCol); $i < $colLimit; $i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<div class="row">'.$dummyResult[$i]['category'].'</div>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '</div>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; </div></div>更容易理解,尽管示例可能更慢:<?phpconst NUM_COLS = 4;$numResults = count($dummyResult);$numPerCol = round($numResults / NUM_COLS);?><div class="container">&nbsp; &nbsp; <div class="row">&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for($i = 0; $i < NUM_COLS; $i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<div class="col">';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach( array_splice($dummyResult, 0, $numPerCol) as $row ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<div class="col">'.$row['category'].'</div>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '</div>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; </div></div>注意:除非另作样式,否则容器将是其父容器的100%宽度。默认情况下,列宽将相等。请参阅Bootstrap网格。
打开App,查看更多内容
随时随地看视频慕课网APP