Jump to content

HELP!!! GROUP fetch_assoc by SAME data


Stella_r

Recommended Posts

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

Link to comment
Share on other sites

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;
}
}

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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;
}
}

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.