RE: [PHP] assign associative array values to variables? SOLVED

2009-03-20 Thread Ford, Mike
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,
 /anbsp;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?)

Cheers!

Mike

 --
Mike Ford,  Electronic Information Developer,
C507, Leeds Metropolitan University, Civic Quarter Campus, 
Woodhouse Lane, LEEDS,  LS1 3HE,  United Kingdom
Email: m.f...@leedsmet.ac.uk
Tel: +44 113 812 4730


To view the terms under which this email is distributed, please go to 
http://disclaimer.leedsmet.ac.uk/email.htm

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



Re: [PHP] assign associative array values to variables? SOLVED

2009-03-20 Thread PJ
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,
 /anbsp;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 = book.id does not seem to work... is my syntax
wrong?

-- 
unheralded genius: A clean desk is the sign of a dull mind. 
-
Phil Jourdan --- p...@ptahhotep.com
   http://www.ptahhotep.com
   http://www.chiccantine.com/andypantry.php


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



Re: [PHP] assign associative array values to variables? SOLVED

2009-03-18 Thread PJ
dg wrote:

 On Mar 17, 2009, at 6:47 PM, PJ wrote:

 The problem is that the $array returns another array (multidimensional
 and associative ?? )


 ?php
 $array_branch_one = array(value_one = red, value_two = blue);
 $array_branch_two = array(value_three = green, value_four =
 black);
 $array_main = array(branch_one = $array_branch_one,branch_two =
 $array_branch_two);
 extract($array_main[branch_one]);
 extract($array_main[branch_two]);
 print $value_one, $value_two, $value_three, $value_four;

 ?

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,
/anbsp;nbsp;  ;
}
}
}

Because the array had keys that had various ids [number] and the value
was [category], I could not extract the category. By reassigning the
array I reset the key/value to [0] and [1]. That's in this, as above:
$catvar = array_values($category[$categoryID]);
$cat = $catvar[1];
The rest was a piece of cake. :-) 8-)

-- 
unheralded genius: A clean desk is the sign of a dull mind. 
-
Phil Jourdan --- p...@ptahhotep.com
   http://www.ptahhotep.com
   http://www.chiccantine.com/andypantry.php


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