473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Help to paginate query result

siridyal
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
Aug 6 '07 #1
8 2424
ak1dnar
1,584 Expert 1GB
You Can use the mySql LIMIT for this.
just google for php+mysql+pagination there are lots of tutorials.
Aug 6 '07 #2
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...
Aug 7 '07 #3
ak1dnar
1,584 Expert 1GB
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.
Aug 7 '07 #4
ak1dnar
1,584 Expert 1GB
This article seems to be a good one to start over for you.
Aug 7 '07 #5
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
Aug 7 '07 #6
kovik
1,044 Expert 1GB
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.
Aug 7 '07 #7
kovik
1,044 Expert 1GB
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.
Aug 8 '07 #8
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
Aug 24 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: James | last post by:
My form and results are on one page. If I use : if ($Company) { $query = "Select Company, Contact From tblworking Where ID = $Company Order By Company ASC"; }
2
by: Xavier Bourguignon | last post by:
Hi, I am new to PHP and I am having a little problem displaying data taken from the mysql database running on my server. The code below shows a html (php) page which is supposed to display the...
9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
1
by: VB Programmer | last post by:
Is it possible to paginate or sort a datalist in ASP.NET 2.0 easily (as it is with the amazing datagrid)? Thanks!
9
by: dajava | last post by:
Hi, Sorry for this beginner's question. I do not know PHP and write for my friend. He has never been a professional programmer. He studied C and PHP with some books and made a bulletin...
9
by: Man-wai Chang | last post by:
I would like to do something like this: select * from myTable where ... pagesize 10 page 1 How could I do it elegantly in: 1. PHP+MySQL before MySQL 5 (without stored procedures) 2. MySQL...
2
by: vacuno | last post by:
Hi! Im reading a xml file via http and writing this content in browser with this code in vbscript/asp: ************* <% dim xmlDom, oNode, nodeCol set xmlDom =...
4
osward
by: osward | last post by:
I had made a table colum sortable and paging the table, following are the code // Display Event List echo "<center>"._EVENTLIST."</center><br>"; $now = Date(Y-m-d); // sort table...
10
Fary4u
by: Fary4u | last post by:
I've recently find some Paginate Plugin but it's only came with NEXT & PRV HERE IS THE CODE eval(function(p,a,c,k,e,r){e=function(c) {...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.