0

I've developed an PHP page that show all the itens from an MySQL table. Now I wanna show an specific item when the user click's one of the itens on this page. How do I get this specific ID an how do I configure the <a href=""> link?

UPDATING THE QUESTION:

This is the header of cars.php page (with all results):

<?

require("admin/db/connect.php");

$sql = "SELECT * FROM tb_carros";

$limite = mysql_query("$sql");

$dados = array();
while ($sql = mysql_fetch_array($limite) ) {
    $dados[] = $sql;
}

?>

and the HTML:

    <?php foreach ($dados as $row): ?>

    <div id="containerResumo">

        <a href="#"> <!-- this is the key problem -->

        <div class="dadosResumo">
            <?=$row['carro']?><br /><br />
            Ano:   <?=$row['ano']?><br /><br />
            Câmbio: <?=$row['cambio']?><br /><br />
            R$ <?=$row['valor']?>
        </div><!-- END of dadosItem -->
        </a>

    </div><!-- END of containerResumo -->

    <?php endforeach ?>

Now, when the user click on an item I wanna open the page carro_item.php with the data of the item loaded on it.

The reference ID from the database is id_carro

I tried many types of code but it didn't work. Even if I put the full url on the browser the data isn't load:

http://adrianomachado.com/testesClientes/carro_item.php?id_carro=1

This is the PHP of carro_item.php:

<?

require("admin/db/connect.php");

$id = (int)$_GET['id_carro'];

$sql = "SELECT * FROM tb_carros WHERE id = $id";

?>

And the HTML:

            <div class="dadosItem">
                R$ <?php $valor ?><br /><br />
                Ano:   <?php $ano ?><br /><br />
                Kilometragem: <?php $km ?><br /><br />
                Cor: <?php $cor ?><br /><br />
                Portas: <?php $portas ?><br /><br />
                Combustível: <?php $combustivel ?><br /><br />
                Câmbio: <?php $cambio ?><br /><br />
                Final da placa: <?php $final_placa ?><br /><br />
                Carroceria: <?php $carroceria ?>
            </div><!-- END of dadosItem -->

Any help?

UPDATE 02:

This is the query in carro_item:

<?

require("admin/db/connect.php");

$sql = "SELECT * FROM tb_carros";

$limite = mysql_query("$sql");

$dados = array();
while ($sql = mysql_fetch_array($limite) ) {
    $dados[] = $sql;
}


?>

But, obviously it returns all the results like the cars.php page. The question is how do I filter the results to the same ID of the link that the user clicked?

I don't know how to code the $sql = "SELECT * FROM tb_carros"; line to do that.

2 Answers2

1

You could make use of $_GET variables.

If the link was formatted like this on the page:

<a href="mypage.php?id=5">

Then in your php page, you would be able to access this value via the global $_GET array.

$id = mysqli_real_escape_string($_GET['id']); // $id will have the value passed to it by the link

Be careful not to leave yourself open to SQL Injection though by either sanitizing your arguments or using a parameterized query.

References

Edit:

To create the links formatted in the correct way, you'd first retrieve all of the ids you need and store them in an array. I'll use $ids as an example.

$ids = array(1, 50, 25, 62, ...); // This was populated from the database

// Loop through all ids and output link code for each one
foreach ($ids as $link_id) {
  echo '<a href="mypage.php?id=' . $link_id . '">Click me</a>';
}

Edit2:

In cars.php format the links like this:

<a href="/testesClientes/carro_item.php?id_carro=<?= $row['id'] ?>">

Edit3:

Your carro_item.php should look something like this:

<?php

  require("admin/db/connect.php");

  $id = (int)$_GET['id_carro'];

  $sql = "SELECT * FROM tb_carros WHERE id = $id";

  $result = mysql_query($sql);

  $row = mysql_fetch_array($result);

  // ...
?>

<!-- And your HTML should look something like this -->
<!-- ... -->

