在php

时间:2015-05-11 19:29:17

标签: php mysqli max row

所以我有一个带有mysqli回显系统的小排设置,所以我可以在我连续的小方框中粘贴我想要的信息。我想要四列,那些应该被填充(如果它们不是,它们将被填充其他盒子)。现在,当达到该限制时,我想要启动一个新行。理论上它应该很容易......但我无法弄明白。

你能吗?

                $sql = mysqli_query($con, "SELECT * FROM as_sound LEFT JOIN as_account ON as_account.id = as_sound.poster_id ");
                        $counter = 0;
                        $max = 4;

                        echo "<div class='row'>";

                            if(mysqli_num_rows($sql) > 0){
                                while (($row = mysqli_fetch_assoc($sql)) and ($counter < $max)){

                                    echo " <div class='column'>
                                                <div class='item'>
                                                    <a class='item-link' href='#'><img src='images/test.jpg' alt='header-image' width='346' ></a>
                                                    <p class='sound-title'><a class='item-link' href='#'>" . $row['name'] ."</p></a>
                                                    <table style='width:100%'>
                                                    <td><a class='item-link' href='#'><p class='sound-desc'>" . $row['short_desc'] ."</p></a></td>
                                                    <td><a class='item-link' href='#'><div class='item-avatar'><img src='" . $row['avatarimg'] . "' alt='Avatar'></a></div>
                                                    </table>
                                                    <div class='price'>$" . $row['price'] . "</div>
                                                </div>
                                            </div>"; 

                                            $counter++; }}

                    echo "</div>";

2 个答案:

答案 0 :(得分:0)

跟踪您输出的内容:

$col = 1;

while(...) {
   $col++;
   if ($col > $col_limit) {
      ... end previous column
      ... start new column
      $col = 0;
   }
   ... output column
}

答案 1 :(得分:0)

预览(模拟数据):https://jsfiddle.net/mf7qd5gn/

$sql     = mysqli_query($con, "SELECT * FROM as_sound LEFT JOIN as_account ON as_account.id = as_sound.poster_id ");
$counter = 0;
$max     = 4;

if (mysqli_num_rows($sql) > 0) {
    while ($row = mysqli_fetch_assoc($sql)) {
        if ($counter % $max == 0) {
            echo "<div class='row'>";
        }
        echo " <div class='column'>
                        <div class='item'>
                            <a class='item-link' href='#'><img src='images/test.jpg' alt='header-image' width='346' /></a>
                            <p class='sound-title'><a class='item-link' href='#'>" . $row['name'] . "</a></p>
                            <table style='width:100%'>
                            <tr>
                            <td><a class='item-link' href='#'><p class='sound-desc'>" . $row['short_desc'] . "</p></a></td>
                            <td><a class='item-link' href='#'><div class='item-avatar'><img src='" . $row['avatarimg'] . "' alt='Avatar' /></div></a></td>
                            </tr>
                            </table>
                            <div class='price'>$" . $row['price'] . "</div>
                        </div>
                    </div>";
        $counter++;
        if ($counter % $max == 0) {
            echo "</div>";
        }
    }
}