Im having problems with a method in a class file:
public static function getPageLeft($pid)
{
self::_dbConnect();
echo "now " .$pid;
$pid--;
echo " after ". $pid;
$data = mysql_query("SELECT id FROM evening_pages WHERE id='".$pid."'") or die(mysql_error());
$rows = mysql_num_rows($data);
if($rows == 1){
echo " in ";
return $data;
}elseif($pid != self::getMinPage()){
self::getPageLeft($pid);
echo " Current: " . $pid . " min: " .self::getMinPage();
}
}
I have the echos in there for debug only, and im getting:
now 22 after 21now 21 after 20 Current: 21 min: 1
This code is to find the next page left in a database driven CMS in case the client deletes a row from the database, it finds the next lower value.
So page id is 22 that your on, looks for id 21 in database, if its not there it should return 0 for the rows and move on and try again but on id 20, but there IS an entry for id = 20.
Anyone spot any issues with the code?
Also getting this error when it tries to find a page which doesnt exist before the current page, eg on page 22 but there is no ID for 21:
Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in /home/prelas/public_html/index.php on line 37
the code there is (line 37 is while):
$getPageLeft = sql::getPageLeft($pid);
while($row = mysql_fetch_array($getPageLeft)){
$pageleft = $row['id'];
}
Many thanks.
Just a quick edit: ” in ” never gets echoed, but yet when there is no deleted pages the navigation works fine.
Further edit: The code which makes use of the code (by like 37):
$navigation = '<div id="direction">
<span class="inNav"> <a href="index.php?pid='. $min .'"><<</a> <a href="index.php?pid='. $pageleft .'"><</a> </span><span class="black">•</span><span class="inNav"> <a href="index.php?pid='. $pageright .'">></a> <a href="index.php">>></a></span>
</div>'
So you can see it uses $pageleft in there however when its dealing with blank pages the value is nothing (empty).
why don’t you write the SQL statement to get the next lower ID? For example:
Or:
That way, you only have to check if something has been returned by the query. Less code, less overhead for database requests.
The mysql extension is being phased out. Learn about using mysqli or PDO, and read up on the dangers of SQL injection.