2013-04-07 32 views
0

我开发了一个PHP页面,显示来自MySQL表的所有itens。现在,我想在用户点击此页面上的某个itens时显示特定项目。我如何获得此特定ID以及如何配置<a href="">链接?如何显示来自MySQL的特定ID - 新手

更新问题:

这是cars.php页的头部(的结果):

<? 

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

$sql = "SELECT * FROM tb_carros"; 

$limite = mysql_query("$sql"); 

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

?> 

和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 ?> 

现在,当用户点击一个项目我想打开页面carro_item.php加载项目的数据。

从数据库参考ID是id_carro

我尝试了很多类型的代码,但没有奏效。即使我把完整的URL浏览器上的数据不负载:

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

这是carro_item.php的PHP:

<? 

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

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

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

?> 

和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 --> 

任何帮助?

更新02:

这是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; 
} 


?> 

但是,很明显它返回所有的结果,如cars.php页面。问题是如何将结果过滤为用户点击的链接的相同ID?

我不知道如何编码$ sql =“SELECT * FROM tb_carros”;要做到这一点。

回答

1

你可以利用$_GET variables

如果该链接被格式化这样在页面上:

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

然后在你的PHP页面,你就可以通过全球$_GET数组访问此值。

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

要小心,不要给自己开SQL Injection虽然通过两种消毒你的论点或使用参数化查询。

参考

编辑:

要建立正确的方式格式化的链接,你会先检索所有的IDS的需要,并将其储存在一个数组中。我将以$ids为例。

$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:

cars.php格式的链接是这样的:

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

EDIT3:

carro_item.php应该是这个样子:

<?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 --> 

<!-- ... --> 

此外,您应该使用mysql_ *形式的函数,因为它们已被弃用。有关更多信息,请参阅Why shouldn't I use mysql_* functions in PHP?

+0

感谢Aiias,但我需要链接以获得ID dinamicly。例如,我开发的页面将返回'n'结果:item01,item02,itemN。这个结果将由客户端管理。他将包括并从数据库中排除itens。所以我不知道链接ID是5还是100.你知道我怎么能做到这一点? – 2013-04-07 22:02:57

+0

@AdrianoMachado - 将html输出到页面时,您只需遍历所有想要点击的id。看我的编辑。 – Aiias 2013-04-07 22:09:07

+0

哎呀,我尝试了很多,但没有奏效。我用部分代码解决了这个问题。 – 2013-04-08 00:30:06

0

PHP:

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

HTML:

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

查询:

SELECT * FROM table WHERE id = $id; 
相关问题