2013-02-28 136 views
2

我有这个说法可以正常工作,但按字母顺序从左到右按3列排列。我希望它按字母顺序排列在列的下方,然后继续到下一列。任何人都知道我可以做到这一点。列出3列按字母顺序排列的mysql数据

$models = mysql_query("SELECT model, id FROM model where make='$phonemake' order by model asc") or die(mysql_error());  
$count = 0;  
$max = 3;  
while($model = mysql_fetch_array($models)) 
{  
    $count++;  
    echo "<div style='float:left; width:31%; padding-right:5px;'>".$model['model']."&nbsp;<a href='include/delete-model.php?id=".$model['id']."' onclick='return makesure".$model['id']."();'>Delete</a></div>"; 
    if($count >= $max){ 
     //reset counter 
     $count = 0; 
     //end and restart 
     echo"<div style='clear:both;'></div>"; 

回答

0

回响在一个UL列表中的数据,并使用你的柜台设置在UL列表的行数,当计数器达到最大值,创建一个新的列表,并继续显示。我个人会使用:

if($counter%3==0) { 
    create new list 
} 

这种方式你不需要每次重置计数器。

+0

谢谢v-x我试过并在按字母顺序排列之前得到了相同的结果。 – Breezy 2013-02-28 09:04:19

+0

Johny感谢您的回复,并且您会更详细地解释。谢谢 – Breezy 2013-02-28 09:04:50

0

你有两个选择:

可以缓冲导致进入阵列和更改添加的div

的顺序代码看起来是这样的:

$allmodels = array(); 
while($model = mysql_fetch_array($models)) 
{  
    $allmodels[] = $model; 
} 
for($i = 0; 3 * $i < count($allmodels); $i++) 
{ 
    for($j = 0; $j < 3; $j++) 
    { 
     if(isset($allmodels[($i * 3) + $j])) 
     { 
      $model = $allmodels[($i * 3) + $j]; 
      // print your stuff here... 
     } 
    } 
} 

或者你能以某种方式重新排序SQL查询中的项目,可能转储到临时表或使用mysql_data_seek播放。