0

你好我有这个查询

$upit = "SELECT id from usluga";
$rezultat = mysql_query($upit);
while($red = mysql_fetch_object($rezultat))
{
    echo "<li>{$red->id}</li>";
}

我想在退出时得到这个:

<li>1,2,3,4,5,6</li>
<li>7,8,9,10,11,12</li>
<li>13,14,15,16,17,18</li>
<li>19,20,21,22,23,24</li>

等等..

有什么帮助吗?

4

2 回答 2

5

您可以使用 mod operator(%) 或使用数组函数来实现这一点

$upit = "SELECT id from usluga";
$rezultat = mysql_query($upit);

while ($red=mysql_fetch_object ($rezultat)) {
    $t[] = $red->id;
}

$chunks = array_chunk($t, 6);
foreach ($chunks as $chunk) {
    echo "<li>" . join(",", $chunk) . "</li>";
}
于 2013-04-04T10:23:59.290 回答
0

试试这个..

$upit="SELECT id from usluga";
$rezultat=mysql_query($upit);
$i=0;
$liflag=0;
while ($red=mysql_fetch_assoc($rezultat))
{
    if($i<6)
    {
        $liflag=1;
        if($i==0)
        {
            echo "<li>$red->id";
            $i++;
            continue;
        }
        else 
        {
            echo ",$red->id";
            $i++;
            continue;
        }
    }
    else
    {
        echo ",$red->id</li>";
        $i=0;
        $liflag=0;
        continue;
    }
}
if($liflag==1)
{
    echo "</li>";
}
于 2013-04-04T10:44:19.653 回答