Working with two tables

Discussion in 'PHP' started by asgsoft, Jan 29, 2008.

  1. #1
    Hey everyone

    I am stuck again :D

    This time I'm trying to work with two tables.

    First one is a categories one
    Second one is an articles one

    The category one has the ability to have categories and sub cats too.

    So basically I want to list the articles from the article database which have a value of say x for their cat_id field to be listed under their appropriate heading.

    So the structure looks a bit like this:
    Here is the code which just lists the category titles. I couldn't go on from there :S

    $sql = "SELECT * FROM categories WHERE ParentID = 0 AND id = $id";
    $res = mysql_query($sql);
    while ($row = mysql_fetch_assoc($res))
    {
         
    	 
    	 // if link empty echo cat page else echo link
    	 
    	 echo "<dt>$row[name]</dt>";
    //echo articles under man cat
         $sql_sub = "SELECT * FROM categories WHERE ParentID = ".$row["id"];
    	 //echo $sql_sub;
         $res_sub = mysql_query($sql_sub);
         while ($row_sub = mysql_fetch_assoc($res_sub))
         {
              echo "<dd>$row_sub[name]</dd>";
    		  //echo articles under subcat
         }
    }
    PHP:

    any help would be greatly appreciated :D
     
    asgsoft, Jan 29, 2008 IP
  2. LittleJonSupportSite

    LittleJonSupportSite Peon

    Messages:
    386
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Looks like you were almost there.

    I mean list the cats, then get all the id's now go into the other table and pull all the articles where catid=x and articled=y


    I mean looks like you pretty much have it right there.

    Not sure if your asking for advice or if your asking me to code the whole thing.
     
    LittleJonSupportSite, Feb 1, 2008 IP