Jump to content

Pagination Script


Russia

Recommended Posts

I am currently in need of a simple pagination that will run from a database, basicly it will go to a table called 'persons' and post every row on seperate pages, limiting it to 10 or 15 each page.

 

If someone can help me out with that I would be happy.

 

What it needs to do:

Get rows from 'persons' table

show rows on a webpage.

limit up to 10 rows on each page, then creates a next page or a page number.

 

Thank You.

Link to comment
Share on other sites

I currently have this:

 

 

<?php
if(empty($_GET['page'])) {
echo 'Page number is not defined!';
exit();
}



else if(!preg_match("/^([0-9])+$/",$_GET['page'])) {
echo 'Page is not a valid value!';
exit();
}



$num_per_page = (empty($_GET['num']))? 10 : $_GET['num'];
if(!preg_match("/^([0-9])+$/",$num_per_page)) {
echo 'Num is not a valid value!';
exit();
}

$page = $_GET['page'];


$link = mysql_connect('localhost','*****','******');
mysql_select_db('*****');

$qPage = (($page-1)*$num_per_page);


$query = "SELECT * FROM `persons` ORDER BY `col_name` ASC LIMIT ".$qPage.",".($num_per_page+1);
$result = mysql_query($query) or die(mysql_error());



$num = mysql_num_rows($result);
if($num == 0) {
echo 'No more results!';
exit();
}


for($i = 0; $i < $num_per_page; $i++) {
$row = mysql_fetch_array($result);
echo '<pre>';
print_r($row);
echo '</pre>';
}
mysql_close($link);


if($num > $num_per_page) {
echo '<a href="index.php?page='.($page+1).'&num='.$num_per_page.'">Next Page</a>';
}
echo '<br/>'; 


if($page > 1) {
echo '<a href="index.php?page='.($page-1).'&num='.$num_per_page.'">Previous Page</a>';
}

?> 

 

But it is saying:

Page number is not defined!

 

Any help on it?

 

 

Link to comment
Share on other sites

russia i no your the new bill gates.

 

I can see you always ask, i no we ask to learn but we also,

use Google to get examples, if we dont understand a example then we get another until we nearly understand it then we ask others.

 

your asking before you do any tutorials to improve your php skills.

 

please be fair bro.

 

 

 

 

Link to comment
Share on other sites

Hm I have gotten it to work:

 

<?php

/*
Check whether the 'page' GET variable is not defined or equals zero.
In either case, an error is sent and the script terminated.
*/

if(empty($_GET['page'])) {
echo 'Page number is not defined!';
exit();
}

/*
The following makes sure that the 'page' variable only contains numbers,
therefore limiting it to just integers(whole numbers).
*/

else if(!preg_match("/^([0-9])+$/",$_GET['page'])) {
echo 'Page is not a valid value!';
exit();
}

/*
If the 'num' GET variable isn't defined in the URI, then the var num_per_page is by default set to 10.
This utilizes the ternary operator. After the check for existence, then there is a check to make sure that
the string contains only an integer using the same method as above.
*/

$num_per_page = (empty($_GET['num']))? 10 : $_GET['num'];
if(!preg_match("/^([0-9])+$/",$num_per_page)) {
echo 'Num is not a valid value!';
exit();
}
//Get the page number from the URI; the pages start when page=1, not page=0
$page = $_GET['page'];

/*
Connect to database and then select a database.
You can also include this information if you want, but this is just an example 
*/

$link = mysql_connect('localhost','****','****');
mysql_select_db('*******');

/*
This piece is the essence of pagination. Using LIMIT in the mysql query is what we are going
to use for this. The following line creates a new variable, qPage, which is the value of the
page number minus one times the number of rows you want to show up on each page.
For example, if page = 1 in the URI, then 1-1*10 = 0. This is the offset for the LIMIT in the query.
If page = 2then 2-1*10 = 10. Since 10 is the number per page in the example, then it only makes sense
to start the next limit at an offset of 10.
*/

$qPage = (($page-1)*$num_per_page);

/*
The following is the query of the pagination; this is where the magic takes place.
the SELECT is self-explanatory. The LIMIT is the most important. LIMIT has the syntax
LIMIT [offset],[length]. Knowing this format you should be able to follow the query now.
Now for the following you are probably wondering why I added 1 to the value for the number per page.
I did this simply to know if there is going to be another page after the current one. We will need this
information later when we are echoing out the 'Next page' link.

Note: You may want to change the query to fit your needs, however the LIMIT clause should stay the same.
Everything else can be changed 
*/

