/    Sign up×
Community /Pin to ProfileBookmark

mysql_fetch

hi all,
Can anybody please give me an idea to display the database values, 5 rows in the first page and the next 5 rows as in the 2nd page.. etc..

thanks in advance..

to post a comment
PHP

15 Comments(s)

Copy linkTweet thisAlerts:
@GarySJul 04.2006 — The key to this will be the use of LIMIT in your SQL. So...

SELECT * FROM item LIMIT 0,5

...will bring back the first 5 results.

SELECT * FROM item LIMIT 5,5

...will bring back the next 5 results


* * * * * *

Next challenge will be to set up your paging. A query string is the usual way to do this;

results.php?page=3

You can test for the value of $_GET['page'] and work out the number of the first result you need from there. Something like:

[code=php]
$page=$_GET['page'];
$resultsPerPage=5;
$startResult=($page-1) * $resultsPerPage;

$sql="SELECT * FROM item LIMIT $startResult,$resultsPerPage";
[/code]



* * * * *

Hope that's enough to get you going.
Copy linkTweet thisAlerts:
@jenyauthorJul 05.2006 — thank you GaryS..

i got ur point...
Copy linkTweet thisAlerts:
@GarySJul 05.2006 — Post pack if you have problems.
Copy linkTweet thisAlerts:
@jenyauthorJul 05.2006 — hi,

i now have a problem how to get the $_get[page].if i use forms it will be not great. i like to use the link(< a href="") but when i use this, it will not pass any values. please help me GaryS....

thanks in advance...
Copy linkTweet thisAlerts:
@GarySJul 05.2006 — You'll be dynamically generating the link - and passing the value in the querystring. Here's the kind of thing you'll be aiming for:

<a href="results.php?[b]page=3[/b]">Page 3</a>
Copy linkTweet thisAlerts:
@jenyauthorJul 05.2006 — i can get u but see the following code

<?php

session_start();

if ($_SESSION['name'])

