View Full Version : Building Your Own PHP/SQL Navigation System

10th January 2005, 10:15 AM
Next in my php tutorials is building a navigation system.
Im not going to give out all the code as i would normally be selling it.
But i ill give you some examples and give you tips on how to put it together

First off you need to set the $start variable.i find it best to pass it through the url.

so you would have some code to get it from the url

But what if it isnt set ?
thats where "if" statements come in.

$start=0 ;
} else {

That will check if its blank and if so it will set it to "0"
the last few lines are redundent but its good practice.

So Now You need a $limit variable to set how many results to show on each page.

Now you have those 2 variables you can use them in a mysql query.
$sql_query="SELECT * FROM table ORDER BY id asc LIMIT '$limit' '$start' "

That will perform a query and will only find number of results from you $limit variable and it will star from the $start variable

Next you need to know how many pages there will be in total
Another mysql query will help you get that number
This time you will use a query with no limit.
$sql_rows="SELECT * FROM table"
Then to determine the total number of results

Now you have a variable called $total_rows which contains the number of results.
This number alone is useless.
You need to divide it by the numer in the $limit variable

if you had 25 results and you divide it by 10 the output is 2.5
that also is useless for navigation.
you need to to be a whole number for it to work properly.
there are a few php commands that can convert it to a whole number but you need to use the right one.
your options are "ceil", "round" and "floor"

ceil will round the number up.
round will round it up or down depening if it is above .5
floor will round it down

so the logical one to use for this is "floor"
so if you use a statement like this

You will now have a whole number.

The most important aspect is now complete.

There are a few other things you will need to do
You will need to determine if there are multiple pages.
a simple if stament will tell you that
if($total_pages >1)

You will also need to determine if the page the user is on is either the first or last page.
To do this you will need another variable containing the current page
the current page will be equal to the start variable divided by the $limit variable which in this case is 10.

Now you can user the $curr_page variable to work out if they are on the first or last page.

that will of course tell you they are on page 1

that will tell you if they are on the last page.
the -1 is used because the currpage will be 0 onwards but the $total_page variable starts at 1.

Those checks allow to show only next buttons for the start page and back buttons for the last page

Now you can build you links inside those if statments.

for next buttons you want the url to be mypage.php?start=$start+10
and for next it would be -10

Thats more or less all you need.
Hope this helps