$query = "SELECT * FROM `persons` ORDER BY `id` ASC LIMIT ".$qPage.",".($num_per_page+1);
//You should remove the 'die(mysql_error());' part of the following after development
$result = mysql_query($query) or die(mysql_error());

/*
Here 'num' is set to the number of rows that the above query returned. The condition simply
sees if the number of rows returned equals zero, because if it does than that means that there
are no more rows for the specified LIMIT.
*/

$num = mysql_num_rows($result);
if($num == 0) {
echo 'No more results!';
exit();
}

/*
Here's is another interesting part. At first thought one may want to use a while loop here.
The only problem with the while loop is that it echos that last row that we selected in the
query when we don't want it to. Instead of handling this it's easier to use a for loop
instead, using the num_per_page variable as shown. Obviously you can format the HTML and PHP
output however you'd like; I'm just outputting the array as an example.
*/

for($i = 0; $i < $num_per_page; $i++) {
$row = mysql_fetch_array($result);
echo '<pre>';
print_r($row);
echo '</pre>';
}
mysql_close($link); //Close the link to the MySQL database.

/*
This goes back to selecting that extra row in the query. If the amount selected (num) is
greater than the number per page(num_per_page) than that means that there is at least one more
row to be returned after the the range that you want, therefore you know you can echo the 'Next Page'
link without having to worry if you are linking to a blank page or not.

The rest should be self-explanatory. If you are going onto the next page, you are going to increment the
page variable by one, and of course you still want to keep the current number per page otherwise you may
see repeats, so you just echo that value in it's corresponding place within the url query.
*/

if($num > $num_per_page) {
echo '<a href="index.php?page='.($page+1).'&num='.$num_per_page.'">Next Page</a>';
}
echo '<br/>'; //Just acts as a separator for the two links 

/*
The following acts the same way as above, however instead of incrementing you are subtracting one from the
page variable to 'go back' to the previous page. Obviously you do not want to 'go back' when you are on the first page
to begin with, so we first check to make sure that the page value is larger than '1' (our first page) before we start
echoing it to the browser.
*/

if($page > 1) {
echo '<a href="index.php?page='.($page-1).'&num='.$num_per_page.'">Previous Page</a>';
}

?>

 

But it is spitting out the information like this:

 

Array
(
    [0] => 
    [FirstName] => 
    [1] => 
    [LastName] => 
    [2] => 173.70.169.187
    [ip] => 173.70.169.187
    [3] => August 25 2009
    [AddedDate] => August 25 2009
    [4] => 98
    [id] => 98
)

Array
(
    [0] => 
    [FirstName] => 
    [1] => 
    [LastName] => 
    [2] => 173.70.169.187
    [ip] => 173.70.169.187
    [3] => August 25 2009
    [AddedDate] => August 25 2009
    [4] => 99
    [id] => 99
)

Array
(
    [0] => 
    [FirstName] => 
    [1] => 
    [LastName] => 
    [2] => 173.70.169.187
    [ip] => 173.70.169.187
    [3] => August 25 2009
    [AddedDate] => August 25 2009
    [4] => 100
    [id] => 100
)

Array
(
    [0] => 
    [FirstName] => 
    [1] => 
    [LastName] => 
    [2] => 173.70.169.187
    [ip] => 173.70.169.187
    [3] => August 25 2009
    [AddedDate] => August 25 2009
    [4] => 101
    [id] => 101
)

 

How do I make it more formatted like in a table or a nicer way.

Link to comment
Share on other sites

try this way

for($i = 0; $i < $num_per_page; $i++) {
$row = mysql_fetch_assoc($result)or die("mysql_error()");

echo $row['database row name']; // like username then format it in html //tables / css what ever.

}
mysql_close($link); //Close the link to the MySQL database.

Link to comment
Share on other sites

Ok, I did that, here is the updated code:

 

<?php

/*
Check whether the 'page' GET variable is not defined or equals zero.
In either case, an error is sent and the script terminated.
*/

if(empty($_GET['page'])) {
echo 'Page number is not defined!';
exit();
}

/*
The following makes sure that the 'page' variable only contains numbers,
therefore limiting it to just integers(whole numbers).
*/

