Stella_r Posted March 13, 2009 Share Posted March 13, 2009 I'm having trouble separating an element from an array that I've taken from the database. ??? and here is how it displays..... Title one 2009-03-08 20:53:25 new comment by UserID4 Title Two 2009-03-09 01:35:29 new comment by UserID4 Title Two 2009-03-09 01:39:08 new comment by UserID4 Title Three 2009-03-10 22:26:54 new comment by UserID10 But what I want to do is: if Entry_ID's are identical, display the corresponding Entry_Title only once with the rest of the info following as below... Title one 2009-03-08 20:53:25 new comment by 4 Title Two 2009-03-09 01:35:29 new comment by 4 2009-03-09 01:39:08 new comment by 4 Title Three 2009-03-10 22:26:54 new comment by 10 Here is a print_r of the arrays.....from my query:"SELECT Entry_Title, Entry_Date, comment.Dream_ID, Comment_Date, comment.User_ID FROM comment, entry WHERE comment.Entry_ID=entry.Entry_ID AND dentry.User_ID=$User_ID AND Comment_Read='FALSE'"; Array ( [Entry_Title] => Title One [Entry_Date] => 2009-02-26 [Entry_ID] => 31 [Comment_Date] => 2009-03-08 20:53:25 [user_ID] => 4 ) Array ( [Entry_Title] => Title Two [Entry_Date] => 2009-02-26 [Entry_ID] => 27 [Comment_Date] => 2009-03-09 01:35:29 [user_ID] => 4 ) Array ( [Entry_Title] => Title Two [Entry_Date] => 2009-02-26 [Entry_ID] => 27 [Comment_Date] => 2009-03-09 01:39:08 [user_ID] => 4 ) Array ( [Entry_Title] => Title Three [Dream_Date] => 2009-02-26 [Entry_ID] => 25 [Entry_Date] => 2009-03-10 22:26:54 [user_ID] => 10 ) Here is my function... protected function displayNewComments() { $new_comment = $this->model->getReadComment($_SESSION['User_ID']); $html ='<div id="user">'."\n"; $html .="<div class='entry_header_space'>".$new_comment."</div>"; echo $html; $rso=$this->model->getUnreadComments($_SESSION['User_ID']); while($comments=$rso->fetch_assoc()) { $html="<h2>".$comments['Entry_Title']."</h2>"; $html .=$comments['Comment_Date'] ." new comment by ".$comments['User_ID'] ; echo $html; } (I've tried various things but none of them work quite right.) ??? Quote Link to comment https://forums.phpfreaks.com/topic/149214-help-group-fetch_assoc-by-same-data/ Share on other sites More sharing options...
sasa Posted March 13, 2009 Share Posted March 13, 2009 try protected function displayNewComments() { $new_comment = $this->model->getReadComment($_SESSION['User_ID']); $html ='<div id="user">'."\n"; $html .="<div class='entry_header_space'>".$new_comment."</div>"; echo $html; $rso=$this->model->getUnreadComments($_SESSION['User_ID']); $last_title = ''; while($comments=$rso->fetch_assoc()) { if ($last_title != $comments['Entry_Title']){ $last_title = $comments['Entry_Title']; $html="<h2>".$comments['Entry_Title']."</h2>"; } $html .=$comments['Comment_Date'] ." new comment by ".$comments['User_ID'] ; echo $html; } } Quote Link to comment https://forums.phpfreaks.com/topic/149214-help-group-fetch_assoc-by-same-data/#findComment-783595 Share on other sites More sharing options...
Stella_r Posted March 15, 2009 Author Share Posted March 15, 2009 Thanks! it's a start towards what I want to display, but it still prints out the same title twice, adding another comment on with each loop.... Title one 2009-03-08 20:53:25 new comment by UserID4 Title Two 2009-03-09 01:35:29 new comment by UserID4 Title Two 2009-03-09 01:39:08 new comment by UserID4 2009-03-09 01:35:29 new comment by UserID4 Title Three 2009-03-10 22:26:54 new comment by UserID10 Quote Link to comment https://forums.phpfreaks.com/topic/149214-help-group-fetch_assoc-by-same-data/#findComment-785019 Share on other sites More sharing options...
sasa Posted March 15, 2009 Share Posted March 15, 2009 oops try protected function displayNewComments() { $new_comment = $this->model->getReadComment($_SESSION['User_ID']); $html ='<div id="user">'."\n"; $html .="<div class='entry_header_space'>".$new_comment."</div>"; echo $html; $rso=$this->model->getUnreadComments($_SESSION['User_ID']); $last_title = ''; while($comments=$rso->fetch_assoc()) { if ($last_title != $comments['Entry_Title']){ $last_title = $comments['Entry_Title']; $html="<h2>".$comments['Entry_Title']."</h2>"; } else $html = ''; $html .=$comments['Comment_Date'] ." new comment by ".$comments['User_ID'] ; echo $html; } } Quote Link to comment https://forums.phpfreaks.com/topic/149214-help-group-fetch_assoc-by-same-data/#findComment-785067 Share on other sites More sharing options...
Stella_r Posted March 15, 2009 Author Share Posted March 15, 2009 You're Awesome!!! thanks so much! you don't want to know how long I've spent trying to figure that out, and it's so simple hehe. I think part of my problem, with my previous attempts, was that I wasn't first setting the $last_title variable. Quote Link to comment https://forums.phpfreaks.com/topic/149214-help-group-fetch_assoc-by-same-data/#findComment-785405 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.