Any help you can offer is much appreciated!
I have a web site where I have visitors who may or may not see the same content. For example, if you are a doctor you should see records 1 and 2. If you are a veterinarian you should see records 2 and 3. The doctor should never see record 3 and the vet should never see record 1. For the example below each record is in a different content category and the category name should display only if there is content to show for that category (a record can belong to only one category).
So, for example, when the doctor pulls up her page (page.php?audience=doctor), she should see the following:
--------page display--------
Welcome Doctors
Here's your content:
Category 1
- <Record 1 content title>
Category 2
- <Record 2 content title>
When the veterinarian pulls up her page (page.php?audience=vet), she should get:
--------page display--------
Welcome Veterinarians
Here's your content:
Category 2
- <Record 2 content title>
Category 3
- <Record 3 content title>
My tables are:
Content table:
- content_id
- title
- text
- category_name
Audience table:
- audience_id
- audience_type
Category table
- cat_id
- cat_type
And the table that associates the content record with the audience type:
Audience_data
- id
- audience_id
- content_id
I'm using the following code to get the Category and Record X titles out of the database, but it's not working correctly. The result is actually pretty strange. Say I have four records:
Record 1, Category 1
Record 2, Category 2
Record 3, Category 2
Record 4, Category 1
The date looks like so on screen:
Category 1
- <Record 1 content title>
Category 2
- <Record 2 content title>
- <Record 3 content title>
Category 1
- <Record 4 content title>
I'm doing an ORDER BY category in my query, but it isn't helping. I'm not sure why Category 1 is showing up again? And why Record 4 just doesn't show up under the first instance of Category 1?
Here's the code I'm using - I'm sure it's the long way, but it's all I've got:
connect to dbase
$result2 = mysql_query ("SELECT * FROM audience_data where audience_id = '$audience'", $db); // I get the $audience variable from the URL
$row_count2 = mysql_num_rows($result2);
if ($row_count2 == 0) {
echo ("Can't find the records for this audience.\n");
} else {
while ($row2 = mysql_fetch_array ($result2)) {
$content_id = $row2['content_id'];
// Now we have the content id info for the records we need to display. So we run a
//query on the content table
$result3 = mysql_query ("SELECT title, category_name FROM content WHERE content_id = '$content_id' ORDER BY 'category_name'", $db);
$row_count3 = mysql_num_rows($result3);
if ($row_count3 == 0) {
echo ("Something's gone wrong....");
} else {
$category = ''; // this bit of script I grabbed from another post
while ($row3 = mysql_fetch_array ($result3)){
$old_category = $category;
$category = $row3['category'];
$title = $row3['title'];
if ($old_category != $category) {
echo ("<br><b>$category</b>\n");
}
echo ("<li>$title<br>");
}
}
}
}