else if(!preg_match("/^([0-9])+$/",$_GET['page'])) {
echo 'Page is not a valid value!';
exit();
}

/*
If the 'num' GET variable isn't defined in the URI, then the var num_per_page is by default set to 10.
This utilizes the ternary operator. After the check for existence, then there is a check to make sure that
the string contains only an integer using the same method as above.
*/

$num_per_page = (empty($_GET['num']))? 10 : $_GET['num'];
if(!preg_match("/^([0-9])+$/",$num_per_page)) {
echo 'Num is not a valid value!';
exit();
}
//Get the page number from the URI; the pages start when page=1, not page=0
$page = $_GET['page'];

/*
Connect to database and then select a database.
You can also include this information if you want, but this is just an example 
*/

$link = mysql_connect('localhost','****','********');
mysql_select_db('*****');

/*
This piece is the essence of pagination. Using LIMIT in the mysql query is what we are going
to use for this. The following line creates a new variable, qPage, which is the value of the
page number minus one times the number of rows you want to show up on each page.
For example, if page = 1 in the URI, then 1-1*10 = 0. This is the offset for the LIMIT in the query.
If page = 2then 2-1*10 = 10. Since 10 is the number per page in the example, then it only makes sense
to start the next limit at an offset of 10.
*/

$qPage = (($page-1)*$num_per_page);

/*
The following is the query of the pagination; this is where the magic takes place.
the SELECT is self-explanatory. The LIMIT is the most important. LIMIT has the syntax
LIMIT [offset],[length]. Knowing this format you should be able to follow the query now.
Now for the following you are probably wondering why I added 1 to the value for the number per page.
I did this simply to know if there is going to be another page after the current one. We will need this
information later when we are echoing out the 'Next page' link.

Note: You may want to change the query to fit your needs, however the LIMIT clause should stay the same.
Everything else can be changed 
*/

$query = "SELECT * FROM `persons` ORDER BY `id` ASC LIMIT ".$qPage.",".($num_per_page+1);
//You should remove the 'die(mysql_error());' part of the following after development
$result = mysql_query($query) or die(mysql_error());

/*
Here 'num' is set to the number of rows that the above query returned. The condition simply
sees if the number of rows returned equals zero, because if it does than that means that there
are no more rows for the specified LIMIT.
*/

$num = mysql_num_rows($result);
if($num == 0) {
echo 'No more results!';
exit();
}

/*
Here's is another interesting part. At first thought one may want to use a while loop here.
The only problem with the while loop is that it echos that last row that we selected in the
query when we don't want it to. Instead of handling this it's easier to use a for loop
instead, using the num_per_page variable as shown. Obviously you can format the HTML and PHP
output however you'd like; I'm just outputting the array as an example.
*/

for($i = 0; $i < $num_per_page; $i++) {
$row = mysql_fetch_assoc($result)or die("mysql_error()");

echo $row['id']; // like username then format it in html //tables / css what ever.

}
mysql_close($link); //Close the link to the MySQL database.

/*
This goes back to selecting that extra row in the query. If the amount selected (num) is
greater than the number per page(num_per_page) than that means that there is at least one more
row to be returned after the the range that you want, therefore you know you can echo the 'Next Page'
link without having to worry if you are linking to a blank page or not.

The rest should be self-explanatory. If you are going onto the next page, you are going to increment the
page variable by one, and of course you still want to keep the current number per page otherwise you may
see repeats, so you just echo that value in it's corresponding place within the url query.
*/

if($num > $num_per_page) {
echo '<a href="index.php?page='.($page+1).'&num='.$num_per_page.'">Next Page</a>';
}
echo '<br/>'; //Just acts as a separator for the two links 

/*
The following acts the same way as above, however instead of incrementing you are subtracting one from the
page variable to 'go back' to the previous page. Obviously you do not want to 'go back' when you are on the first page
to begin with, so we first check to make sure that the page value is larger than '1' (our first page) before we start
echoing it to the browser.
*/

if($page > 1) {
echo '<a href="index.php?page='.($page-1).'&num='.$num_per_page.'">Previous Page</a>';
}

?> 

 

It shows this error:

104mysql_error()

Link to comment
Share on other sites

Try this big boy lol

 

 

 



<?php

/*
Check whether the 'page' GET variable is not defined or equals zero.
In either case, an error is sent and the script terminated.
*/

if(empty($_GET['page'])) {
echo 'Page number is not defined!';
exit();
}