{

include('welcome.php');

include('mysql_connect.php');

$page=$_
GET['page'];

$resultsPerPage=5;

//SELECT * FROM item LIMIT 0,5

//SELECT *
FROM item LIMIT 5,5

$startResult=($page-1) * $resultsPerPage;

$sql=mysql_query("SELECT *
FROM announcement LIMIT $startResult,$resultsPerPage");

echo "<table border=1 align="center">";

while ($row=mysql_fetch_array($sql))

{

echo"<tr>";

echo"<td><font color="blue">".$row[0]."</font></td>";

echo"<td><font color="blue">".$row[1]."</font></td>";

echo"<td><font color="blue"><a href="view_announce.php?no=$row[0]">".$row[2]."</a></font></td>";

//echo"<td>".$row[3]."</td>";

echo "</tr>";

$row[0]++;

}

echo "</table>";

echo "<a href="view_ann.php?page=3">Page 3</a>";

mysql_close($q1);

}

else

echo "<meta http-equiv=refresh content=0;url=first.php?error=1>";

?>


here i dont know how to make the link to go to the particular page and how to display the first 5 rows as the default...

i am getting warning as

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:wampwwwview_ann.php on line 15

Page 3

help me GaryS...

Thanks in advance....
Copy linkTweet thisAlerts:
@GarySJul 05.2006 — Give this a try (untested, so may need some tweaking)

[code=php]
<?php
session_start();
if ($_SESSION['name'])
{
include('welcome.php');
include('mysql_connect.php');



//Test for the GET
if(!isset($_GET['page']) ){

$page=1;

}
else{

$page=$_GET['page'];

}


$resultsPerPage=5;
//SELECT * FROM item LIMIT 0,5
//SELECT * FROM item LIMIT 5,5
$startResult=($page-1) * $resultsPerPage;
$sql=mysql_query("SELECT * FROM announcement LIMIT $startResult,$resultsPerPage");
echo "<table border=1 align="center">";
while ($row=mysql_fetch_array($sql))
{
echo"<tr>";
echo"<td><font color="blue">".$row[0]."</font></td>";
echo"<td><font color="blue">".$row[1]."</font></td>";
echo"<td><font color="blue"><a href="view_announce.php?no=$row[0]">".$row[2]."</a></font></td>";
//echo"<td>".$row[3]."</td>";
echo "</tr>";
$row[0]++;
}
echo "</table>";

//several ways to do the "list of links" - the following is inelegant... plenty of room for improvement!

$previous=$page-1;

if($previous > 0){
echo "<a href="view_ann.php?page=$previous">Previous</a>";
}
//Current page - no link
echo "Page $page";

$next=$page+1;
echo "<a href="view_ann.php?page=$next">Next</a>";



mysql_close($q1);
}
else
echo "<meta http-equiv=refresh content=0;url=first.php?error=1>";
?>
[/code]
Copy linkTweet thisAlerts:
@jenyauthorJul 05.2006 — it's working but if there is 20 records it displays the record and the "next" and "back" link. after displaying last 5 records (i.e is 15-20) it also displays "next" link. i dont want to display that. Do i want to check end of the table?

Waiting for ur reply..

thanks in adance....
Copy linkTweet thisAlerts:
@GarySJul 05.2006 — Knew that would be the next question ?

You're right: to do a proper job of this, you need to know the total number of results. Easiest would be to do a COUNT(*) kind of query. Once you have the result count, you put in place the appropriate condition for the display of "next"
Copy linkTweet thisAlerts:
@jenyauthorJul 05.2006 — if my maximum record counted as 27, then, should i calculate the no.of 5's and how many pages to display? or is there is any other way? also with the above code is there is any way to get me an answer?

Thanks in advance....
Copy linkTweet thisAlerts:
@GarySJul 05.2006 — Something like this should work:

if ( ($totalResults/$resultsPerPage) > $page) {

$next=$page+1;

echo "<a href="view_ann.php?page=$next">Next</a>";

}

(Untested)
Copy linkTweet thisAlerts:
@jenyauthorJul 05.2006 — it works out... thanks a lot GaryS...

my code now looks like this

<?php

session_start();

if ($_SESSION['name'])

{

include('welcome.php');

include('mysql_connect.php');

//Test for the GET

if(!isset($_
GET['page']) ){

$page=1;

}

else{

$page=$_GET['page'];

}

$sql=mysql_query("SELECT * FROM announcement");

$totalResults=mysql_num_rows($sql);

//echo $totalResults;

$resultsPerPage=5;

//SELECT *
FROM item LIMIT 0,5

//SELECT * FROM item LIMIT 5,5

$startResult=($page-1) *
$resultsPerPage;

$sql=mysql_query("SELECT * FROM announcement LIMIT $startResult,$resultsPerPage");

echo "<table border=1 align="center">";

while ($row=mysql_fetch_array($sql))

{

echo"<tr>";

echo"<td><font color="blue">".$row[0]."</font></td>";

echo"<td><font color="blue">".$row[1]."</font></td>";

echo"<td><font color="blue"><a href="view_announce.php?no=$row[0]">".$row[2]."</a></font></td>";

//echo"<td>".$row[3]."</td>";

echo "</tr>";

$row[0]++;

}

echo "</table>";

//several ways to do the "list of links" - the following is inelegant... plenty of room for improvement!

$previous=$page-1;

if($previous > 0){

echo "<a href="view_ann.php?page=$previous">Previous</a>";

}

//Current page - no link

echo "Page $page";

/*$next=$page+1;

echo "<a href="view_ann.php?page=$next">Next</a>"; *
/

if ( ($totalResults/$resultsPerPage) > $page) {

$next=$page+1;

echo "<a href="view_ann.php?page=$next">Next</a>";

}

mysql_close($q1);

}

else

echo "<meta http-equiv=refresh content=0;url=first.php?error=1>";

?>
Copy linkTweet thisAlerts:
@GarySJul 05.2006 — Excellent!
Copy linkTweet thisAlerts:
@jenyauthorJul 05.2006 — can u please tell some site for open projects
×

Success!

Help @jeny spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...