prev next link headache
Moderator: General Moderators
prev next link headache
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 >" ;
}
?>
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
hrmm its in there when i pasted it, but when i submit this form all the <a href=""</a> went missing
Code: Select all
<?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><br>\n";
echo "<font face="Tahoma" size="2">Title:".$resultї'title']."</font><br>\n";
echo "<font face="Tahoma" size="2">File:</font><a href="$file" target="_blank"><font face="Tahoma" size="2">".$resultї'file']."</font></a></p>\n";
}
// This displays the "Previous" link
if ($prev_page) {
echo "<a href="$PHP_SELF?page=$prev_page">< Prev</a>";
}
// This loops the Pages and displays individual links =
for ($i = 1; $i <= $num_pages; $i++) {
if ($i != $page) {
echo " <a href=$PHP_SELF?page=$i>$i</a>";
} else {
echo " $i ";
}
}
// This displays the "Next" link.
if ($page != $num_pages) {
echo " |<a href="$PHP_SELF?page=$next_page"> Next ></a>" ;
}
?>
</body>
</html>
Last edited by Braendan on Tue Jun 25, 2002 3:25 am, edited 1 time in total.
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Had a play with your code and the following worked fine for one of my databases:
Mac
Code: Select all
<?php
// Connects to sql server and logs in with username and password and select the required database
$connect = @mysql_connect('localhost', 'user', 'pass') or die ('couldnt connect to sql server');
@mysql_select_db('db') or die ('couldnt select database');
// This is where you choose the amount you want to display in one page
$per_page = 5;
// Selects all of the data from database
$sql_text = "SELECT * FROM table ORDER BY ID DESC";
// Sets page number, if no page is specified, it will create page 1
if(!isset($_GETї'page'])) {
$page = 1;
} else {
$page = $_GETї'page'];
}
$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;
} elseif (($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) {
echo '<p>You have specified an invalid page number.</p>';
}
echo $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 ($info = mysql_fetch_assoc($query)) {
echo <<<END
\n<p><font face="Tahoma" size="2">Document ID: {$infoї'ID']}</font>
<font face="Tahoma" size="2">Title: {$infoї'title']}</font>
<font face="Tahoma" size="2">File: {$infoї'file']}</font></p>\n
END;
}
// This displays the "Previous" link
if ($prev_page != 0) {
echo '<a href="test.php?page='.$prev_page.'">< Prev</a>';
}
// This loops the Pages and displays individual links =
for ($i = 1; $i <= $num_pages; $i++) {
if ($i != $page) {
echo ' <a href="test.php?page='.$i.'">'.$i.'</a> ';
} else {
echo ' '.$i.' ';
}
}
// This displays the "Next" link.
if ($page != $num_pages) {
echo ' |<a href="test.php?page='.$next_page.'"> Next ></a>';
}
?>