/*
The following makes sure that the 'page' variable only contains numbers,
therefore limiting it to just integers(whole numbers).
*/

else if(!preg_match("/^([0-9])+$/",$_GET['page'])) {
echo 'Page is not a valid value!';
exit();
}

/*
If the 'num' GET variable isn't defined in the URI, then the var num_per_page is by default set to 10.
This utilizes the ternary operator. After the check for existence, then there is a check to make sure that
the string contains only an integer using the same method as above.
*/

$num_per_page = (empty($_GET['num']))? 10 : $_GET['num'];
if(!preg_match("/^([0-9])+$/",$num_per_page)) {
echo 'Num is not a valid value!';
exit();
}
//Get the page number from the URI; the pages start when page=1, not page=0
$page = $_GET['page'];

/*
Connect to database and then select a database.
You can also include this information if you want, but this is just an example 
*/

$link = mysql_connect('localhost','****','****');
mysql_select_db('*******');

/*
This piece is the essence of pagination. Using LIMIT in the mysql query is what we are going
to use for this. The following line creates a new variable, qPage, which is the value of the
page number minus one times the number of rows you want to show up on each page.
For example, if page = 1 in the URI, then 1-1*10 = 0. This is the offset for the LIMIT in the query.
If page = 2then 2-1*10 = 10. Since 10 is the number per page in the example, then it only makes sense
to start the next limit at an offset of 10.
*/

$qPage = (($page-1)*$num_per_page);

/*
The following is the query of the pagination; this is where the magic takes place.
the SELECT is self-explanatory. The LIMIT is the most important. LIMIT has the syntax
LIMIT [offset],[length]. Knowing this format you should be able to follow the query now.
Now for the following you are probably wondering why I added 1 to the value for the number per page.
I did this simply to know if there is going to be another page after the current one. We will need this
information later when we are echoing out the 'Next page' link.

Note: You may want to change the query to fit your needs, however the LIMIT clause should stay the same.
Everything else can be changed 
*/

$query = "SELECT * FROM `persons` ORDER BY `id` ASC LIMIT ".$qPage.",".($num_per_page+1);
//You should remove the 'die(mysql_error());' part of the following after development
$result = mysql_query($query) or die(mysql_error());

/*
Here 'num' is set to the number of rows that the above query returned. The condition simply
sees if the number of rows returned equals zero, because if it does than that means that there
are no more rows for the specified LIMIT.
*/

$num = mysql_num_rows($result);
if($num == 0) {
echo 'No more results!';
exit();
}

/*
Here's is another interesting part. At first thought one may want to use a while loop here.
The only problem with the while loop is that it echos that last row that we selected in the
query when we don't want it to. Instead of handling this it's easier to use a for loop
instead, using the num_per_page variable as shown. Obviously you can format the HTML and PHP
output however you'd like; I'm just outputting the array as an example.
*/

for($i = 0; $i < $num_per_page; $i++) {
$row = mysql_fetch_assoc($result)or die(mysql_error());

echo $row['FirstName'];

echo "<br/>";

echo $row['LastName'];

echo "<br/>";

echo $row['Ip'];

echo "<br/>";

echo $row['AddedDate'];

echo "<br/>";

echo $row['id'];

echo "<br/>";


}

mysql_close($link); //Close the link to the MySQL database.

/*
This goes back to selecting that extra row in the query. If the amount selected (num) is
greater than the number per page(num_per_page) than that means that there is at least one more
row to be returned after the the range that you want, therefore you know you can echo the 'Next Page'
link without having to worry if you are linking to a blank page or not.

The rest should be self-explanatory. If you are going onto the next page, you are going to increment the
page variable by one, and of course you still want to keep the current number per page otherwise you may
see repeats, so you just echo that value in it's corresponding place within the url query.
*/

if($num > $num_per_page) {
echo '<a href="index.php?page='.($page+1).'&num='.$num_per_page.'">Next Page</a>';
}
echo '<br/>'; //Just acts as a separator for the two links 

/*
The following acts the same way as above, however instead of incrementing you are subtracting one from the
page variable to 'go back' to the previous page. Obviously you do not want to 'go back' when you are on the first page
to begin with, so we first check to make sure that the page value is larger than '1' (our first page) before we start
echoing it to the browser.
*/

if($page > 1) {
echo '<a href="index.php?page='.($page-1).'&num='.$num_per_page.'">Previous Page</a>';
}

