Help to paginate query result  | Newbie | | Join Date: Aug 2007 Location: beijing, china
Posts: 7
| | |
I have a wholesale website that i'm working on that shows hundreds of items that are updated from time to time. These items are kept in a mysql database with several tables.
I want to let the the customer browse (from a dynamically created drop down list - which i've done) the items by furniture class, and then have the query results limited to 26 items per page, in two columns using DIVs. The CSS for the DIVs is taken care of, as is the PHP to query the database as shown at: http://www.chinaonsite.com/washburn/ctr4/example.6-8.php
Below is the code that has gotten me this far. I now want to:
*limit results that are shown per page
*have the results shown in two column divs"leftcol" "rightcol"
*dynamically create previous/next/first/last links - paginate
[PHP]
<?php
require "../includes/db.inc";
// selectDistinct() function shown in Example 6-9 goes here
require "example.6-9.php";
// Connect to the server
if (!($conn = @ mysql_connect($hostName, $username, $password)))
;
if (!mysql_select_db($databaseName, $conn))
;
print "<p>\nType of Furniture:</p>";
// Produce the select list
// Parameters:
// 1: Database connection
// 2. Table that contains values
// 3. Attribute that contains values
// 4. <SELECT> element name
// 5. Optional <OPTION SELECTED>
selectDistinct($conn, "furniture_class", "furniture_class_id", "typeName",
"All");
?>
<br />
<input type="submit" value="Show Types" /><br />
</form>
<?php
// Show all items in a furniture class in a
function displayClassList($conn,
$query,
$typeName)
{
// Run the query on the server
if (!($result = @ mysql_query ($query, $conn)))
showerror();
// Find out how many rows are available
$rowsFound = @ mysql_num_rows($result);
// If the query has results ...
if ($rowsFound > 0)
{
// ... print out a header
print "pieces from furniture type $typeName<br>";
// Report how many rows were found
print "{$rowsFound} records found matching your criteria<br>";
// and start a <table>.
print '<ul class="contentone">';
// Fetch each of the query rows
while ($row = @ mysql_fetch_array($result))
{
// Print one row of results
print '<li class="float expand">' .
"<p>{$row["sku_id"]}</p>" .
"<p>{$row["length"]} " ." x " . " {$row["depth"]} " . " x " . "{$row["height"]}</p>" .
"<p>Unit Price: {$row["exWx"]}</p>" .
"<p>Quantity: {$row["quantity"]}</p>" . "<a " .
'class="p1" ' . 'href="../../Product/RAY/070721/' .
"{$row["sku_id"]}" . '.JPG">' .
'<img src="../../Product/RAY/070721/' . "{$row["sku_id"]}" . '.JPG"/>' .
'<img class="large" src="../../Product/RAY/070721/' . "{$row["sku_id"]}" . '.JPG" ' .
'title="' . "{$row["description"]}" . '"/>' .
"</a></li>";
} // end while loop body
// Finish the <table>
print "</ul>";
} // end if $rowsFound body
} // end of function
// Connect to the MySQL server
if (!($conn = @ mysql_connect($hostName, $username, $password)))
die("Could not connect");
// Secure the user parameter $regionName
$typeName = mysqlclean($_GET, "typeName", 30, $conn);
if (!mysql_select_db($databaseName, $conn))
showerror();
// Start a query ...
$query = "SELECT sku_id, description, quantity, exWx, length, depth, height
FROM furniture, furniture_class
WHERE furniture.furniture_class = furniture_class.furniture_class_id";
// ... then, if the user has specified a region, add the typeName
// as an AND clause ...
if (isset($typeName) && $typeName != "All")
$query .= " AND furniture_class_id = \"{$typeName}\"";
// ... and then complete the query.
$query .= " ORDER BY sku_id";
// run the query and show the results
displayClassList($conn, $query, $typeName);
?>
[/PHP]
Any thoughts or ideas would be much appreciated. This is my first PHP driven website, so the hours have been long and frustrating.
Thanks - Siri
|  | Moderator | | Join Date: Jan 2007 Location: Colombo
Posts: 1,440
| | | re: Help to paginate query result |  | Newbie | | Join Date: Aug 2007 Location: beijing, china
Posts: 7
| | | re: Help to paginate query result
A question whose answer probably lies within one of the first chapters of the two php books that i've got my hands on, that i overlooked.
For each <?php...?> function, that makes a call to the database do i need to include a new $connection and $query ?
For example:
in order for the products to be displayed based on type of furniture i use a relational database connect:
// Connect to the MySQL server
if (!($conn = @ mysql_connect($hostName, $username, $password)))
die("Could not connect");
and query:
[PHP]
// Start a query ...
$query = "SELECT sku_id, description, quantity, exWx, length, depth, height
FROM furniture, furniture_class
WHERE furniture.furniture_class = furniture_class.furniture_class_id";[/PHP]
So for the pagination to work do i use the same connect and query, but within a new <?php...?> tag and additional pagination code?
Again thanks for the help...
|  | Moderator | | Join Date: Jan 2007 Location: Colombo
Posts: 1,440
| | | re: Help to paginate query result
There are several ways of doing the pagination. If you went through the googled samples well, Its not that much big deal my friend.
If I am answering to your question you can create some global Connection string and functions and reuse them.
If You want me to build up a sample for this, Sorry I cant do that shortly.
My boss'll kick me out from my job.
|  | Moderator | | Join Date: Jan 2007 Location: Colombo
Posts: 1,440
| | | re: Help to paginate query result
This article seems to be a good one to start over for you.
|  | Newbie | | Join Date: Aug 2007 Location: beijing, china
Posts: 7
| | | re: Help to paginate query result
Thank you ajaxrand to take the time to answer my questions so quickly! I truly appreciate it. However let me back up one or maybe two steps.
Firstly, i don't want someone to code this for me (thank you for the offer, but i don't want your job to be lost due to me :-) ). I want to figure it out on my own.
I guess my question is more of a basic theoretical question:
If i have a query where the results are equal to about 100, and these results are pulled from a database of over 800, i want to limit these results to 26 per page.
My question is how do i create paging based on the initial querie's results?
I have figured out how to create paging for my entire database, meaning i have 800 results, at 26 per page. But that's without any previous query limiting my results to a total of 100 (or however many pieces of furniture there are in a specific category).
To reiterate from my initial post, i am totally new to php, and i basically don't understand how to create an index for pages based on another query.
Does what I'm asking make sense? Or am i missing something?
Again thank you for your help!
Siri
|  | Expert | | Join Date: Jun 2007 Location: Baltimore
Posts: 587
| | | re: Help to paginate query result
Pagination is simply MySQL LIMITs, GET variables to determine the current page, and displaying the links.
It isn't exactly complicated, but there is enough going on that it can be broken down into it's individual elements. I was planning on holding off on this, but I've decided that I'm going to make the pagination classes that I use public, and write a tutorial on their creation. PM me, and I'll notify you when it's completed.
|  | Expert | | Join Date: Jun 2007 Location: Baltimore
Posts: 587
| | | re: Help to paginate query result
I expected this thread to be pages away by the time I finished. Object-Oriented Pagination in PHP.
The classes that are produced during these tutorials serve as a bit of a middle-man between the database and the application.
|  | Newbie | | Join Date: Aug 2007 Location: beijing, china
Posts: 7
| | | re: Help to paginate query result
Thank you both ajaxrand and volectricity. I had some difficulty understanding how to use the classes that were created from your tutorial volectricity. Some is an understatement. I didn't go through with it 'cause I just wasn't really sure how to use the classes. I haven't quite gotten my head around OOP yet.
I was able to figure out how to do what I needed. You can go to my test site to see what came out of it.
The drop down list is dynamically created based on a 'furniture_class' table, and once selecting a type of furniture, php takes that input and queries the table that holds everything i have in my warehouse.
Now what I am working on is a user login so that the css files that are loaded depend on who the client is. I currently have this type of set up:
www...orderForm/clientOne
www...orderForm/clientTwo
and so on.
So what I want to do is create login class that when clientOne logs in they are taken to a site that has their logo/colors/designs etc. Also I want the session variable to be carried through a shopping cart procedure (I'm not sure if that came out right).
I found a really good (what i think is anyway) tutorial on building a rather extensive login class here, and a simple (and i hope effective) shopping cart class here.
This post is sort of just checking in, but i'm also looking for some community support to let me know that i'm on the right track, and also to find out if there are any other places you guys/gals think i should be looking.
Thanks for the support -- Siri
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,510 network members.
|