Please Help with this problem

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
leebo
Forum Commoner
Posts: 44
Joined: Sun Oct 20, 2002 9:49 am

Please Help with this problem

Post by leebo »

Hi

I am new to php and i`m enjoying learning it.

I have just started on databases and come across a slight problem:

I need to display records of 10 the have a next and prev button to display the remainder of the records.

I have seached all way through this forum but the code i have come across is all Chinese to me - here is the code i have so far:

<?
$server= "myserver";
$user= "myaccount";
$password= "pass";
$database= "database1";
$table= "table1";
MYSQL_CONNECT($server, $user, $password) or die ( "<H3>Server unreachable</H3>");
MYSQL_SELECT_DB($database) or die ( "<H3>Database non existent</H3>");
$result=MYSQL_QUERY( "SELECT * FROM $table order by name ");

if(mysql_num_rows($result)) {
// it is true, so let's print the results to the browser
while($row = mysql_fetch_row($result))
{

print (" $row[0] "); ?><? print (" $row[1] "); ?><? print (" $row[2] ");
?>


<?
}
} else {
// false, no results
}

?>

thats the code i am using so could someone please help me where to put the code for displaying the next records etc...

Thank you very much

Lee
The PHP Newbie !
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

here is a tutorial that is easy to follow, provides a simple script structure that is quite expandable: http://codewalkers.com/tutorials.php?show=4
leebo
Forum Commoner
Posts: 44
Joined: Sun Oct 20, 2002 9:49 am

Post by leebo »

Ok i have manged to get the next previous buttons working but say 20 records are shown then when clicking the next button theres no more records to show ! so i need not to display the next button after the last record is shown - can anyone help me with this problem ?

The code i`m using is this:


$prevlimit=$limit-5;


if ($prevlimit<0) $prevlimit=0;
if ($limit > 4 )

echo "<a href=\"$_SERVER[PHP_SELF]?search=$search&limit=$prevlimit\>previous 5</a>";
else
echo " ";

$nextlimit=$limit+5;
if ($limit < $no_of_records )

echo "<a href=\"$_SERVER[PHP_SELF]?search=$search&limit=$nextlimit\">next 5</a>";
else
echo " ";
?>

Thanks
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

the query

Code: Select all

SELECT count(*) FROM tablename
will return the number of records in tablename. You can use this to determine the upper limit.
leebo
Forum Commoner
Posts: 44
Joined: Sun Oct 20, 2002 9:49 am

Post by leebo »

sorry but i dont understand where to add this ?? :? i`m new to php and databases and learning it quite frustrating :oops:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

it's explained in the last section of the tutorial mydimension pointed you to ;)
http://codewalkers.com/tutorials/4/5/
Post Reply