【问题标题】:PHP in_array logic to determine #row and #pagesPHP in_array 逻辑来确定#row 和#pages
【发布时间】: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 .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
} else if ($pn == $lastPage) {
$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
$centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
} else if ($pn > 2 && $pn < ($lastPage - 1)) {
$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub2 . '">' . $sub2 . '</a> &nbsp;';
$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
$centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add2 . '">' . $add2 . '</a> &nbsp;';
} else if ($pn > 1 && $pn < $lastPage) {
$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
$centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
}

// 此行设置“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 .=  '&nbsp;  <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 .=  '&nbsp;  <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


    【解决方案1】:

    这是我们所有人作为开发人员一次又一次面临的问题。像这样的大多数问题都已由库类中的某个人解决。这里有几个应该能够做你想做的事情:

    【讨论】:

    • 付钱?作为一个拥有 10 年以上 PHP/webdev 经验的人(并且在他那个时代写了一堆分页类!)我必须建议,每当你遇到一个常见问题 9/10 次时,最好的解决方案就是不要重新- 发明轮子。不过我为你的热情鼓掌! :)
    猜你喜欢
    • 2012-10-03
    • 1970-01-01
    • 2016-04-29
    • 2012-04-21
    • 1970-01-01
    • 1970-01-01
    • 2012-07-29
    • 2016-02-08
    • 1970-01-01
    相关资源
    最近更新 更多