更新:抱歉,我的问题第一次没有明确,希望这更有意义。 我有一个查询列出了所有类别,在它迭代并将其放在段落标记中时。
$data = $conn->query('SELECT * FROM category ORDER BY category_name');
foreach($data as $row) {
echo '
<p><a href="category/'.$row['category_slug'].'">'.$row['category_name'].'</a></p>';
}
如何按字母顺序将所有名称放在列中,即以A开头的列放在一列中,b放在另一列中,依此类推。类似的东西:
一个--------------乙--------ç
动作-------- ----布拉赫卡通
冒险--- ----布拉赫漫画
我可以创建一个列,然后在每个列中执行查询,但这意味着查询字母表中的每个字母。我可以用一个查询来实现这一点。
答案 0 :(得分:2)
我认为他想要的是这样的:
$wordArray = array('august', 'March', 'october', 'April', 'January', 'February', 'June', 'July');
foreach($wordArray as $word){
// Get first letter uppercased
$letter = ucfirst(substr($word, 0, 1));
$ordered[$letter][] = $word;
}
echo '<pre>';
print_r($ordered);
echo '</pre>';
输出是:
Array
(
[A] => Array
(
[0] => august
[1] => April
)
[M] => Array
(
[0] => March
)
[O] => Array
(
[0] => october
)
[J] => Array
(
[0] => January
[1] => June
[2] => July
)
[F] => Array
(
[0] => February
)
)
然后你可以构建一个像@CodeGodie这样的表,然后你会完成它。)
答案 1 :(得分:1)
当您抓取数据并显示数据时,您可能需要定义显示顺序的内容。
$query = "SELECT * FROM category ORDER BY category_name ORDER BY `category` ASC";
$row = mysql_query($query);
$row = mysql_fetch_object($row);
//Count number of pets
$count = mysql_num_rows($query);
//Figure out how many pets to put in each column
$columncount = ceil($count/3);
$lastcolumncount = $count - $columncount *2;
$middlecolumnstart = $count - $columncount - $lastcolumncount +1;
$lastcolumnstart = $count-$lastcolumncount +1;
//Put the pets into three columns
if ($row):
$count = 0;
?>
<div class="column">
<?php
foreach ($rows as $cat):
$count ++;
if($count == $middlecolumnstart) {
echo '</div><div class="column">';
}
if($count == $lastcolumnstart) {
echo '</div><div class="column last-column">';
}
?>
<a href=""><?php echo $cat->category_name; ?></a>
<?php
endforeach;
endif; ?>
</div>
<div style="clear:both;"></div>
CSS:
.column {
width:180px;
float:left;
margin-right:26px;
}
.olumn a {
display:block;
width:100%;
margin-bottom:10px;
}
.column.last-column {
margin-right:0;
}
答案 2 :(得分:0)
首先,你排序就像这样:
$array = $data->fetch_assoc();
$arr = array();
foreach ($array as $n) {
$f = substr($n, 0, 1);
$arr[$f] = array();
foreach ($array as $n2) {
$f2 = substr($n2, 0, 1);
if ($f2 === $f) {
array_push($arr[$f], $n2);
}
}
}
?>
然后您可以将其迭代到构建您的列,如下所示:
$cols = "";
foreach ($arr as $letter => $names) {
$column = "<div class='column'>";
$column .= "<h3>$letter</h3>";
foreach ($names as $name) {
$column .= "<div>$name</div>";
}
$column .= "</div>";
$cols .= $column;
}
?>
现在在 HTML :
<body>
<?= $cols ?>
</body>
<强> CSS 强>:
.column{
float:left;
padding:10px;
}
答案 3 :(得分:0)
问题不明确。
快速解决方案:
$data = $conn->query('SELECT * FROM category ORDER BY category_name');
echo '<table><tr><td>';
$curIndexLatter='';
foreach($data as $row) {
if ($curIndexLatter != '' && $curIndexLatter!=substr($row['category_name'],0,1) )
echo '</td><td>';
echo '<p><a href="category/'.$row['category_slug'].'">'.$row['category_name'].'</a></p>';
$curIndexLatter=substr($row['category_name'],0,1);
}
echo '</td</tr></table>';