我使用PHP从mySQL得到了一些输出,如下所示:
$sqlCommand = "SELECT id, linklabel FROM pages WHERE showing='1' ORDER BY id ASC";
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error());
$menuDisplay = '';
while ($row = mysqli_fetch_array($query)) {
$pid = $row["id"];
$linklabel = $row["linklabel"];
$menuDisplay .= '<li><a href="index.php?pid=' . $pid . '">' . $linklabel . '</a></li>';
}
mysqli_free_result($query);
但我想把我的输出分成三个UL,而不是全部包含在一个UL中。有什么办法吗?
澄清:我有上面代码的输出:
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
我想要这个输出:
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
如果物品数量不均衡,最后一次UL应该更短。
你的问题很模糊,据你所知,解决问题的一种方法是每次将消息拆分为三个子消息。我认为最好的选择是创建一个数组,其中每个单元格都包含一个子消息。你可能会发现这个代码很有用:
$sqlCommand = "SELECT id, linklabel FROM pages WHERE showing='1' ORDER BY id ASC";
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error());
$menuDisplay = '';
$output_count = 0 ; // count output
$output_array = array () ; //every 3 loops we'll insert the output to this array
while ($row = mysqli_fetch_array($query)) {
$output_count++ ; //update count value
$pid = $row["id"];
$linklabel = $row["linklabel"];
$menuDisplay .= '<li><a href="index.php?pid=' . $pid . '">' . $linklabel .'</a></li>';
array_push($output_array, $menuDisplay) ; // insert current message to array
}
$arrry_size = sizeof($output_array);
$count = 0 ;
$currentMessage = '' ;
for($i = 0 ; $i < $array_size ; $i++){
$count ++ ;
if($count >= $array_size / 3){
echo $currentMessage ; //you could save it in an array or do whatever you want
$currentMessage = '' ;
$count = 0;
}
if($count == $array_size-1 ) // in case we've achieved the last index of the array
echo currentMessage ;
$currentMessage.= $output_array[$i];
}
mysqli_free_result($query);