<div class="dadosItem">
  R$ <?= $valor ?><br /><br />
  Ano:   <?= $row['ano'] ?><br /><br />
  Kilometragem: <?= $row['km'] ?><br /><br />
  Cor: <?= $row['cor'] ?><br /><br />
  Portas: <?= $row['portas'] ?><br /><br />
  Combustível: <?= $row['combustivel'] ?><br /><br />
  Câmbio: <?= $row['cambio'] ?><br /><br />
  Final da placa: <?= $row['final_placa'] ?><br /><br />
  Carroceria: <?= $row['carroceria'] ?>
</div><!-- END of dadosItem -->

<!-- ... -->

Also, you should stray from using functions of the form mysql_* as they are deprecated. See Why shouldn't I use mysql_* functions in PHP? for more information.

Community
  • 1
  • 1
Aiias
  • 4,683
  • 1
  • 18
  • 34
  • Thanks Aiias, but I need the link to get the ID dinamicly too. For example, the page that I developed will return 'n' results: item01, item02, itemN. This results will be managed by the client. He will include and exclude itens from the database. So I don't know if the link ID is 5 or 100. Do you know how could I make this? – Adriano Machado Apr 07 '13 at 22:02
  • @AdrianoMachado - When outputting the html to the page, you'd just loop through all of the ids you want to be clickable. See my edit. – Aiias Apr 07 '13 at 22:09
  • Aiias, I tried a lot but it didn't work. I uddated the question with part of the code. – Adriano Machado Apr 08 '13 at 00:30
  • @AdrianoMachado - What are all of the column names in the table? Is there an `id`? How are you assigning the variables used in `carro_item.php`? (like `$valor`, `$ano`, etc) – Aiias Apr 08 '13 at 00:32
  • @AdrianoMachado - See my **Edit 2**. Can you post the code that is executing the query in `carro_item.php` and how the variables are being assigned? – Aiias Apr 08 '13 at 00:38
  • Thanks a lot Aiias... the links are working fine now. But the carro_item.php still not show the results. The query code and HTML of this page are below the "This is the PHP of carro_item.php:" in the updated question. – Adriano Machado Apr 08 '13 at 00:50
  • @AdrianoMachado - Where are you calling `mysql_fetch_array` for `carro_item.php`? I don't see that listed. (You listed `carro.php`) – Aiias Apr 08 '13 at 01:11
  • That is the question Aiias... how do I associate the ID from the cars.php with the $sql = "SELECT"; of the carro_item.php? I'll update my question with the new query code that I put on this page. But it's returning all the entries and not the specific one corresponding to the ID. – Adriano Machado Apr 08 '13 at 01:21
  • @AdrianoMachado - See my **Edit 3**. I have added the parts that filter your query as well as your updated HTML for the page. – Aiias Apr 08 '13 at 01:34
  • I heard about the mysql_* ... I'll take a look on it later. The query of your Edit 3 didn't work. It show's this: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in... – Adriano Machado Apr 08 '13 at 01:59
  • @AdrianoMachado - That probably means that the query failed (`$result` is `FALSE`). Are you sure you have a column named `id` in your `tb_carros` table? Is it named `id_carro`? If it is named `id_carro`, your query should be `SELECT * FROM tb_carros WHERE id_carro = $id` – Aiias Apr 08 '13 at 02:01
  • Sorry Aiias... I didn't change the id for id_carros before... now it's working... thanks A LOT for your help. – Adriano Machado Apr 08 '13 at 02:13
  • @AdrianoMachado - No problem, happy to help. Good luck with your site! :) – Aiias Apr 08 '13 at 02:15
  • @AdrianoMachado - Just a tip for using StackOverflow, if your question has been answered, you should [mark it as answered](http://stackoverflow.com/faq#howtoask), otherwise it may continually get more responses since it is not considered answered. – Aiias Apr 08 '13 at 03:34
  • Thanks Aiia... I'll do it now. – Adriano Machado Apr 09 '13 at 23:34
0

PHP:

 $id = (int)$_GET['id'];

HTML:

<a href="page.php?id=<?= $id; ?>">

query:

SELECT * FROM table WHERE id = $id;
Royal Bg
  • 6,988
  • 1
  • 18
  • 24