RE: how to extract fields from associative array into different variables

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

 



On 16 June 2009 13:58, PJ advised:

> Ford, Mike wrote:
>> On 15 June 2009 18:07, PJ advised:
>> 
>> 
>>> Is there an easier or simpler way to do this?
>>> code:
>>> 
>>> $sql = "SELECT first_name, last_name, book_author.ordinal
>>>       FROM author, book_author
>>>       WHERE book_author.bookID = $idIN && book_author.authID =
>>> 
>> author.id
>> 
>>> ORDER BY ordinal";
>>> 
>>>     $author = array();
>>>     if ( ( $results = mysql_query($sql, $db) ) !== false ) {
>>>         while ( $row = mysql_fetch_array($results, MYSQL_ASSOC) ) {
>>> 
>> 
>> 
>>>         $author[] = $row; }
>>>     }
>>> $numrows = mysql_num_rows($results);
>>> switch ($numrows)
>>> {
>>> case 5:
>>>   $first_nameIN = $author[0]['first_name'];
>>>   $last_nameIN = $author[0]['last_name'];
>>>   $first_name2IN = ;
>>>   $last_name2IN = $author[1]['last_name'];
>>>   $first_name3IN = $author[2]['first_name'];
>>>   $last_name3IN = $author[2]['last_name'];
>>>   $first_name4IN = $author[3]['first_name'];
>>>   $last_name4IN = $author[3]['last_name'];
>>>   $first_name5IN = $author[4]['first_name'];
>>>   $last_name5IN = $author[4]['last_name'];
>>>   break;
>>> case 4:
>>>   $first_nameIN = $author[0]['first_name'];
>>>   $last_nameIN = $author[0]['last_name'];
>>> snip....
>>> 
>> 
>> Why not just use $author[0]['first_name'] instead of $first_nameIN
(and
>> so on) in the rest of your code?
>> 
>> 
> I probably should have explained in greater detail: there are up to 5
> authors for each book in the db and the insert and update
> fields in the
> form input needs the different variables as each author is treated
> separately. I'm not sure if there is another way to do it.

So? In what way does this invalidate my query?

Wherever you have used $first_nameIN, just use $author[0]['first_name'];
wherever you have used $first_name2IN, just use
$author[1]['first_name']; and so on.

Or is there some other context here that you're not telling us? Perhaps
you need to quote more of your script (except leaving out the humungous
switch!)?

Cheers!

Mike

 --
Mike Ford,  Electronic Information Developer,
C507, Leeds Metropolitan University, Civic Quarter Campus, 
Woodhouse Lane, LEEDS,  LS1 3HE,  United Kingdom
Email: m.ford@xxxxxxxxxxxxxx
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



[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