prev next link headache
Posted: Tue Jun 25, 2002 2:34 am
HI list,
I am trying to do a prev next link using php. But i seem to be having a problem and i dont know where went wrong. It would be nice if you guys could comment about it. Any help would be greatly appreciated.
Please bear with the code below cause i am very new at this.
TIA
Braendan
<?php
//
// Connects to sql server and logs in with username and password and select the required database
//
$connect=@mysql_connect("localhost","username","password") or die ("couldnt connect to sql server");
$db= @mysql_select_db("test") or die ("couldnt select database");
//
// This is where you choose the ammount you want to display in one page
//
$per_page = 5;
//
// Selects all of the data from database
//
$sql_text = ("SELECT * from pdf ORDER BY id DESC");
//
// Sets page number, if no page is specified, it will create page 1
//
if(!$page) {
$page = 1;
}
$prev_page = $page - 1;
$next_page = $page + 1;
$query = @mysql_query($sql_text);
// Sets up specified page
$page_start = ($per_page * $page) - $per_page;
$num_rows = @mysql_num_rows($query);
if($num_rows <= $per_page) {
$num_pages = 1;
}
else if (($num_rows % $per_page) == 0) {
$num_pages = ($num_rows / $per_page);
}
else {
$num_pages = ($num_rows / $per_page) + 1;
}
$num_pages = (int) $num_pages;
if (($page > $num_pages) || ($page < 0)) {
error("You have specified an invalid page number");
}
$sql_text = $sql_text . " LIMIT $page_start, $per_page";
$query = mysql_query($sql_text);
?>
<title>Output here</title>
<font face="Tahoma" size="2"> There are currently<font color="red">
<?php echo "$num_rows"; ?> </font> files in this section<p></font>
<?php
while ($result = mysql_fetch_array($query)) {
echo "<p><font face=\"Tahoma\" size=\"2\">Document ID:".$result['id']."</font>
\n";
echo "<font face=\"Tahoma\" size=\"2\">Title:".$result['title']."</font>
\n";
echo "<font face=\"Tahoma\" size=\"2\">File:</font><font face=\"Tahoma\" size=\"2\">".$result['file']."</font></p>\n";
}
// This displays the "Previous" link
if ($prev_page) {
echo "< Prev";
}
// This loops the Pages and displays individual links =
for ($i = 1; $i <= $num_pages; $i++) {
if ($i != $page) {
echo " $i";
} else {
echo " $i ";
}
}
// This displays the "Next" link.
if ($page != $num_pages) {
echo " | Next >" ;
}
?>
I am trying to do a prev next link using php. But i seem to be having a problem and i dont know where went wrong. It would be nice if you guys could comment about it. Any help would be greatly appreciated.
Please bear with the code below cause i am very new at this.
TIA
Braendan
<?php
//
// Connects to sql server and logs in with username and password and select the required database
//
$connect=@mysql_connect("localhost","username","password") or die ("couldnt connect to sql server");
$db= @mysql_select_db("test") or die ("couldnt select database");
//
// This is where you choose the ammount you want to display in one page
//
$per_page = 5;
//
// Selects all of the data from database
//
$sql_text = ("SELECT * from pdf ORDER BY id DESC");
//
// Sets page number, if no page is specified, it will create page 1
//
if(!$page) {
$page = 1;
}
$prev_page = $page - 1;
$next_page = $page + 1;
$query = @mysql_query($sql_text);
// Sets up specified page
$page_start = ($per_page * $page) - $per_page;
$num_rows = @mysql_num_rows($query);
if($num_rows <= $per_page) {
$num_pages = 1;
}
else if (($num_rows % $per_page) == 0) {
$num_pages = ($num_rows / $per_page);
}
else {
$num_pages = ($num_rows / $per_page) + 1;
}
$num_pages = (int) $num_pages;
if (($page > $num_pages) || ($page < 0)) {
error("You have specified an invalid page number");
}
$sql_text = $sql_text . " LIMIT $page_start, $per_page";
$query = mysql_query($sql_text);
?>
<title>Output here</title>
<font face="Tahoma" size="2"> There are currently<font color="red">
<?php echo "$num_rows"; ?> </font> files in this section<p></font>
<?php
while ($result = mysql_fetch_array($query)) {
echo "<p><font face=\"Tahoma\" size=\"2\">Document ID:".$result['id']."</font>
\n";
echo "<font face=\"Tahoma\" size=\"2\">Title:".$result['title']."</font>
\n";
echo "<font face=\"Tahoma\" size=\"2\">File:</font><font face=\"Tahoma\" size=\"2\">".$result['file']."</font></p>\n";
}
// This displays the "Previous" link
if ($prev_page) {
echo "< Prev";
}
// This loops the Pages and displays individual links =
for ($i = 1; $i <= $num_pages; $i++) {
if ($i != $page) {
echo " $i";
} else {
echo " $i ";
}
}
// This displays the "Next" link.
if ($page != $num_pages) {
echo " | Next >" ;
}
?>