Advertisement

12.30.2007 at 06:11PM PST, ID: 23050208
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.6

Paging mysql results with php - only the first page of results loads correctly.

Asked by databsl in PHP and Databases, PHP Scripting Language, MySQL Server

Tags:

I have been trying for weeks to get paging working for myql database results. I can get to the point where my results are limited by $offset and $rowsperpage, and paging links showing up but going through to the 2nd page of results doesnt display anything.

I have tried some tweaks found elsewhere on the net but the only improvement I can make is getting to the point where it loads the first 5 results on every single page (hardly an improvement).

Any help is greatly appreciated.

Below is the original code, based on a forum from tizag.com and modified.Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
//Variables from form POST
$type=$_POST['type'];
$cost=$_POST['cost'];
$capacity=$_POST['capacity'];
$alcohol=$_POST['alcohol'];
$food=$_POST['food'];
$location=$_POST['location'];
$keywords=$_POST['keywords']; 
 
//Connect to database
$con = mysql_connect("localhost","root");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db("venuetest", $con); 
 
// How many rows of results need to be on each page?
$rowsPerPage = 5;
 
// by default we show first page
$pageNum = 1;
 
// if $_GET['page'] defined, use it as page number
if(isset($_GET['page'])) {
    $pageNum = $_GET['page']; 
	}
	
// Count the offset so each page will know how many rows to skip in order to get to the rows meant for each page
$offset = ($pageNum - 1) * $rowsPerPage;
 
//query database
$query = "select * from venues where (classification = '$type' AND 
												   cost='$cost' AND
												   capacity='$capacity' AND
												   alcohol='$alcohol' AND
												   food='$food' AND
												   location='$location' AND
												   keywords LIKE '$keywords') LIMIT $offset, $rowsPerPage";
												   
$result=mysql_query($query);
 
// We have to query the database again to count how many rows we have total
$query2   = "select * from venues where (classification = '$type' AND 
												   cost='$cost' AND
												   capacity='$capacity' AND
												   alcohol='$alcohol' AND
												   food='$food' AND
												   location='$location' AND
												   keywords LIKE '$keywords')";
$result2  = mysql_query($query2) or die('Error, query2 failed');
$numrows = mysql_num_rows($result2);
 
//count total rows in database
$total = mysql_query("select * from venues") or die (mysql_error());
$dbtotal = mysql_num_rows($total);
 
//If no results display message
if ($numrows == 0)
	{
	echo ("Please return to the search page and broaden your search criteria");
	}
	
//otherwise, display results in a table
else 
	{ 
echo "<table>
while($showresult = mysql_fetch_array( $result )) {
echo ("<tr><td>");
echo ($showresult["venue"]);
echo ("</td><td>");
echo ($showresult["email"]);
echo ("</td><td>");
echo ($showresult["images"]);
echo ("</td></tr>");
	}
	}
echo "</table>";
 
// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);  
 
// Print page number links
$self = $_SERVER['PHP_SELF'];
$nav = '';
for($page = 1; $page <= $maxPage; $page++) {
    if ($page == $pageNum) {
            $nav .= " $page ";   // no need to create a link to current page
    } else {
            $nav .= " <a href=\"$self?page=$page&rowsPerPage=$rowsPerPage\" class='page'>$page</a> ";
    }
}
 
    // creating previous and next link
    // plus the link to go straight to
    // the first and last page
    if ($pageNum > 1) {
        $page = $pageNum - 1;
        $prev = " <a href=\"$self?page=$page&rowsPerPage=$rowsPerPage\" class='page'>&lt;</a> ";
         
        $first = " <a href=\"$self?page=1&rowsPerPage=$rowsPerPage\" class='page'>&lt;&lt;</a> ";
    } else {
        $prev  = '&nbsp;'; // we're on page one, don't print previous link
        $first = '&nbsp;'; // nor the first page link
    }
    if ($pageNum < $maxPage) {
        $page = $pageNum + 1;
        $next = " <a href=\"$self?page=$page&rowsPerPage=$rowsPerPage\" class='page'>&gt;</a> ";
        $last = " <a href=\"$self?page=$maxPage&rowsPerPage=$rowsPerPage\" class='page'>&gt;&gt;</a> ";
    } else {
        $next = '&nbsp;'; // we're on the last page, don't print next link
        $last = '&nbsp;'; // nor the last page link
    }
// print the navigation link
echo $first . $prev . $nav . $next . $last;
// and close the database connection
mysql_close();
[+][-]12.30.2007 at 08:55PM PST, ID: 20553365

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.01.2008 at 01:17PM PST, ID: 20561431

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.01.2008 at 03:14PM PST, ID: 20561722

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.02.2008 at 12:54AM PST, ID: 20563254

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: PHP and Databases, PHP Scripting Language, MySQL Server
Tags: php
Sign Up Now!
Solution Provided By: schmavey
Participating Experts: 1
Solution Grade: B
 
 
[+][-]01.03.2008 at 04:15PM PST, ID: 20578541

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628