每10个mysql结果结束并启动新的UL

时间:2011-06-04 19:53:52

标签: php mysql html-lists

我想做的就是结束电流,每10个结果开始一个新的UL

继承我的代码100%无效:

    $sql1 = mysql_query("select * from `provinces` order by `name` asc");

    while ($row1 = mysql_fetch_assoc($sql1))
    {
        echo '<li id="popup_'.strtolower(str_replace(' ', '_', $row1['name'])).'">';
        echo '<ul>';

        $sql2 = mysql_query("select * from `cities` where `id_province`='".$row1['id']."' order by `name` asc");
        $count = 1;

        while ($row2 = mysql_fetch_assoc($sql2))
        {
            if ($count % 10 == 0)
                echo '</ul>';

            echo '<li>'.$row2['name'].'</li>';

            if ($count % 10 == 0)
                echo '<ul>';

            $count++;
        }

        echo '</ul>';
        echo '</li>';
    }

2 个答案:

答案 0 :(得分:5)

替换此

if ($count % 10 == 0)
       echo '</ul>';

    if ($count % 10 == 0)
            echo '</ul><ul>';

然后完全删除第二张支票

        if ($count % 10 == 0)
            echo '<ul>';

否则你的html搞砸了

//花了我一些时间来回答这个问题,因为问题不容易识别:)

答案 1 :(得分:-1)

所以它应该可以使用新的ul AFTER 10结果...如果你想要它每10个结果之前你必须在li输出之前设置if块

$sql1 = mysql_query("select * from `provinces` order by `name` asc");

    while ($row1 = mysql_fetch_assoc($sql1))
    {
        echo '<li id="popup_'.strtolower(str_replace(' ', '_', $row1['name'])).'">';
        echo '<ul>';

        $sql2 = mysql_query("select * from `cities` where `id_province`='".$row1['id']."' order by `name` asc");
        $count = 1;

        while ($row2 = mysql_fetch_assoc($sql2))
        {
            echo '<li>'.$row2['name'].'</li>';
            if ($count % 10 == 0)
                echo '</ul><ul>';
            $count++;
        }

        echo '</ul>';
        echo '</li>';
    }