Jump to content

Nested SQL Statements


seran128

Recommended Posts

ok I have three tables the first table holds the category Name
Somthing like

tbl_category

CategoryID
Category Name

The second table holds news information Something like

tbl_news

NewsID (int)
News Title (varchar80)
News Content (blob)

The third table joins the two tables

tbl_news_join_category

ID (INT)
NewsID (INT)
CategoryID (INT)

this way I can have a news article belong to many different Categories ok

my function


[code]function news(){

global $connection;
$sql="select * from tbl_category";
$result=mysql_query($sql,$connection) or die(mysql_error());
while($row=mysql_fetch_array($result)) {
$news .= "<table width='200' border='0'>
    <tr>
      <td>" . stripslashes($row['category_name']) . "</td>

    </tr>
    <tr>
      <td></td>
    </tr>
  </table>";
}
return $news
}[/code]

This returns the category name as it should now what I would like to do is do a query after the

[code]<td>" . stripslashes($row['category_name']) . "</td>[/code]

and display all the news titles that belong to that category

then continue on
with the rest of the code

[code]</tr>
    <tr>
      <td></td>
    </tr>
  </table>";
}
return $news
}[/code]
Link to comment
Share on other sites

[code]<?php
function news(){

global $connection;
$sql="select * from tbl_category";
$result=mysql_query($sql,$connection) or die(mysql_error());
while($row=mysql_fetch_array($result)) {
$news .= "<table width='200' border='0'>
    <tr>
      <td>" . stripslashes($row['category_name']) . "</td>";
$sqls = mysql_query("SELECT * FROM tbl_news WHERE category='$row[category_name]'") or die(mysql_error());
while($rows=mysql_fetch_array($sqls)) {
echo $rows['column_name_from_tbl_news'];
}
echo"
    </tr>
    <tr>
      <td></td>
    </tr>
  </table>";
}
return $news
}
?>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.