Re: displaying repetitive results
| From: | Tom Rogers | Date: | Sun, 14 Nov 2004 02:59:25 +0000 |
| Subject: | Re: displaying repetitive results | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-201945@lists.php.net to get a copy of this message | ||
Hi,
Thursday, November 11, 2004, 4:04:27 AM, you wrote:
CL> Given a database query thats returns results from a linking (or xref)
CL> table which includes repetition because of the joins:
CL> +----+--------------------------+----------+
CL> | id | title | subject |
CL> +----+--------------------------+----------+
CL> | 1 | Collected Poems of Keats | poetry |
CL> | 2 | Spy High | suspense |
CL> | 3 | Sci Fi Spies | suspense |
CL> | 3 | Sci Fi Spies | sci-fi |
CL> +----+--------------------------+----------+
CL> What is the best way to go about displaying this for the user so that
CL> the record looks "complete":
CL> ID: 3
CL> title: Sci Fi Spies
CL> Subjects: suspense, scifi
CL> or something similar? Or is there some better way to query? It's also
CL> a problem in terms of limiting the query because if I limit the query
CL> to 10 records I might be chopping off some subjects for the last book?
CL> The query and tables are simple:
CL> select books.id, books.title, subjects.subject
CL> from books, subjects, books_subjects
CL> where books_subjects.bid = books.id
CL> and books_subjects.sid = subjects.id
CL> BOOKS
CL> 1. collected poems of keats
CL> 2. spy high
CL> 3. sci-fi spies
CL> SUBJECTS:
CL> 1. poetry
CL> 2. suspense
CL> 3. sci-fi
CL> 4. horror
CL> 5. mystery
CL> BOOKS_SUBJECTS
CL> bid sid
CL> 1 1
CL> 2 2
CL> 3 2
CL> 3 3
CL> c
when you read the rows create an array of subjects like this
(assuming mysql)
while ($row = mysql_fetch_assoc($result){
$list[$row['title']]['subjects'][] = $row['subject'];
}
//Then you can loop through $list and implode the subjects
foreach ($list as $title=>$subjects){
$subject_list = implode(',',$subjects);
echo
"<tr><td>$title</td><td>$subject_list</td></tr>";
}
--
regards,
Tom