Results 1 to 3 of 3

Thread: categories and id (2 tables)

  1. #1
    Join Date
    Nov 2007
    Posts
    151
    Thanks
    67
    Thanked 0 Times in 0 Posts

    Default categories and id (2 tables)

    Hi

    I have 2 tables, which were created in order to save stories in categories:

    stories:
    1) story_id
    2) story_category (works as category id)


    stories_categories :
    1) story_categoryname
    2) story_category (works also as category id)

    I've tried to print the information in this way:

    story_categoryname:
    1) story_id
    2) story_id
    3)story_id

    other story_categoryname:
    1) story_id
    2) story_id

    if some category is empty and doesn't have stories it won't be printed.

    this the code I've tried and didn't get where I went wrong:

    PHP Code:
    $query1 "select distinct story_categoryname from stories_categories";
    $res1 mysql_query($query1);
    if(
    $res1){
      while(
    $cat mysql_fetch_assoc($res1)){
         echo 
    $cat['story_categoryname']."<br/>";
         
    $query2 "select story_id from stories where story_category in (select story_category from stories_categories where story_categoryname = '".$cat['story_categoryname']."')";
         
    $res2 mysql_query($query2);
         if(
    $res2){
            while(
    $story_id mysql_fetch_assoc($res2)){
              echo 
    $story['$story_id'].'<br/>';
            }
         }
      }
    }else{
      print 
    mysql_error();


    Thank you

  2. #2
    Join Date
    Oct 2006
    Posts
    183
    Thanks
    0
    Thanked 11 Times in 11 Posts

    Default

    You could try modifying this (given to you in another topic)
    Code:
    $result = mysql_query("SELECT mytable1.name, mytable1.lastname, mytable2.shirtcolor FROM mytable1, mytable2
    WHERE mytable1.lastname = mytable2.lastname") or die(mysql_error());
    
    echo "<table border='1'>";
    echo "<tr>  <th>name</th> <th>lastname</th> <th>shirtcolor</th> </tr>";
    // keeps getting the next row until there are no more to get
    while($row = mysql_fetch_array( $result )) {
        // Print out the contents of each row into a table
        echo "<tr><td>"; 
        echo $row['mytable1.name'];
        echo "</td><td>"; 
        echo $row['mytable1.lastname'];
        echo "</td><td>"; 
        echo $row['mytable2.shirtcolor'];
        echo "</td></tr>"; 
    } 
    
    echo "</table>";

  3. #3
    Join Date
    Nov 2007
    Posts
    151
    Thanks
    67
    Thanked 0 Times in 0 Posts

    Default

    Thank you
    Last edited by d-machine; 08-11-2008 at 03:19 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •