mauro
mauro

Reputation: 127

echo list of products by category

Can someone tell me how to echo data that is relating with another table?

I have a table

tbl_category_products:<
- id_categorys
- category

And other table for the products

tbl_products:
- title
- description
- id_categorys

How could I echo the list of products by category in PHP?

Here is some code, but no sucess. Im trying but no sucess, here is the original code Table names: 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'];

  ?>

Upvotes: 0

Views: 145

Answers (5)

Ruben Nagoga
Ruben Nagoga

Reputation: 2218

Something like this

First of all: mysql connect

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

Upvotes: 1

Fahim Parkar
Fahim Parkar

Reputation: 31627

Try this

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'

Upvotes: 0

Vaishu
Vaishu

Reputation: 2363

SELECT tp.title, tp.description, tcp.category FORM tbl_products tp,tbl_category_products tcp where tp.id_categorys = tcp.id_categorys

Upvotes: 0

amburnside
amburnside

Reputation: 1903

Use a JOIN to query both tables

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

Upvotes: 0

xdazz
xdazz

Reputation: 160833

You need a join:

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

Upvotes: 1

Related Questions