Project Search PHP

Asked

Viewed 80 times

1

I have the following lines of a Search Project using the GET method I am trying to implement a second field in INPUT more unsuccessfully :(

Someone please help me insert a new field so that the search is done in 2 columns at the same time?

This is the code

<?php header("Content-Type: text/html; charset=ISO-8859-1",true);?>
<?php include_once("conexao.php");
//Verificar se está sendo passado na URL a página atual, senao é atribuido a pagina 
$pagina = (isset($_GET['pagina']))? $_GET['pagina'] : 1;
if(!isset($_GET['pesquisar']) && ($_GET['pesquisar1']) ) 
{
    header("Location: index.php");
}else{
    $valor_pesquisar = $_GET['pesquisar'];  
}

//Selecionar todos os cursos da tabela
$result_curso = "SELECT * FROM cursos WHERE nome  LIKE '%$valor_pesquisar%' AND conteudo LIKE '%$valor_pesquisar1%' ";
$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;



//Todas as Mensagens

    $result_cursos = "SELECT * FROM cursos WHERE nome LIKE '%$valor_pesquisar%' AND conteudo LIKE '%$valor_pesquisar1%'";
    $resultado_cursos = mysqli_query($conn , $result_cursos);



//Contar o total de itens
    $total_cursos = mysqli_num_rows($resultado_cursos);

?>

<!DOCTYPE html>
<html lang="pt-br">
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Criar pagina com abas</title>
        <link href="css/bootstrap.min.css" rel="stylesheet">
        <link href="css/style.min.css" rel="stylesheet">
        <link href="style.css" rel="stylesheet">
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
           <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>  
    </head>
    <body>
        <div class="container theme-showcase" role="main">
            <div class="page-header">
                <button class="btn btn-success" type="button">
                <span class="badge"><?php echo $total_cursos; ?></span> encontrados em <b><?php echo $valor_pesquisar; ?></b>
            </button>
                <div class="row">

                    <div class="col-sm-6 col-md-6">
                        <h1>Cursos</h1>
                    </div>
                    <div class="col-sm-6 col-md-6">
                        <form class="form-inline" method="GET" action="pesquisar.php">
                            <div class="form-group">
                                <label for="exampleInputName2">Pesquisar</label>
                                <input type="text" name="pesquisar" class="form-control" id="exampleInputName2" placeholder="Digitar...">
                                <input type="text" name="pesquisar1" class="form-control" id="exampleInputName2" placeholder="Digitarddd...">
                            </div>
                            <button type="submit" class="btn btn-primary">Pesquisar</button>
                        </form>
                    </div>
                </div>
            </div>


            <?php

            $total_cursos = mysqli_num_rows($resultado_cursos);
                if(mysqli_num_rows($resultado_cursos) <= 0){
                        echo '<div class="alert alert alert-info alert-dismissible" role="alert">
                              <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                              <span aria-hidden="true">&times;</span></button>
                              <strong>Ops!</strong> Não encontramos oque procurava, tente denovo!
                              </div>';
                        }
            ?>
            <div class="row">
                <?php while($rows_cursos = mysqli_fetch_assoc($resultado_cursos)){ ?>
                    <div class="col-sm-6 col-md-4">
                        <div class="thumbnail">
                            <img src="imagens/produto.jpg" alt="...">
                            <div class="caption text-center">
                                <a href="detalhes.php?id_curso=<?php echo $rows_cursos['id']; ?>"><h3><?php echo $rows_cursos['nome']; ?></h3></a>
                                <p><a href="#" class="btn btn-primary" role="button">Comprar</a> </p>
                            </div>
                        </div>
                    </div>
                <?php } ?>
            </div>

                    </li>
                </ul>
            </nav>
        </div>

        <script type="text/javascript">
  window.setTimeout(function() {
    $(".alert").fadeTo(500, 0).slideUp(500, function(){
        $(this).remove(); 
    });
}, 2500);                                       
    </script>
        <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
        <!-- Include all compiled plugins (below), or include individual files as needed -->
        <script src="js/bootstrap.min.js"></script>
    </body>
</html>
  • 1

    Be clearer in your question and restrict the code to content relevant to a [MCVE]

2 answers

2

Wesley Roberto, you are not assigning the value to the searched value1 variable

}Else{

$valor_pesquisar = $_GET['pesquisar'];  

$valor_pesquisar1 = $_GET['pesquisar1'];

}

  • You are passing only one of the form inputs and you need both.

  • how do I do this ??

  • just add a new variable and use a new $_get to pick up the value of the input!

  • Yes I just do however is not working, it even records the term of the second input but does not return the result, ex if I search for Wesley (search) and Roberto on (search 1), it only presents the results Wesley. understands?

  • that is even if there are 10 results by Wesley he will only return as answer only those that St.Wesley Robert.

  • @Wesleyroberto but isn’t that what you want? Just the records that are Wesley and Roberto at the same time?

  • No, I want him to look for Wesley in the NAME column and in the LAST column (which in my project is as content) Roberto. and will only introduce me as a result the names that form Wesley Roberto, understood?

  • @Wesleyroberto understood now.

Show 3 more comments

2


Wesley,

You are using in form one GET which is a method of sending a form that is faster than the post but has visibility through the browser, if you do not want anyone to see use post but the response is a little more time consuming due to its encapsulation.

  • To use the GET and only you declare the $_GET['name of the "NAME"'].

A golden tip does not use the like in your trial because it is slower. in this situation I would use so:

  • "SELECT * FROM courses WHERE name = "$_GET[search]" AND content = "$_GET[search1]"

Because then it would be simpler, faster your query and would not weigh to the database.

  • This way this giving error in the SELECT line

  • We had to concatenate the $_GET and put quotes as they are strings

  • I still could not, by my code here it seems that he is not able to search for the content, for example I want you to do a search in the search and search1 and return only the results that have searched the terms..

  • sends the error here

  • compares with a code from me

  • $query = "SELECT * FROM products"; $result = mysqli_query($Conn, $query); while ($line = mysqli_fetch_assoc($result)) { echo "$line[product name]"}

  • my select did so $user = $_POST['user']; $pass = $_POST['password']; $query = "SELECT username, user_user, password_user, access_user FROM users WHERE usuario_user = '$user' AND password_user = '$pass'";

  • Are you testing the procedures in the database ? which error in the database ? , and which error you present in php ?

  • when it does not present on the screen error in php is error in the database I to manage my mysql I use heidisql

  • I managed using your tip and changing some parameters. valeuuuu

Show 5 more comments

Browser other questions tagged

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