How to display data from a specific row in a database without knowing which row it is?

1

Hello. I have while which prints on the screen a list of all the company names in my database:

$sql = "SELECT 'nomep' FROM 'cadastropn' WHERE 'cidade' = '$cidade'";
        $result = mysqli_query($mysqli, $sql);
        while($linha = $result->fetch_assoc()) {
            echo '<div id="lista"><img src="_imagens/pizzaiolo_corpo-300x288.png" class="logo"><a href="javascript:mostrar();"><h2>'.$linha['nomep'].'</h2></a></div>';

Each record of this list, when clicked, causes this article to be shown:

<article class="detalhes">
        <?php
        $sql = "SELECT 'nomep', 'descricao', 'funciona','googlemaps', 'telefone', 'cidade', 'bairro', 'endereco', 'site', 'email', 'facebook', 'googleplus' FROM 'cadastropn' WHERE 'cidade' = '$cidade' && 'id' = '1'";
        $result = mysqli_query($mysqli, $sql);
        while($linha = $result->fetch_assoc()) { (...)

My question is that this list always shows the result of the same item even when I click on others (which is why I indicated the above id) Now how to do the following: I clicked on the first item of the list and in the article details will appear the record of id = '1' . Then I clicked on the second item in the list and in the article details will appear the record of id = '2' , so on.

    
asked by anonymous 07.09.2016 / 22:38

2 answers

0
echo '<div id="lista"><img src="_imagens/pizzaiolo_corpo-300x288.png" class="logo"><a href="javascript:mostrar();"><h2>'.$linha['nomep'].'</h2></a></div>';

Places the identifier "list" as a class, and places the id as the id of the database, like this:

echo "<div class='lista' id=".$linha['id']."><img src="_imagens/pizzaiolo_corpo-300x288.png" class="logo"><a href='javascript:mostrar();'><h2>'.$linha['nomep'].'</h2></a></div>";

Now just get the id of the element by javascript at the moment of the click and it happens in your query.

It would look like this in a very simple way:

 echo "<div class='lista' id=".$linha['id']."onclick='mostrar(this.id);'"> 

Use the function of the other answer that gives right. ....

function mostrar(id){

         var url = document.URL + "?id=" + id;

}

And finally your php

<article class="detalhes">
        <?php
        $id = $_GET["id"];
        $sql = "SELECT 'nomep', 'descricao', 'funciona','googlemaps', 'telefone', 'cidade', 'bairro', 'endereco', 'site', 'email', 'facebook', 'googleplus' FROM 'cadastropn' WHERE 'cidade' = '$cidade' && 'id' = '$id'";
        $result = mysqli_query($mysqli, $sql);
        while($linha = $result->fetch_assoc()) { (...)
    
08.09.2016 / 02:57
0

In this case, you have to pass the variable $id , its query , where it is, id = 1 , pass the variable, id = $id . For example: $cidade in its SELECT, is a value variable, coming from somewhere that is not in your code, do the same with the "id" field.

By Javascript

Replace this snippet:

 echo '<div id="lista"><img src="_imagens/pizzaiolo_corpo-300x288.png" class="logo"><a href="javascript:mostrar();"><h2>'.$linha['nomep'].'</h2></a></div>';

by

 echo '<div id="lista"><img src="_imagens/pizzaiolo_corpo-300x288.png" class="logo"><a id="linkq" href="<?php echo $_SERVER['PHP_SELF']; ?>" onclick="javascript:mostrar();"><h2>'.$linha['nomep'].'</h2><h2 id="id">'.$linha['id'].'</h2></a></div>';

Your function mostrar() looks like this:

function mostrar(){
         var id = document.getElementById("id").innerHTML;
         var el = document.getElementsByClassName("detalhes");
         el[0].classList.toggle("show");

         var url = document.URL + "?id=" + id;
         var element = document.getElementById('linkq');
             element.setAttribute("href",url);
}

And finally your php

<article class="detalhes">
        <?php
        if(isset($_GET["id"])){
        $id = $_GET["id"];
        $sql = "SELECT 'nomep', 'descricao', 'funciona','googlemaps', 'telefone', 'cidade', 'bairro', 'endereco', 'site', 'email', 'facebook', 'googleplus' FROM 'cadastropn' WHERE 'cidade' = '$cidade' && 'id' = '$id'";
        $result = mysqli_query($mysqli, $sql);
            while($linha = $result->fetch_assoc()) { (...)}
        }

Removing the "ugly" item from the div:

no css is placed

#id{display:none;}

Using $ _SESSIONS

session_start();
$sql = "SELECT 'nomep' FROM 'cadastropn' WHERE 'cidade' = '$cidade'";
        $result = mysqli_query($mysqli, $sql);
        while($linha = $result->fetch_assoc()) {
            $_SESSION["id"] = $linha['id'];
            echo '<div id="lista"><img src="_imagens/pizzaiolo_corpo-300x288.png" class="logo"><a href="javascript:mostrar();"><h2>'.$linha['nomep'].'</h2></a></div>';

And the other part

<article class="detalhes">
        <?php
        $id = $_SESSION["id"];
        $sql = "SELECT 'nomep', 'descricao', 'funciona','googlemaps', 'telefone', 'cidade', 'bairro', 'endereco', 'site', 'email', 'facebook', 'googleplus' FROM 'cadastropn' WHERE 'cidade' = '$cidade' && 'id' = '$id'";
        session_destroy();
        $result = mysqli_query($mysqli, $sql);
        while($linha = $result->fetch_assoc()) { (...)}
    
07.09.2016 / 23:30