PHP& MySql |如果Sql行的数量超过给定数量,则添加额外DIV

时间:2011-12-28 19:40:09

标签: php mysql count

我有以下PHP函数,它将列出MySql表中的用户,并将打印HTML代码,如您所见:

//List All Users From The Database Table In An Array
public function listUsersInArray() {
    $sql = "SELECT username FROM user";
    $users = array();
    if($stmt = $this->conn->prepare($sql)) {
        $stmt->bind_result($usrn);
        $stmt->execute();

        while ($row = $stmt->fetch()) {
            $stmt->bind_result($usrn);
            $users[] = $usrn;
        }
        $stmt->close();
        return $users;
    }
        else {
            $error = true;
            $message['error'] = true;
            $message['message'] = "The Users Could Not Stored In Array";
            return json_encode($message);       
        }
}

//Build A Form In Order To Print Out The Users For Deleting
private function createForm($id) {
    $form = array(  '<form name="delete-user" id="'.$id.'" class="delete-user" method="post" action="#">',
                        '<input type="hidden" name="user_id" value="'.$id.'" />',
                        '<fieldset class="user-wrapper">',
                            '<label for="user" class="user-label">User</label>',
                            '<input type="text" name="user" class="user" value="'.$id.'" autocomplete="off" readonly="readonly" />',
                        '</fieldset>',
                        '<fieldset class="delete-wrapper">',
                            '<button type="submit" name="delete" class="delete">Delete</button>',
                        '</fieldset>',
                    '</form>' 
            );
    $form = implode("",$form);
    return  $form;                  
}

//List All Users From Array As Forms For Deleting
public function listUsersForDelete() {
    $users = $this->listUsersInArray();     
    for ($i = 0; $i < sizeof($users) ; $i++) {
        while((sizeof($users) <= ($i % 7 == 0 || $i == 0))) {
        echo $this->createForm($users[$i]);
        $i++;
    }
    }   
}

现在我只需要弄清楚如果表单大于7,如何在div中添加包装所有表单,并在每次遇到7的倍数时执行此操作。

例如,如果我有9个表单,将第一个7包装在div中,还将其余的包装在另一个div中,如果我有16个表单,则将前7个包装在div中,将下一个7包装在div中另外两个div中的其余部分,等等......

2 个答案:

答案 0 :(得分:1)

你建议代码看起来很好。我建议的唯一一件事就是先将<form>代码存储到变量中,而不是仅复制和粘贴它。复制和粘贴容易出现很多错误,特别是如果以后必须更新代码。

答案 1 :(得分:-1)

模数可以工作但你可能想要使用一个数组来容易地将表格括在div中,如果它超过7个结果。

尝试调整此脚本,因为它会在表格网格视图中显示结果。

<?php
echo '<table>';
$number_of_thumbs_in_row = 2;
$result = mysql_query("");
$nr = mysql_num_rows($result);
if (empty($nr)) {
    $result_final = "\t<tr><td></td></tr>\n";
} else {
    while ($row = mysql_fetch_array($result)) {
            $result_array[] = "<tr>your data</tr>";        
    }
    $result_final = "<tr>\n";
    foreach ($result_array as $thumbnail_link) {
        if ($counter == $number_of_thumbs_in_row) {
            $counter = 1;
            $result_final .= "\n</tr>\n<tr>\n";
        } else
            $counter++;
        $result_final .= "\t<td>" . $thumbnail_link . "</td>\n";
    }
    if ($counter) {
        if ($number_of_photos_in_row - $counter)
            $result_final .= "\t<td colspan='" . ($number_of_photos_in_row - $counter) .
                "'>&nbsp;</td>\n";
    }
    $result_final .= "</tr>";
}
echo $result_final;
echo '</table>';
?>