Skip to content
Advertisement

Problem making previous next posts with php

I searched everywhere and found out you can use “–” before and “++” after a variable to substract or increase it by 1, but it just wont work properly, here’s my code

$sql=mysqli_fetch_array(mysqli_query($link ,'SELECT max(id) as cnt FROM products'));

$maxid = $sql['cnt'];
$minus = --$_GET['id'];
$plus  = $_GET['id']++;
    
if ($minus == '0')
{}
else 
{echo '<a style="float:left" href="/product/'.$minus.'">Previous offer</a>';}

if ($plus <= $maxid)
{echo '<a style="float:right" href="/product/'.$plus.'">Next offer</a>';}
else 
{}

Advertisement

Answer

You can read more on increment and decrement operators. It basically does not applies to what you want to achieve here using increment and decrement operators. You are using this operators which applies to your $_GET[‘id’] resulting in change in your $_GET[‘id’] itself.

Simply use $minus = $_GET['id']-1; $plus = $_GET['id']+1;

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement