two foreach loops - not sure how

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
glennn.php
Forum Commoner
Posts: 41
Joined: Sat Jul 08, 2006 12:26 pm

two foreach loops - not sure how

Post by glennn.php »

this is hard to describe - i need two arrays returned in a string: each mp3 will have a title: oh heck, i'll just show you:

Code: Select all

while($target_row = mysql_fetch_array($target_qid))
	{
	$pic = trim($target_row[0]);
	$pic = ereg_replace(" ", "%20", $pic);
	$artist = trim($target_row[2]);
	$mp3 = trim($target_row[1]);
	$mp3s = explode(',', $mp3); 
	$title = trim($target_row[3]);
	$titles = explode(',', $title); 
	// $genre = trim($target_row[4]);
print "
<node>
	<node label=\"".$artist."\">";
	foreach($mp3s as $song)
	{
		print "<node label=\"".$title."\" src=\"audio/".$song."\" artist=\"".$artist."\" cover=\"../stars/".$pic."\" />";}
		print "
	</node>
</node>";
}
?>
i need foreach $song AND foreach $title coinciding - i have three songs (in my test query) and my attempts have returned NINE combinations, vice just the correct THREE.

i can't find the simple solution, only scads of complex foreach loops ... can someone help?

thanks
g
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

You should probably be doing the join in your SQL and only querying once.

You should also avoid referencing the returned fields numerically, get MySQL to return the result set as an associative array of fields and your code will be much more self documenting.
glennn.php
Forum Commoner
Posts: 41
Joined: Sat Jul 08, 2006 12:26 pm

Post by glennn.php »

right - i've been wondering if i should have had this done before the query, in the db or in the SQL. don't know enough to just go do it, and i was already so close to being done.

thanks - and i see that about $target_row["title"]; versus [1]. didn't know if there were any subtle reasons for one over the other...

thanks. so i need to join these two arrays somehow.

"I'll go look it up."

:o) thanks
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post by blackbeard »

It looks like you have in each row of the table multiple MP3's and their titles. Something like this:

Column 0: "Pic"
Column 1: "MP3_1, MP3_2, MP3_3"
Column 2: "Artist name"
Column 3: "Title_1, Title_2, Title_3"


If that's the case, I'd think you be better off re-designing your table setup to have one MP3 / Title pair, rather than using PHP to seperate them out after the query.
glennn.php
Forum Commoner
Posts: 41
Joined: Sat Jul 08, 2006 12:26 pm

Post by glennn.php »

that's easy - i haven't made a commitment yet to the db - so i should enter into one row title1, mp31, title2, mp32 ...?

i still don't see how i'd get them stored in alternate variables...

i tried array_combine($titles, $mp3s); but that looks like i'd pretty much get the same thing, one array of title, song, title, song etc...

i'm sure the solutions a snap, it's just one (of the many) of which i'm not familiar.

thanks for any help.

g
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post by blackbeard »

Set up the table with 4 fields, such as:
  • pic
    Artist
    MP3
    Title
When you enter information into that table, only include one piece of information per field. This way, when you run your query, you won't have to parse out data on a per field basis.
glennn.php
Forum Commoner
Posts: 41
Joined: Sat Jul 08, 2006 12:26 pm

Post by glennn.php »

well, the problem with that is that the table is already designed for "Artists" (i meant earlier that i wasn't commited to the row, not the entire DB), each artist having many fields, of course (each one is also aready assigned an indivual table for other reasons) - when this need arose i just created a new row in which to enter an array of mp3 names and a row for titles. an artist only has one Pic.

with already over 400 artists and expectations to grow soon, i'm trying to prevent running into a bottleneck later. i'm thinking that i might need to pay close attention to how this is automated right now, rather than later...

i don't want to end up manually entering a Artist name, Pic name, Mp3 name, Title array for each mp3 that's uploaded... do i?
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post by blackbeard »

so what the relationship is is that each artist is unique, has a unique pic, and can have multiple MP3's / Titles?

If so, then what you can do is have 2 tables. The first table contains the artist and their pic. The second table would contain
the artist name, MP3, and title. The artist name would be what ties the two tables together, and you would use a join on the two tables to get the information you need.
paladaxar
Forum Commoner
Posts: 85
Joined: Fri Jun 18, 2004 11:50 pm

Post by paladaxar »

I think you need some serious re-structuring of your db. You'll be glad you did in the end. If you are not familiar with DB normalization, read this article.

http://www.oreilly.de/catalog/javadtabp/chapter/ch02.pdf

It's written for Java, but the idea is the same. AND, it just happens to use an example that seems very close to what you are trying to do with artists, track names, etc.
glennn.php
Forum Commoner
Posts: 41
Joined: Sat Jul 08, 2006 12:26 pm

Post by glennn.php »

awesome, thanks --- yes, i'm sure i do - i just built the thing for the needs i had at the time...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Didn't I already go over this? Yeah, I did.

viewtopic.php?p=300820#300820
Post Reply