?>

Link to comment
Share on other sites

Thank You.

 

Now how do I limit it to 15 on a page, and it creates like a next button for the next 15...

 

Thank You very much.

 

And one more thing, how do I load the page like this:

 

Right now to load the results I need this url:

http://website.com/pages.php?page=1

 

I want it to show first 1-15 results like this:

http://website.com/pages.php (without the ?page=1 )

 

Then the next page with like 16-30 results will be like

http://website.com/pages.php?page=2

Link to comment
Share on other sites

Trie this bro.

 

 

<?php

/*
Check whether the 'page' GET variable is not defined or equals zero.
In either case, an error is sent and the script terminated.
*/

if(empty($_GET['page'])) {
echo 'Page number is not defined!';
exit();
}

/*
The following makes sure that the 'page' variable only contains numbers,
therefore limiting it to just integers(whole numbers).
*/

else if(!preg_match("/^([0-9])+$/",$_GET['page'])) {
echo 'Page is not a valid value!';
exit();
}

/*
If the 'num' GET variable isn't defined in the URI, then the var num_per_page is by default set to 10.
This utilizes the ternary operator. After the check for existence, then there is a check to make sure that
the string contains only an integer using the same method as above.
*/

$num_per_page = (empty($_GET['num']))? 10 : $_GET['num'];
if(!preg_match("/^([0-9])+$/",$num_per_page)) {
echo 'Num is not a valid value!';
exit();
}
//Get the page number from the URI; the pages start when page=1, not page=0
$page = $_GET['page'];

/*
Connect to database and then select a database.
You can also include this information if you want, but this is just an example 
*/

$link = mysql_connect('localhost','****','****');
mysql_select_db('*******');

/*
This piece is the essence of pagination. Using LIMIT in the mysql query is what we are going
to use for this. The following line creates a new variable, qPage, which is the value of the
page number minus one times the number of rows you want to show up on each page.
For example, if page = 1 in the URI, then 1-1*10 = 0. This is the offset for the LIMIT in the query.
If page = 2then 2-1*10 = 10. Since 10 is the number per page in the example, then it only makes sense
to start the next limit at an offset of 10.
*/

$qPage = (($page-1)*$num_per_page);

/*
The following is the query of the pagination; this is where the magic takes place.
the SELECT is self-explanatory. The LIMIT is the most important. LIMIT has the syntax
LIMIT [offset],[length]. Knowing this format you should be able to follow the query now.
Now for the following you are probably wondering why I added 1 to the value for the number per page.
I did this simply to know if there is going to be another page after the current one. We will need this
information later when we are echoing out the 'Next page' link.

Note: You may want to change the query to fit your needs, however the LIMIT clause should stay the same.
Everything else can be changed 
*/

$query = "SELECT * FROM `persons` ORDER BY `id` ASC LIMIT ".$qPage.",".($num_per_page+1);
//You should remove the 'die(mysql_error());' part of the following after development
$result = mysql_query($query) or die(mysql_error());

/*
Here 'num' is set to the number of rows that the above query returned. The condition simply
sees if the number of rows returned equals zero, because if it does than that means that there
are no more rows for the specified LIMIT.
*/

$num = mysql_num_rows($result);
if($num == 0) {
echo 'No more results!';
exit();
}

/*
Here's is another interesting part. At first thought one may want to use a while loop here.
The only problem with the while loop is that it echos that last row that we selected in the
query when we don't want it to. Instead of handling this it's easier to use a for loop
instead, using the num_per_page variable as shown. Obviously you can format the HTML and PHP
output however you'd like; I'm just outputting the array as an example.
*/

for($i = 0; $i < $num_per_page; $i++) {
$row = mysql_fetch_assoc($result)or die("mysql_error()");

echo $row['FirstName'];

echo "<br/>";

echo $row['LastName'];

echo "<br/>";

echo $row['Ip'];

echo "<br/>";

echo $row['AddedDate'];

echo "<br/>";

echo $row['id'];

echo "<br/>";


}

mysql_close($link); //Close the link to the MySQL database.

/*
This goes back to selecting that extra row in the query. If the amount selected (num) is
greater than the number per page(num_per_page) than that means that there is at least one more
row to be returned after the the range that you want, therefore you know you can echo the 'Next Page'
link without having to worry if you are linking to a blank page or not.

The rest should be self-explanatory. If you are going onto the next page, you are going to increment the
page variable by one, and of course you still want to keep the current number per page otherwise you may
see repeats, so you just echo that value in it's corresponding place within the url query.
*/

