How do I view data from a specific line in a database without knowing which line it is?

Asked

Viewed 796 times

1

Hello.
I have a while that prints on the screen a list of all the names of the companies 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 in this list, when clicked, makes this article 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 expected because I indicated the id above) 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 of the list and in the article details will appear the record of id = '2', thus successively.

  • I advise you to use javascript and get the id of the element you clicked, and this id should be the same as the one in the database.

  • @Nayronmorais Can exemplify?

2 answers

0

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

By Javascript

Replace this excerpt:

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

for

 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() gets away with it:

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:

in css if put

#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()) { (...)}

0

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

Sets the identifier "list" as class, and puts the id as the database id, this way:

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 take the id of the element by javascript at the time of click and pass 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()) { (...)
  • I am very new to javascript. How would this function of javascript?

Browser other questions tagged

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