Reputation: 23
Okay so I just had a massive long discussion on here trying to get this code to work as it should... I am trying to display a "status" system on my site. So users can post on other users profiles and then comment on these statuses. Finally, we managed to get the 3 databases combined. I will explain the code first, and show the errors below...
Users
Statuses
Comments
Users contains the standard...
userid
username
first_name
last_name
photo
Statuses contains any information about a main status posted from each user, these statuses are displayed on the users profiles. So user 1 could post a status on user2's profile. Here is the statuses table design...
status_id (auto-i)
user_id (The users ID whos profile the post was added too)
sender_id (The user who sent the post or wrote it)
date (Date/Time was sent)
rate (This doesn't matter for a moment)
comments (This will count all the comments and display the number)
status (The actual status written out)
These tables worked fine added together in my script which connected both tables and displayed the users information (the one who posted the status) such as their profile photo and name etc... Here is my current script which has no issues at all, this combines the third table now (comments)...
//// GET STATUSES
$user_id = $profile_data['userid'];
$data = mysql_query("
SELECT -- added alias to your tables so its easier to read
s.status_id,
s.user_id,
s.sender_id,
s.status,
s.date,
u.first_name,
u.last_name,
u.photo,
u.username,
u.userid as uid,
o.userid as oid,
o.first_name as ofn,
c.comment,
c.status_id as csi
FROM statuses s
LEFT JOIN users u ON s.sender_id=u.userid
left JOIN comments c ON c.user_id = u.userid AND c.status_id = s.status_id
left join users o on o.userid = c.sender_id
where s.user_id = {$profile_data['userid']}
ORDER BY c.date DESC, s.date DESC") or die(mysql_error());
while($status = mysql_fetch_array( $data ))//added this
{
$statusid = $status['status_id'];
$date = $status['date'];
$rate = $status['rate'];
$comments = $status['comments'];
$userid = $profile_data['userid'];
$senderid = $status['uid'];
$statusbody = $status['status'];
$username = $status['username'];
$firstname = $status['first_name'];
$lastname = $status['last_name'];
$photo = $status['photo'];
$comment = $status['comment'];
$name = $status['ofn'];
$csi = $status['csi'];
?>
<form action="" method="POST" role="form" enctype="multipart/form-data" id="statusupdate" class="facebook-share-box">
<div class="share">
<div class="panel panel-default">
<div class="panel-heading"><a href="<? echo 'http://basecentre.co.uk/',$status["username"]; ?>"><img alt="" align="left" hspace="20" height="70" width="70" src="<? echo 'http://basecentre.co.uk/userimages/',$status["photo"]; ?>"> </a> <font size="+2"><i class="icon icon-comment-o"></i></font> <a href="<? echo 'http://basecentre.co.uk/',$status["username"]; ?>"><font size="+2"><?php echo $status['first_name']; ?> <?php echo $status['last_name'] ; ?></font></a> | <i class="icon icon-clock-o"></i> <a rel="tooltip" href="#" data-original-title="<? echo "". date('F j, Y, g:i a', strtotime($status['date']) + 60*60) .""; ?>"><?php echo "<strong>". date('j F', strtotime($status['date']) + 60*60) ."</strong>"; if ($status['sender_id'] == $_SESSION['userid'] || $status['user_id'] == $_SESSION['userid']){
echo '<form name="deletestatus" action="" method="POST">
<input type="hidden" name="statusid" value="'.$status['status_id'].'" />
<td><input type="submit" value="Delete" onclick="return checkDelete()" class="btn btn-danger btn-xs" name="deletestatus" ></td>
</form>';
} else
echo "";?></a></div>
<div class="panel-body">
<div class="">
<?php echo $status['status']; ?>
</div>
</div>
<div class="panel-footer">
<div class="row">
<div class="col-md-7">
<? echo "".$status['comment']."".$status['ofn']."".$status['csi'].""; ?>
</div>
</div>
</div>
</div>
</div>
</form>
</br>
<?
}
?>
Here is the information and database for "Comments" table.
comment_id (auto-i)
status_id (added depending on which status you comment on. If the comment is on status id #5, the same number will be sent to this to connect the comments with the correct statuses)
sender_id (the id of the user who is sending the comment which would be $_session['userid'];
date (date the comment was sent)
rate (Doesn't matter yet)
comment (the actual comment written out).
MY ISSUE: The problem I currently have which I am trying to work out is that "statuses" with more than ONE comment come up twice? So you'll have the same status repeated with a different comment below? I need to get all the comments on the same status where the status_id is the same.
In my last discussion we were talking about checking for duplicates and then if the comments table is found to have more than one comment for the same status_id, it should be added to a separate array?
I thought this was a bit off subject for my last post and I have no idea of how I would go about doing this? Would anyone be able to help me out here ? Or even give me some advice on where I would start? Thank you.
Upvotes: 0
Views: 53
Reputation: 602
You'll probably have to iterate through your results in some way, obviously according to how your arrays are structured. An example would be:
Ex.1, if $results is an associate array with the the keys as the status id.
$data = array();
foreach ($results as $k =>$v)
{
// $k could be a status, for example "status_1"
// if it doesn't yet exist in the data array as a key
// we create an array that will hold comments for that status
if (!isset($data[$k])
{
$data[$k] = array();
}
// this status is already a key in our final data set $data, so we add
// add the comments to the array
else
{
$data[$k][] = <another_comment>
}
}
Ex.2, If $results is not an associative array.
$data = array();
foreach ($results as $v)
{
$status_id = $v['status_id];
// if it doesn't yet exist in the data array as a key
// we create an array that will hold comments for that status
if (!isset($data[$status_id])
{
$data[$status_id] = array();
}
// this status is already a key in our final data set $data, so we add
// add the comments to the array
else
{
$data[$status_id][] = $v['the_comment'];
}
}
Either way, your final array would look something like:
array(
status_1 => array(
[0] => 'some string 1',
[1] => 'some string 2,
)
)
Upvotes: 2