簡體   English   中英

按類別回收產品列表

[英]echo list of products by category

有人能告訴我如何回顯與另一個表有關的數據嗎?

我有一張桌子

tbl_category_products:<
- id_categorys
- category

和其他表的產品

tbl_products:
- title
- description
- id_categorys

我怎樣才能在PHP中按類別回顯產品列表?

這是一些代碼,但沒有成功。 我嘗試但沒有成功,這里是原始代碼表名稱:tbl_categorias_noticias
- id_categoria_noticia
- categoria

tbl_noticias
- id_noticia
- id_categoria_noticia
-titulo
-decricao
-msg
-nome_arquivo
-legenda

<?php
 $categoryId = 1; 
  $sql_visualizar = mysql_query("SELECT * FROM tbl_noticias AS t1 JOIN tbl_categorias_noticias c
ON c.id_categoria_noticia=t1.id_categoria_noticia WHERE id_categoria_noticia = {$categoryId}");
while($linha = mysql_fetch_array($sql_visualizar)){
$titulo = $linha ['titulo'];
$descricao = $linha['descricao'];
$msg = $linha['msg'];
$legenda = $linha['legenda'];
$nome_arquivo = $linha['nome_arquivo'];
$id_noticia = $linha['id_noticia'];

  ?>

您需要加入:

SELECT t1.title, t1.description, t2.category
FORM tbl_products t1
LEFT JOIN tbl_category_products t2 ON t1.id_categorys = t2.id_categorys

像這樣的東西

首先: mysql連接

$categoryId = 1;    
$query = "SELECT t1.* FROM tbl_products AS t1 JOIN tbl_category_products c
ON c.id_categorys=t1.id_categorys WHERE id_categorys = {$categoryId}";
$result = mysql_query($query);

while($r = mysql_fetch_assoc($result)) {
    //show your products
}

使用JOIN查詢兩個表

SELECT p.title, p.decription, c.category
FROM tbl_products p
JOIN tbl_category_products c
ON c.id_categorys=p.id_categorys
ORDER BY c.category ASC
SELECT tp.title, tp.description, tcp.category FORM tbl_products tp,tbl_category_products tcp where tp.id_categorys = tcp.id_categorys

嘗試這個

SELECT t1.title, t1.description, t2.category
FORM tbl_products t1
JOIN
tbl_category_products t2 
ON t1.id_categorys = t2.id_categorys
WHERE t1.id_categorys = 'myCategory'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM