PHP in_array logic to determine #row and #pages
I am 4 months new to php (so be gentle) and I am trying to determine how many rows(people) are in a table to populate my page (while meeting conditions). I think I am on the right track but there may be a better way to do what I am doing since i am still a newb...
I am doing a query for my info using $myid for the query condition for the one record. I use this to 'retrieve' and 'explode' an array (actually 4 separate arrays - friends, blocked, interested, discarded). No issues with this...
I then do the same query as above but without $myid as a condition (to acquire others data)
Here is where i think I am dropping the ball: I am trying to only run a while loop from the 2nd query if the others data IS NOT in any of my 4 arrays...while counting the $nr (number of rows).
[ 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 ]
/////////// Then I do pagination code: (I will start with the else command from above) ////////////
[ } 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> ';
$centerPa开发者_C百科ges .= ' <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> ';
}
// This line sets the "LIMIT" range... the 2 values we place to choose a range of rows from database in our query (( set to show 5 per page now )))) $limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage;
// Now we are going to run the same query as above but this time add $limit onto the end of the SQL syntax
// $sql2 is what we will use to fuel our while loop statement below
$sql2 = mysql_query("the query goes here and added $limit ");
/////////////////////// END Pagination Logic ////////////////////// //////////////////////// Pagination Display Setup /////////////////
$paginationDisplay = "";
// Initialize the pagination output variable // This code runs only if the last page variable is not equal to 1, if it is only 1 page we require no paginated links to display
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 Pagination Display Setup ////////////////////
// Build the Output Section Here
$outputList = "";
while($row = mysql_fetch_array($sql2)) { /// populate others data if not in the 4 arrays... ]
//////////////////// This is where the other members data gets populated in the while loop...
/// All this works but as people get added to any of the 4 arrays (when the page refreshes) the data doesn't display correctly but the number of rows ($nr) is always correct. Sometimes the $nr will say 5 but only 3 people show OR it will say 2 but no one shows.
This is my first pagination so the error is probably in there.
Let me know if my conditional logic is right or if there is a better way to do what I am trying.Thank you in advance
Steve
This is a problem all of us have faced as developer time and time again. Most problems like this have been solved by someone in a library class. Here are a couple that should be able to do what you're trying to do:
- http://pear.php.net/package/Pager
- http://framework.zend.com/manual/en/zend.paginator.html
精彩评论