【发布时间】:2011-06-15 19:33:27
【问题描述】:
我是 4 个月的 php 新手(所以要温柔),我正在尝试确定表格中有多少行(人)来填充我的页面(同时满足条件)。我认为我在正确的轨道上,但可能有更好的方法来做我正在做的事情,因为我还是个新手......
我正在使用 $myid 作为一条记录的查询条件来查询我的信息。我用它来“检索”和“分解”一个数组(实际上是 4 个单独的数组 - 朋友、被阻止、感兴趣、被丢弃)。这个没问题...
然后我执行与上面相同的查询,但没有 $myid 作为条件(获取其他数据)
这是我认为我丢球的地方: 如果其他数据不在我的 4 个数组中的任何一个中,我试图仅从第二个查询运行一个 while 循环......同时计算 $nr(行数)。
[ while($row = mysql_fetch_array($sql)) {
$mem2 = $row['id'];
if (!in_array($mem2, $mydiscard_array)) {
if (!in_array($mem2, $myblocked_array)) {
if (!in_array($mem2, $myinterested_array)) {
if (!in_array($mem2, $myfriend_array)) {
$nr = $nr + 1; // this only adds if !in_arrays ////
} // end of $discard_array condition
} // end of $blocked_array condition
} // end of $interested_array condition
} // end of $friend_array condition
} //// close while loop
$variableA= '';
$variableB= '';
$variableC= '';
if ($nr == 0) { // zero out all php variable to ""; and skip processing others data
} else { /// run the pagination code here
/// run the populate other people's data code here
} // place this below populated data and output ]
/////////// 然后我做分页代码:(我将从上面的 else 命令开始) ////////////
[ } else {
if (isset($_GET['pn'])) { // Get pn from URL vars if it is present
$pn = preg_replace('#[^0-9]#i', '', $_GET['pn']); // filter everything but numbers for security(new)
} else { // If the pn URL variable is not present force it to be value of page number 1
$pn = 1;
}
//This is where we set how many database items to show on each page
$itemsPerPage = 5;
// Get the value of the last page in the pagination result set
$lastPage = ceil($nr / $itemsPerPage);
// Be sure URL variable $pn(page number) is no lower than page 1 and no higher than $lastpage
if ($pn < 1) { // If it is less than 1
$pn = 1; // force if to be 1
} else if ($pn > $lastPage) { // if it is greater than $lastpage
$pn = $lastPage; // force it to be $lastpage's value
}
// This creates the numbers to click in between the next and back buttons
$centerPages = ""; // Initialize this variable
$sub1 = $pn - 1;
$sub2 = $pn - 2;
$add1 = $pn + 1;
$add2 = $pn + 2;
if ($pn == 1) {
$centerPages .= ' <span class="pagNumActive">' . $pn . '</span> ';
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> ';
} else if ($pn == $lastPage) {
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> ';
$centerPages .= ' <span class="pagNumActive">' . $pn . '</span> ';
} else if ($pn > 2 && $pn < ($lastPage - 1)) {
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub2 . '">' . $sub2 . '</a> ';
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> ';
$centerPages .= ' <span class="pagNumActive">' . $pn . '</span> ';
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> ';
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add2 . '">' . $add2 . '</a> ';
} else if ($pn > 1 && $pn < $lastPage) {
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> ';
$centerPages .= ' <span class="pagNumActive">' . $pn . '</span> ';
$centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> ';
}
// 此行设置“LIMIT”范围...我们放置的 2 个值用于在查询中从数据库中选择一系列行((现在设置为每页显示 5 个)))) $limit = 'LIMIT' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage;
// 现在我们将运行与上面相同的查询,但这次将 $limit 添加到 SQL 语法的末尾
// $sql2 将用于为下面的 while 循环语句提供动力
$sql2 = mysql_query("the query goes here and added $limit ");
////////////////////////////////////////////////////////////////////////////// / ///////////////////////////////////////////////////////////////////////////
$paginationDisplay = "";
//初始化分页输出变量 // 此代码仅在最后一页变量不等于 1 时运行,如果只有 1 页,我们不需要显示分页链接
if (($lastPage != "1")||($lastPage != "0")){
// This shows the user what page they are on, and the total number of pages
$paginationDisplay .= 'Page <strong>' . $pn . '</strong> of ' . $lastPage. ';
// If we are not on page 1 we can place the Back button
if ($pn != 1) {
$previous = $pn - 1;
$paginationDisplay .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $previous . '"> Back</a> ';
}
// Lay in the clickable numbers display here between the Back and Next links
$paginationDisplay .= '<span class="paginationNumbers">' . $centerPages . '</span>';
// If we are not on the very last page we can place the Next button
if ($pn != $lastPage) {
$nextPage = $pn + 1;
$paginationDisplay .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $nextPage . '"> Next</a> ';
}
}
//////// END 分页显示设置 ////////////////////////p>
// 在此处构建输出部分
$outputList = "";
while($row = mysql_fetch_array($sql2)) { /// populate others data if not in the 4 arrays... ]
////////////////// 这是在 while 循环中填充其他成员数据的地方...
/// 所有这些都有效,但是当人们被添加到 4 个数组中的任何一个时(当页面刷新时),数据无法正确显示,但行数 ($nr) 始终正确。有时 $nr 会说 5 但只有 3 人显示,或者会说 2 但没有人显示。
这是我的第一次分页,所以错误可能就在那里。
让我知道我的条件逻辑是否正确,或者是否有更好的方法来做我正在尝试的事情。
提前谢谢你
史蒂夫
【问题讨论】:
标签: php mysql pagination