Hi all,
For some reason I can't seem to wrap my mind around JOINING tables and getting the data I need from this.
I have three tables I am trying to join. I need to count the number of blogs, comments and number of views for each user. I have all data ready to be pulled from the DB but I am just not sure of the cleanest most effective way to do this. I could do separate queries for each table but this seems illogical to me.
Some of the areas are not yet filled in here. It's the query I need help with.
Here is what I have:
function displayAgents()
{
$sql = ("SELECT * FROM users AS u JOIN comments AS c JOIN blogs AS b WHERE pu.id_user = pc.id_user AND pc.id_user = pb.id_user ");
$result=mysql_query($sql);
$display = '';
while ($row = mysql_fetch_array($result)) {
$agentId = $row['id_user'];
$agentName = $row['name'];
$agentEmail = $row['email'];
$agentImage = $row['user_image'];
$agentHeadline = $row['headline'];
$agentStatus = $row['user_status'];
$agentPro = $row['pro_status'];
$agentCompany = $row['company'];
$agentLocation = $row['location'];
$agentWebsite = $row['websites'];
$display .= "
<div class=\"agentLocated\">
<div class=\"agentLocatedTop\">
<div class=\"agentLocatedName\"><a href=\"#\">". $agentName . "</a></div>
<div class=\"agentLocatedProStatus\">, Agent</div>
<div class=\"agentLocatedLevel\"><img width=\"15\" height=\"15\" src=\"images/icons/medal_gold_2.png\" /></div>
<div class=\"agentLocatedProfile\"><a href=\"\">Profile</a></div>
<br/>
<br/>
<div class=\"agentLocatedImageWrapper\">
<div class=\"agentLocatedImage\"><img class=\"\" width=\"54\" height=\"64\" src=\"images/users/Asha1.jpg\"></div>
</div>
<div class=\"agentLocatedTagline\">". $agentHeadline . "</div>
<br/>
<div class=\"agentLocatedInfo\">" . $agentCompany . " | | " . $agentLocation . "</div>
<br/>
<br/>
<div class=\"agentlocatedStats\">9,999 Blog posts</div>
<div class=\"agentlocatedStats\">9,999 Comments</div>
<div class=\"agentLocatorRec\">
<div class=\"agentLocatedRecs\">
<div class=\"agentLocatorRecsCount\">0</div>
<div class=\"agentLocatorRecsText\">RECS</div>
</div>
<div class=\"agentLocatedRevs\">
<div class=\"agentLocatorRevsCount\">1</div>
<div class=\"agentLocatorRevsText\">REVIEWS</div>
</div>
<div class=\"agentLocatorRecommend\"><a href=\"#\"></a></div>
</div>
</div>
</div> \n";
}
if (!$result) {
return false;
} else {
return $display;
}
}
Thanks in advance for any help on this...