Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

149
Visualizações
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 
{}
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

i think u need use ++$_GET['id'] to get increased value.

over 4 years ago · Santiago Trujillo Relatório

0

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;

over 4 years ago · Santiago Trujillo Relatório

0

$_GET values are by default strings when you receive them from frontend. To make it work for your case, you need to convert that to integer first. You can of course make the code more shorter, but since you want to use increment or decrement operators, here is one way:

$minus = intval($_GET['id']);
$plus  = intval($_GET['id']);

--$minus;
$plus++;

You seem to be tempted to do,

$plus  = $_GET['id']++;

Post increment during an assignment is a bad practice, even if you know what you are doing. This can lead to long debugging hours if something goes wrong. You could rather assign the value to a variable and then do the increment or decrement operations as needed.


On further observation, primary key IDs need not be sequential. So, the approach of +1 or -1 won't work. I am keeping my above explanation as is to make you realize those few things.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda