Forum Moderators: coopster

Message Too Old, No Replies

Query in Query

         

Twisted Mind

10:43 am on Jan 17, 2006 (gmt 0)

10+ Year Member



Hi i have these tables (Example)

¦id¦name ¦info¦catid¦published¦
+--+------+----+-----+---------+
¦1 ¦name1 ¦bla ¦1 ¦0 ¦
etcetera.
ID is just for id of table name is name of product info in just info :D. catid stand for category-id wich should exist in another table published is just if it is published or not.
The other table:
¦id¦name ¦info¦published¦
+--+---------+----+---------+
¦1 ¦catname1 ¦bla ¦0 ¦

I want a query to loop all products this is not hard for me (Its damn easy):
$result = mysql_query("SELECT * FROM mos_mediaman_products");
now i need to do another query in the result doing something like:
select * from mos_mediaman_category WHERE id == $row['catid']; and after echoing the result the first query just collaps.

Here is my code i tried:
plz take a look:

<?

if (!isset($_GET['type']))
{
header("Location: index2.php?option=com_mediaman&mosmsg=ERROR: Something wierd happened did you follow the menu link?");
}
if ($_GET['type'] == 'product')

{
$pagename = "products";
$v1 ="name";
$v2 ="Category";
$v3 ="Published";
$result = mysql_query("SELECT * FROM mos_mediaman_products");
}
?>

<table class="adminheading">
<tr><th>Manage Media - Add <?echo "$pagename";?></th></tr>
</table>
<table width="600" class="adminform">
<tr>
<th width="20">#</th>
<th align="left" style="white-space:nowrap;"><?php echo "$v1"?></th>
<th align="left" style="white-space:nowrap;"><?php echo "$v2"?></th>
<th align="left" style="white-space:nowrap;" width="10"><?php echo "$v3"?></th>
</tr><?while ($row = mysql_fetch_array($result)){?>
<tr>
<td><?echo $row['id'];?></td>
<td><?echo $row['name'];?></td>
<?

IF ($_GET['type'] == 'product')

{$rresult = mysql_query("SELECT name FROM mos_mediaman_category WHERE id == ". $row['catid'] ."");}
while ($rrow = mysql_fetch_array($result)){
?>
<td><?echo $rrow['name'];}?></td>
<td></td>
</tr>
<?}?>
<tr>
<th width="" colspan="4">.</th>

</tr>
</table>

Twisted Mind

11:03 am on Jan 17, 2006 (gmt 0)

10+ Year Member



plz peeps if something is not clear please ask me and if you know to push me in the right direction please tell me too! cuz when i get this done this script i would be verry happy :D.

tomda

11:30 am on Jan 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Only one query can do the job!

Example

$result = mysql_query("SELECT mos_mediaman_products.id, mos_mediaman_products.name, mos_mediaman_products.catid, mos_mediaman_category.name FROM mos_mediaman_products, mos_mediaman_category where id='10' AND mos_mediaman_products.catid=mos_mediaman_category.id");

This query should return:
Products: ID - NAME - CATID + Category: NAME

Done on the fly! To be tested!
Tomda

Twisted Mind

11:41 am on Jan 17, 2006 (gmt 0)

10+ Year Member



WOuldn work becouse the id is static in the query and it has to be looped...

Twisted Mind

11:42 am on Jan 17, 2006 (gmt 0)

10+ Year Member



the 10 should be mos_mediaman_products.catid would this work ill try...

Twisted Mind

11:49 am on Jan 17, 2006 (gmt 0)

10+ Year Member



Error Code : 1052
Column 'id' in where clause is ambiguous
(0 ms taken)

thats what i get with ur query and if i change id to mos_mediaman_category.id it returns the products table without results thanks for trying to help me though :D.

Twisted Mind

11:51 am on Jan 17, 2006 (gmt 0)

10+ Year Member



the query works though i cant use it this way :D the id should be all id's in the query

Twisted Mind

11:54 am on Jan 17, 2006 (gmt 0)

10+ Year Member



my bad it works well if you just delete the id=10

Twisted Mind

11:55 am on Jan 17, 2006 (gmt 0)

10+ Year Member



my big saviour can u tell me how to return this row?
Will it be row = mysql fetch array still and then how to call em back?

Twisted Mind

12:03 pm on Jan 17, 2006 (gmt 0)

10+ Year Member



i can only return the category name now :P maybe i should just change the db field names?

Twisted Mind

12:06 pm on Jan 17, 2006 (gmt 0)

10+ Year Member



NVM got it working thanks to you my saviour ;)

tomda

12:11 pm on Jan 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Woah... So many posts!
Glad it works!

At least post the solution for yourself :)