if($num > $num_per_page) {
echo '<a href="index.php?page='.($page+1).'&num='.$num_per_page.'">Next Page</a>';
}
echo '<br/>'; //Just acts as a separator for the two links 

/*
The following acts the same way as above, however instead of incrementing you are subtracting one from the
page variable to 'go back' to the previous page. Obviously you do not want to 'go back' when you are on the first page
to begin with, so we first check to make sure that the page value is larger than '1' (our first page) before we start
echoing it to the browser.
*/

if($page > 1) {
echo '<a href="index.php?page='.($page-1).'&num='.$num_per_page.'">Previous Page</a>';
}

?>






<?php
for($i = 0; $i < $num_per_page; $i++) {

$row = mysql_fetch_assoc($result)or die(mysql_error());

echo $row['FirstName'];

echo "<br/>";

echo $row['LastName'];

echo "<br/>";

echo $row['Ip'];

echo "<br/>";

echo $row['AddedDate'];

echo "<br/>";

echo $row['id'];

echo "<br/>";


}
mysql_close($link); //Close the link to the MySQL database.

?>

Link to comment
Share on other sites

Great thanks.

 

I am now trying to integreate it with my delete row script.

 

Can you help me out?

 

Basicly combine these scripts into one, pagination delete rows script.

 

Here are both.

 

PHP Pagination:

 

<?php

/*
Check whether the 'page' GET variable is not defined or equals zero.
In either case, an error is sent and the script terminated.
*/

if(empty($_GET['page'])) {
echo 'Page number is not defined!';
exit();
}

/*
The following makes sure that the 'page' variable only contains numbers,
therefore limiting it to just integers(whole numbers).
*/

else if(!preg_match("/^([0-9])+$/",$_GET['page'])) {
echo 'Page is not a valid value!';
exit();
}

/*
If the 'num' GET variable isn't defined in the URI, then the var num_per_page is by default set to 10.
This utilizes the ternary operator. After the check for existence, then there is a check to make sure that
the string contains only an integer using the same method as above.
*/

$num_per_page = (empty($_GET['num']))? 10 : $_GET['num'];
if(!preg_match("/^([0-9])+$/",$num_per_page)) {
echo 'Num is not a valid value!';
exit();
}
//Get the page number from the URI; the pages start when page=1, not page=0
$page = $_GET['page'];

/*
Connect to database and then select a database.
You can also include this information if you want, but this is just an example 
*/

$link = mysql_connect('localhost','****','****');
mysql_select_db('*******');

/*
This piece is the essence of pagination. Using LIMIT in the mysql query is what we are going
to use for this. The following line creates a new variable, qPage, which is the value of the
page number minus one times the number of rows you want to show up on each page.
For example, if page = 1 in the URI, then 1-1*10 = 0. This is the offset for the LIMIT in the query.
If page = 2then 2-1*10 = 10. Since 10 is the number per page in the example, then it only makes sense
to start the next limit at an offset of 10.
*/

$qPage = (($page-1)*$num_per_page);

/*
The following is the query of the pagination; this is where the magic takes place.
the SELECT is self-explanatory. The LIMIT is the most important. LIMIT has the syntax
LIMIT [offset],[length]. Knowing this format you should be able to follow the query now.
Now for the following you are probably wondering why I added 1 to the value for the number per page.
I did this simply to know if there is going to be another page after the current one. We will need this
information later when we are echoing out the 'Next page' link.

Note: You may want to change the query to fit your needs, however the LIMIT clause should stay the same.
Everything else can be changed 
*/

$query = "SELECT * FROM `persons` ORDER BY `id` ASC LIMIT ".$qPage.",".($num_per_page+1);
//You should remove the 'die(mysql_error());' part of the following after development
$result = mysql_query($query) or die(mysql_error());

/*
Here 'num' is set to the number of rows that the above query returned. The condition simply
sees if the number of rows returned equals zero, because if it does than that means that there
are no more rows for the specified LIMIT.
*/

$num = mysql_num_rows($result);
if($num == 0) {
echo 'No more results!';
exit();
}

