Ford, Mike wrote:
> On 18 March 2009 13:21, PJ advised:
>> Thank you, once again, dg. Could not get it to work... whatever...
>> But I did find an unexpected solution which made things very simple.
>> This is the part for the categories section:
>> if ( isset( $book_categories[$bookID] ) ) {
>>     foreach ( $book_categories[$bookID] AS $categoryID ) {
>>         if ( isset( $category[$categoryID] ) ) {
>>         *$catvar = array_values($category[$categoryID]);         $cat
> =
>>         $catvar[1];* $catn = preg_replace("/[^a-zA-Z0-9]/", "", $cat);
>>         echo "<a href='categories/", $catn, ".php'>", $cat,
>>             "</a>&nbsp;&nbsp;  "; }
>>         }
>>     }
> I realise I'm a bit behind the curve on this, but this looks like a
> rather convoluted way of doing what you want to do. However, it's rather
> difficult to tell without *specifics* -- can you post the output from
> var_dump($book_categories) and var_dump($category), and a snippet of
> what you want the eventual output from that to look like, so we can
> actually work out the most efficient route from one to the other?  (It
> might also be helpful to know where $book_categories and $category come
> from originally -- are they the results of a database query?)/email.htm

The results are from a db query. What's the /email.htm ?

var_dump($book_categories) outputs a series of arrays for all the books
in the db - which is not what I want.
var_dump($categories) outputs the same plus those that have a second
author; 28 for book_categories, 34 for categories.

Actually, I'm about to ask if someone would like to look at the code and
see what could be done to improve it. I still have to do some sanitizing
and formatting the code better for HTML output as well as adding LIMIT
stuff to show only a few pages since eventually the db could hold
upwards of a thousand entries.
I'm a little hesitant to post the script as it is almost 200 lines; if
you like, I can either post the whole page or upload the stuff to the
web site as I will have to do it eventually. Here are the categories and
book_categories queries:

$book_categories = array();
$SQL = "SELECT bookID, categories_id FROM book_categories";
if ( ( $results = mysql_query($SQL, $db) ) !== false ) {
    while ( $row = mysql_fetch_assoc($results) ) {
        $book_categories[$row['bookID']][] = $row['categories_id'];

$category = array();
$SQL = "SELECT id, category FROM categories";
if ( ( $results = mysql_query($SQL, $db) ) !== false ) {
    while ( $row =  mysql_fetch_assoc($results) ) {
        $category[$row['id']] = $row;

The book_categories query returns all the categories for all the books.
I haven't been able to limit it to just one book at a time; adding WHERE
book_categories.bookID = does not seem to work... is my syntax

unheralded genius: "A clean desk is the sign of a dull mind. "
Phil Jourdan ---

PHP General Mailing List (
To unsubscribe, visit:

Reply via email to