Values in PHP search

Asked

Viewed 50 times

-3

I have the following arguments:

//passo o valor para a variavel sómente se o get existir
$valor_pesquisar = isset($_GET['pesquisar'])?isset($_GET['pesquisar']):'';
if(!isset($_GET['pesquisar'])){

}else{
    $valor_pesquisar = $_GET['pesquisar'];
}

//Selecionar todos os cursos da tabela
$result_curso = "SELECT * FROM produtos WHERE prod_nome LIKE '%$valor_pesquisar%'";
$resultado_curso = mysqli_query($conn, $result_curso);

//Contar o total de cursos
$total_cursos = mysqli_num_rows($resultado_curso);

//Seta a quantidade de cursos por pagina
$quantidade_pg = 6;

//calcular o número de pagina necessárias para apresentar os cursos
$num_pagina = ceil($total_cursos/$quantidade_pg);

//Calcular o inicio da visualizacao
$incio = ($quantidade_pg*$pagina)-$quantidade_pg;

//Selecionar os cursos a serem apresentado na página
$result_cursos = "SELECT * FROM produtos WHERE prod_nome LIKE '%$valor_pesquisar%' limit $incio, $quantidade_pg";
$resultado_cursos = mysqli_query($conn, $result_cursos);
$total_cursos = mysqli_num_rows($resultado_cursos);

I wonder how to add a echo when the search found no result.

  • 2

    When you say "Find no results", you mean if the $_GET['pesquisar'] be empty?

  • Not when in case you do the search and find no result in the search.

  • this one above is the use to show my table and tbm search, I can search normal but if I look for something I don’t have it returns the empty page I wanted to put an echo saying that did not find result. http://imperionutry.esy.es

  • vlw, I’m already going to study that stop right here.. so I don’t have any problems in the future

1 answer

0


Something like that?

if ($total_cursos != 0) {
    // Se houver cursos, fazer ação
} else {
    // Caso contrário, echo.
    echo "Nenhum curso encontrado";
}
  • yes but do not know where increment because it does not work .

  • At the end of the code. If it’s not too much to ask, you could post the part of the code where it prints the results?

  • 1

    is through a While but I’ve hit here with this solution that passed me, po Thanks same guy was breaking my head, I made the site based on studies on youtube and I’m taking these beatings ai sorry the work expensive.

  • @Léoandrade Magina hahaha One more thing, take care of SQL Injection. You should prepare the query for query. If I search for ', your code will return an error that can be used to delete your database, so be careful.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.