Plz help--Links dont work when i display contents from table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Kingo
Forum Contributor
Posts: 146
Joined: Thu Jun 03, 2004 9:38 am

Plz help--Links dont work when i display contents from table

Post by Kingo »

Only the first 5 rows of the table gets displayed....Any help is really appreciated


<html>
<head>
<title>Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
$hostname = 'localhost';
$username ='abc';
$password ='123';
$dbName = 'phptest';

MYSQL_CONNECT($hostname, $username, $password) OR DIE("Unable to connect");
@MYSQL_SELECT_DB("$dbName") OR DIE("Unable to select database");

$rows_per_page = 5;

$result = mysql_query("SELECT * FROM header");

$total_records = mysql_num_rows($result);

$pages = ceil($total_records / $rows_per_page);

mysql_free_result($result);


if (!isset($screen))
$screen = 0;
$start = $screen * $rows_per_page;

$result = mysql_query("SELECT Name FROM header LIMIT $start, $rows_per_page");

$rows = mysql_num_rows($result);

for ($i = 0; $i < $rows; $i++) {
$Name = mysql_result($result, $i, 0);
echo "$Name<br>\n";
}
echo "<p><hr></p>\n";
// let's create the dynamic links now
if ($screen > 0) {
$url = "example.php?screen=" . $screen - 1;
echo "<a href=\"$url\">Previous</a>\n";
}
// page numbering links now
for ($i = 0; $i < $pages; $i++) {
$url = "example.php?screen=" . $i;
echo " | <a href=\"$url\">$i</a> | ";
}
if ($screen < $pages) {
$url = "example.php?screen=" . $screen + 1;
echo "<a href=\"$url\">Next</a>\n";
}
?>
</body>
</html>
reverend_ink
Forum Contributor
Posts: 151
Joined: Sun Apr 20, 2003 1:18 am
Location: Las Vegas | London

Post by reverend_ink »

Here you go

You were missing a ();

<html>
<head>
<title>Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
$hostname = 'localhost';
$username ='abc';
$password ='123';
$dbName = 'phptest';

MYSQL_CONNECT($hostname, $username, $password) OR DIE("Unable to connect");
@MYSQL_SELECT_DB("$dbName") OR DIE("Unable to select database");

$rows_per_page = 5;

$result = mysql_query("SELECT Name FROM header ");

$total_records = mysql_num_rows($result);

$pages = ceil($total_records / $rows_per_page);

mysql_free_result($result);


if (!isset($screen))
$screen = 0;
$start = $screen * $rows_per_page;

$result = mysql_query("SELECT Name FROM header LIMIT $start, $rows_per_page");

$rows = mysql_num_rows($result);

for ($i = 0; $i < $rows; $i++) {
$Name = mysql_result($result, $i, 0);
echo "$Name<br>\n";
}
echo "<p><hr></p>\n";
// let's create the dynamic links now
if ($screen > 0) {
$url = "example.php?screen=" . ($screen - 1); //was missing the () here
echo "<a href=\"$url\">Previous</a>\n";
}
// page numbering links now
for ($i = 0; $i < $pages; $i++) {
$url = "example.php?screen=" . $i;
echo " | <a href=\"$url\">$i</a> | ";
}
if ($screen < $pages) {
$url = "example.php?screen=" . ($screen + 1); //was missing the () here
echo "<a href=\"$url\">Next</a>\n";
}
?>
</body>
</html>
Kingo
Forum Contributor
Posts: 146
Joined: Thu Jun 03, 2004 9:38 am

Post by Kingo »

Thanx very much..........but the links still doesnt work..i mean when i click the second page link..the data doesnt get displayed..i still see the first page data
reverend_ink
Forum Contributor
Posts: 151
Joined: Sun Apr 20, 2003 1:18 am
Location: Las Vegas | London

Post by reverend_ink »

Just checked it with a table oif mine that had 19 entries.

Use this as it does work

<html>
<head>
<title>Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
$hostname = 'localhost';
$username ='abc';
$password ='123';
$dbName = 'phptest';

MYSQL_CONNECT($hostname, $username, $password) OR DIE("Unable to connect");
@MYSQL_SELECT_DB("$dbName") OR DIE("Unable to select database");

$rows_per_page = 5;

$result = mysql_query("SELECT Name FROM header ");

$total_records = mysql_num_rows($result);

$pages = ceil($total_records / $rows_per_page);

mysql_free_result($result);


if (!isset($screen))
{
$screen = 0; // Everything was being reset to 0
}
$start = $screen * $rows_per_page;

$result = mysql_query("SELECT Name FROM header LIMIT $start, $rows_per_page");

$rows = mysql_num_rows($result);

for ($i = 0; $i < $rows; $i++) {
$Name = mysql_result($result, $i, 0);
echo "$Name<br>\n";
}
echo "<p><hr></p>\n";
// let's create the dynamic links now
if ($screen > 0) {
$url = "example.php?screen=" . ($screen - 1); //was missing the () here
echo "<a href=\"$url\">Previous</a>\n";
}
// page numbering links now
for ($i = 0; $i < $pages; $i++) {
$url = "example.php?screen=" . $i;
echo " | <a href=\"$url\">$i</a> | ";
}
if ($screen < $pages) {
$url = "example.php?screen=" . ($screen + 1); //was missing the () here
echo "<a href=\"$url\">Next</a>\n";
}
?>
</body>
</html>
reverend_ink
Forum Contributor
Posts: 151
Joined: Sun Apr 20, 2003 1:18 am
Location: Las Vegas | London

Post by reverend_ink »

If you need please feel free to IM me and I will help you implement this and get it working.
Kingo
Forum Contributor
Posts: 146
Joined: Thu Jun 03, 2004 9:38 am

Post by Kingo »

I used your code..but still cant get it to work
I still get the first page displayed even if i click the second, third, fourth url's...Your help is really appreciated
Kingo
Forum Contributor
Posts: 146
Joined: Thu Jun 03, 2004 9:38 am

Post by Kingo »

Or is there any other code you can give it to me...which connects the table in a databse and displayes the rows page wise.
Kingo
Forum Contributor
Posts: 146
Joined: Thu Jun 03, 2004 9:38 am

Post by Kingo »

Or is there any other code you can give it to me...which connects the table in a databse and displayes the rows page wise.
Post Reply