Re: displaying repetitive results

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux