Log in

View Full Version : categories and id (2 tables)



d-machine
08-10-2008, 08:20 PM
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:



$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

motormichael12
08-10-2008, 09:36 PM
You could try modifying this (given to you in another topic)

$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>";

d-machine
08-11-2008, 07:40 AM
Thank you