You are here: Re: [PHP] foreach ($media as $key => $val) problem « PHP « IT news, forums, messages
Re: [PHP] foreach ($media as $key => $val) problem

Posted by tg-php on 06/01/05 22:14

Ahh.. an easy one. Unless I'm mistaken (and it does happen more often than I'd like.. hah :) then you might want to ommit the first:

$media = mysql_fetch_assoc($media_result);


That's reading data into $media and then it happens again at the start of your while loop which is why you're only getting the second item.

-TG

= = = Original message = = =

Hi, all,
This might look like a mysql problem but I assure you it's my botching
the PHP which is the problem!

I'm building a part of a page with info from three tables: art, media
and media_art. Media_art is the intersection table. For every entry in
art there can be one or more entries in media.
art.art_id 1 has two entries in media_art:

Media_id art_id
3 2
5 2

I do this SQL, which in phpmyadmin returns the two names of media as
expected

SELECT media.media_name
FROM art, media_art, media
WHERE art.art_id = 1
AND art.art_id = media_art.art_id
AND media.media_id = media_art.media_id


Yet here's where I mess up. To see if I've got them in an array, I do:

$media = mysql_fetch_assoc($media_result);

while ($media = mysql_fetch_assoc($media_result))
~
asort($media);

foreach($media as $key => $val)
~ echo "key: $key value: $val <br />";
~

//end while $media = mysql_fetch_assoc($media_result)


and this returns only the name of the HIGHer number of the two (that is,
5).
What have I done wrong to echo out each name of the media associated
with this record? I'm trying, eventually, to echo it out in the age so
that it appears as

[art_id 1] comprises Ink, watercolor.

Thanks in advance,
Jack

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


___________________________________________________________
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация