Prev / Next links skip missing database records.
Im trying to make my own blog. Currently I have coded it myself and combined it with phpbb.
Now when someone clicks on a blog post I want to have links to the previouse / next posts.
For example like how wordpress has it.
But note I do not use wordpress I coded this myself so my forum members can comment with there accounts on the blog.
Basically I know how to make a next and previouse link.
But the problem is sometimes I'll need to remove a post ore deleted it.
Now of course if I do this a gap will appear between posts.
So for example this are the records in my database: 1 , 2 , 5 , 7
Now the way I made my next and prev links was to connect to my database select the id and put that idea at the end of my $_GET variables.
index.php?page=($row['id']+1); // next
index.php?page=($row['id']-1); // prev
Now everyone can see that this will not work when I remove a record in the database.
If someone knows how I can solve this ore has a tip please let me know.
You might get someone that recommends renumbering your table, don't do that.
I would do something like this (but there are probably more ways to do it) ...
The links on your page ...
In each page, if the variable "action" exists, that means that they clicked "prev" or "next".
If there is no "action" variable, then they want to display the page specified, so it works as normal.
If they assigned a value to "action", then query it one way for "prev" and another for "next"
Previous ID ...
SELECT * FROM tablename WHERE id < $page ORDER BY id DESC LIMIT 1
Next ID ...
SELECT * FROM tablename WHERE id > $page ORDER BY id ASC LIMIT 1
The query will result in only one row ... which will be the one before or after
the specified page (id) ... depending on "action".
Thanks ones again mlseim, great code.
This thread is solved lol. :)
hello sir i want take 2 database and connect only 1 database inserting values into text box...for eg. if i type 2011 in text box it should go to e2011 database or if i type2012 then it should go to e2012 database so can u do correction and send me plz
1st page is index.php page
<form action="" method="post">
<input type="text" name="year" />
<input type="submit" name="submit" value="Login" />
// this sets variables in the session
$con = mysql_connect($_SESSION['host'],$_SESSION['user'],$_SESSION['pass']) or die("Cannot Connect to Database Server");
die('Could not connect: ' . mysql_error());
mysql_select_db($_SESSION['db'],$con ) or die("database does not exist");
next page is dbconn.php
this page i m connecting to all page
$host = "localhost";
$user = "root";
$pass = "";
if ($_SESSION['year'] == 2011 )
mysql_connect($host,$user,$pass) or die("Cannot Connect to Database Server");
mysql_select_db($db) or die("database does not exist");
|All times are GMT +1. The time now is 05:56 PM.|
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.