如果找到重复值,则将数组添加到一起?

时间:2014-05-12 04:36:04

标签: php mysql

好的,所以我在这里进行了长时间的讨论,试图让这段代码正常工作......我正试图显示一个"状态"我的网站上的系统。因此,用户可以在其他用户个人资料上发布,然后对这些状态发表评论。最后,我们设法将3个数据库组合在一起。我将首先解释代码,并在下面显示错误...

Users
Statuses
Comments

用户包含标准......

userid
username
first_name
last_name
photo

状态包含有关每个用户发布的主要状态的任何信息,这些状态显示在用户配置文件中。因此,用户1可以在user2的个人资料上发布状态。这是状态表设计...

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)

这些表在我的脚本中工作得很好,它连接了两个表并显示了用户信息(发布状态的人),例如他们的个人资料照片和姓名等......这是我当前的脚本,它没有任何问题所有,现在结合第三个表(评论)......

 //// 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>


<? 
                                  }


    ?>  

这是&#34;评论&#34;的信息和数据库。表

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).

我的问题: 我目前所遇到的问题是&#34;状态&#34;有两个以上的评论出现两次?所以你会在下面用不同的评论重复相同的状态吗?我需要在status_id相同的状态下得到所有注释。

在我上次的讨论中,我们讨论的是检查重复项,然后如果发现注释表对同一个status_id有多个注释,那么它应该被添加到一个单独的数组中吗?

我认为这是我上一篇文章的主题,我不知道我会怎么做呢?有人能帮助我吗?或者甚至给我一些关于我将从哪里开始的建议?谢谢。

1 个答案:

答案 0 :(得分:2)

您可能需要以某种方式迭代结果,显然是根据数组的结构。一个例子是:

例1,如果$ results是一个关键数组,其中键是状态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>
    }
}

例2,如果$ results不是关联数组。

$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'];
    }
}

无论哪种方式,你的最终数组看起来都像:

array(
    status_1 => array(
        [0] => 'some string 1',
        [1] => 'some string 2,
    )
)