drayarms Posted June 23, 2011 Share Posted June 23, 2011 I must begin by apologizing for the lengthiness of this posting but I really couldn't edit it to be any shorter. Hopefully one of you reading this is quite familiar with both PHP pagination, and displaying search results in rows. Well here's what I'm trying to do. I created a page that displays results from a simple search query. the results displayed are the pictures of a users of a website, alongside their usernames. I'm trying to display the results in rows of 4, as dictated by the $items variable on line 27 of the script i will post below in a minute. Well, the script works just fine as intended. Then when I add the blocks of code that should paginate the results (the pagination blocks of code are indented to the left of the script), I run into a bunch of problems. First of all, oddly enough, when I set the number of rows to be displayed per page to any number order than 4, the page never completes the loading process and the pagination links are not displayed. The page only loads fully and displays the pagination links when $numrows is set to 4 and even then, when I click on the link to take me to the next page, no results get displayed. Instead I get the "No results match this search query' message which is displayed when the query returns no results. It seems to me like there are problems with the pagination code, I'm not quite sure. I have tried about 3 different pagination codes which I found from online tutorials and they all present the same problems. Any help? Here is my page. PS One more problem, when I set the $limit variable(which sets the number of results displayed per row) to 4, the data to be displayed(pictures) get extra padding between them, pushing the last one out of the page. Ironically as explained above, this is the only case where the page loads completely and the pagination links get displayed. To make you picture this easier, I have removed the authentication restrcition off this page and I'll provide the link for you to see what I'm talking about http://mygeneric.info/search_results.php?ethnicity=white <?php //address error handling ini_set ('display_errors', 1); error_reporting (E_ALL & ~E_NOTICE); //authenticate user require('auth.php'); //Set session timeout require('inactive.php'); //Set the page title before the header file $title = 'Search Results'; //Get variables from previous page, profile_searh.php $ethnicity = $_GET['ethnicity']; //Items to display per row. $items = 4; require ('header.php'); //need the header echo' <div id="content" class=""> <div id="left_content" class=""> </div> <!--closes left content--> <div id="right_content" class=""> <div id= "right_content_inner_border"> '; //Obtain the required page number from the $_GET array. Note that if it is not present it will default to 1. if (isset($_GET['pageno'])) { $pageno = $_GET['pageno']; } else { $pageno = 1; } // if // Connect to the database. require_once ('config.php'); //Count how many rows will satisfy the current query.. $query = "SELECT* FROM members INNER JOIN images ON members.member_id = images.member_id WHERE members.ethnicity = '$ethnicity' AND images.image_cartegory = 'main' "; $result = mysql_query($query); $numrows = mysql_num_rows($result); //Use the values in $rows_per_page and $numrows in order to identify the number of the last page. $rows_per_page = 4; $lastpage = ceil($numrows/$rows_per_page); //Check that the value of $pageno is an integer between 1 and $lastpage. $pageno = (int)$pageno; if ($pageno > $lastpage) { $pageno = $lastpage; } // if if ($pageno < 1) { $pageno = 1; } // if //Construct the LIMIT clause for the sql SELECT statement. $limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page; //Now we can issue the database qery and process the result. $query = "SELECT* FROM members INNER JOIN images ON members.member_id = images.member_id WHERE members.ethnicity = '$ethnicity' AND images.image_cartegory = 'main' $limit "; $result = mysql_query($query) or die(mysql_error()); //Check for success here. if(!$result) { die('<p>Could not retrieve the data because: <b>' . mysql_error(). '</p>'); // Handle as desired }else{ //If query is valid. if(mysql_num_rows($result) > 0){ //Start a table to contain the results. echo '<table border= "0" width="100%" cellspacing= "1" cellpadding ="1" align ="center"> '; //Need a counter. $i = 0; //Retrieve each record. while ($row = mysql_fetch_assoc($result)){ //Do we need to start a new row? if($i == 0) {echo "<tr>\n";} //Print the record echo"\t<td align=\"center\"> <div id='search_pic_wrap'> <div id='no_photo'>"; //Define variables to be passed via url. $friend= $row['member_id']; //Make sure an uploaded photo is present. if ($row['image'] !== NULL) { echo"<a href='friend.php?friend=$friend' >"; // Note that we are building our src string using the filename from the database. echo "<img src=\"images/" . $row['image'] . "\" width=\"140\" maxheight=\"154\" />"; echo "</a>"; echo"<h2 style= 'position:relative;bottom:25px;'>" .$row['username']. "</h2> //End of if there is an uploaded pic.} </div> <!--closes no photo--> </div> <!--closes search pic wrap--> </td>\n"; //Icrement the counter. $i++; //Do we need to end the row? if ($i == $items) {echo "</tr>\n"; $i = 0; //Reset counter. } }//End of while loop. if ($i > 0) {//Last row was incomplete. //Print the necessary number of cells. for ($i < $items; $i++ { echo "<td> </td>\n"; } //Complete the row. echo '</tr>'; }//End of if $i is greater than 0 //Close the table. echo '</table>'; echo '<div style= " position:absolute;top:1050px;left:600px;">'; //Construct the hyperlinks which will allow the user to select other pages. We will start with the links for any previous pages. if ($pageno == 1) { echo " FIRST PREV "; } else { echo " <a href='{$_SERVER['PHP_SELF']}?pageno=1'>FIRST</a> "; $prevpage = $pageno-1; echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'>PREV</a> "; } // if //Inform the user of his current position in the sequence of available pages. echo " ( Page $pageno of $lastpage ) "; //Provide the links for any following pages. if ($pageno == $lastpage) { echo " NEXT LAST "; } else { $nextpage = $pageno+1; echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage'>NEXT</a> "; echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$lastpage'>LAST</a> "; } // if echo'</div>'; }else {//No rows returned. End of if number of rows is greater than 0 loop. echo 'No results match this search query.' ; } }//End of else if query is valid. echo' </div> <!--closes right content inner border--> </div> <!--closes right content--> </div> <!--closes content--> '; require ('footer.php'); //need the footer ?> Quote Link to comment https://forums.phpfreaks.com/topic/240249-pagination-seems-to-conflict-with-data-display/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.