/*
Here's is another interesting part. At first thought one may want to use a while loop here.
The only problem with the while loop is that it echos that last row that we selected in the
query when we don't want it to. Instead of handling this it's easier to use a for loop
instead, using the num_per_page variable as shown. Obviously you can format the HTML and PHP
output however you'd like; I'm just outputting the array as an example.
*/

for($i = 0; $i < $num_per_page; $i++) {
$row = mysql_fetch_assoc($result)or die("mysql_error()");

echo $row['FirstName'];

echo "<br/>";

echo $row['LastName'];

echo "<br/>";

echo $row['Ip'];

echo "<br/>";

echo $row['AddedDate'];

echo "<br/>";

echo $row['id'];

echo "<br/>";


}

mysql_close($link); //Close the link to the MySQL database.

/*
This goes back to selecting that extra row in the query. If the amount selected (num) is
greater than the number per page(num_per_page) than that means that there is at least one more
row to be returned after the the range that you want, therefore you know you can echo the 'Next Page'
link without having to worry if you are linking to a blank page or not.

The rest should be self-explanatory. If you are going onto the next page, you are going to increment the
page variable by one, and of course you still want to keep the current number per page otherwise you may
see repeats, so you just echo that value in it's corresponding place within the url query.
*/

if($num > $num_per_page) {
echo '<a href="index.php?page='.($page+1).'&num='.$num_per_page.'">Next Page</a>';
}
echo '<br/>'; //Just acts as a separator for the two links 

/*
The following acts the same way as above, however instead of incrementing you are subtracting one from the
page variable to 'go back' to the previous page. Obviously you do not want to 'go back' when you are on the first page
to begin with, so we first check to make sure that the page value is larger than '1' (our first page) before we start
echoing it to the browser.
*/

if($page > 1) {
echo '<a href="index.php?page='.($page-1).'&num='.$num_per_page.'">Previous Page</a>';
}

?>






<?php
for($i = 0; $i < $num_per_page; $i++) {
   
$row = mysql_fetch_assoc($result)or die(mysql_error());

echo $row['FirstName'];

echo "<br/>";

echo $row['LastName'];

echo "<br/>";

echo $row['Ip'];

echo "<br/>";

echo $row['AddedDate'];

echo "<br/>";

echo $row['id'];

echo "<br/>";


}
mysql_close($link); //Close the link to the MySQL database.

?>

 

PHP Delete Rows Script:

<?php
error_reporting(E_ALL);

require("inc/config.php");
if (isset($_POST['del'])) 
{

    for ($count = 0;$count<count($_POST[delchk]);$count++)
   {
           $delete = $_POST[delchk][$count];
           $query = "DELETE FROM persons WHERE id = '$delete'";
           $result = mysql_query($query);
           if (!$result) 
   {
            die("Error deleting persons! Query: $query<br />Error: ".mysql_error());
        }
    }
}
$result = mysql_query("SELECT * FROM persons");

// Check how many rows it found
if(mysql_num_rows($result) > 0){
echo "<table id=\"mytable\">
<thead>

    <tr>
        <th align=\"center\" scope=\"col\">Delete</th>
        <th align=\"center\" scope=\"col\">First Name</th>
        <th align=\"center\" scope=\"col\">Last Name</th>
        <th align=\"center\" scope=\"col\">Profile</th>
        <th align=\"center\" scope=\"col\">Date of Entry</th>
        <th align=\"center\" scope=\"col\">IP Address</th>
    </tr>
</thead>
<tbody>"; 


echo "<form name = 'myform' action='' method='post'>";
while($row = mysql_fetch_array($result))
  {
  echo "<tr align=\"center\">";
  echo '<td><input type="checkbox" id="delchk" name="delchk[]" value="'.$row['id'].'" /></td>';  
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "<td><a target=frame2 href='" ."profile.php?user1=". $row['FirstName'] ."'>Check HighScores</a></td>"; 
  echo "<td>" . $row['AddedDate'] . "</td>";
  echo "<td>" . $row['Ip'] . "</td>";
  echo "</tr>";
  }
echo "</tbody>";
echo "</table>";
echo "<hr>";
echo "<input type='submit' name = 'del' value='Delete Selected'></form>";
echo "<input type='button' onclick='checkall(document.myform.delchk);' value='Select All'>";
echo "<input type='button' onclick='uncheckall(document.myform.delchk);' value='UnSelect All'>";

}

else{
// No rows were found ...

echo "No members found";
}

mysql_close($con);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.