help with joining two arrays

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

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

help with joining two arrays

Post by glennn.php »

i can't figure out how to join the two arrays i have, one of song filenames and one of song titles
so that I can run one query that will print each $title with its associateed $song:

Code: Select all

$mp3 = trim($target_row[1]);
	$mp3s = explode(',', $mp3); 
	$title = trim($target_row[3]);
	$titles = explode(',', $title); 
	// $genre = trim($target_row[4]);
print "
<node>

/////////////////////////////////////	

foreach($mp3s as $song) 
	{
		print "<node label=\"".$title."\" src=\"audio/".$song."\" artist=\"".$artist."\" cover=\"../stars/".$pic."\" />";}
		print "
	</node>
can someone kindly lend a hand? i thank you,
g
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

array_combine() may be of interest.

The native version is PHP5+ but there are several PHP4 compatible versions posted below in the user comments.
glennn.php
Forum Commoner
Posts: 41
Joined: Sat Jul 08, 2006 12:26 pm

Post by glennn.php »

awesome - easy enough - i guess i didn't ask a thorough enough question, though. i can't see how to get the new array into three pairs that i need for:

Code: Select all

$mp3 = trim($target_row[1]);
	$mp3s = explode(',', $mp3); 
	$title = trim($target_row[3]);
	$titles = explode(',', $title); 
	$pair = array_combine($titles, $mp3s); 


////////

	foreach($pair  AS          ?           )
	{
		print "<node label=\"".$title."\" src=\"audio/".$mp3."\" ... ;
}
do i want to explode the new array first, somehow? and if so, would spacing between words be an issue?
glennn.php
Forum Commoner
Posts: 41
Joined: Sat Jul 08, 2006 12:26 pm

Post by glennn.php »

will i get away with all this in the array_combine() :

$pair = array_combine("label=".$titles, "src=\"audio/".$mp3s);


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

Post by glennn.php »

the answer to that is "no". could someone point me in the right direction so i'd know what q. to ask, or for what to seek in the tutes? then i'd be fine...

thanks
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

To reference the array keys and values after your array_combine() call:

Code: Select all

$mp3 = trim($target_row[1]);
        $mp3s = explode(',', $mp3);
        $title = trim($target_row[3]);
        $titles = explode(',', $title);
        $pair = array_combine($titles, $mp3s);
        foreach($pair as $title => $mp3)
        {
                print "<node label=\"".$title."\" src=\"audio/".$mp3."\">";
}
Got to ask though, whose idea was it to store the data in such a manner? It's totally inflexible, it's slow, and it'll go wrong if you have a title that contains a comma (which lots of songs do). It'd make a lot more sense to store each title/mp3 pair in a seperate record.
glennn.php
Forum Commoner
Posts: 41
Joined: Sat Jul 08, 2006 12:26 pm

Post by glennn.php »

yes, i agree and i thank you - the database was built with other needs in mind, then this came up. i'm really wanting to ensure that i don't run into problems later. what you just showed me taught me what i need to know, though.

if i may ask your advice, before i go too far with this, i'll likely want to optimize this whole procedure. may i show you the structure and ask your advice on how i might make room for multiple mp3/video entries per ARTIST?

Code: Select all

CREATE TABLE `stars` (
  `customerid` bigint(32) NOT NULL auto_increment,
  `clicks` varchar(255) NOT NULL default '',
  `picId` varchar(255) NOT NULL default '',
  `url` varchar(255) NOT NULL default '',
  `firstname` varchar(40) default NULL,
  `lastname` varchar(50) default NULL,
  `password` varchar(20) default NULL,
  `aka` varchar(255) NOT NULL default '',
  `title` text NOT NULL,
  `genre` varchar(50) NOT NULL default '',
  `album` varchar(50) NOT NULL default '',
  `email` varchar(255) default NULL,
  `website` varchar(255) default NULL,
  `city` varchar(50) default NULL,
  `state` varchar(50) default NULL,
  `talent` varchar(255) default NULL,
  `biography` text,
  `av` text NOT NULL,
  `approved` varchar(11) NOT NULL default '0',
  PRIMARY KEY  (`customerid`)
) TYPE=MyISAM PACK_KEYS=1 AUTO_INCREMENT=417 ;
the only thing dynamically occurring is that "clicks" is incrementing a click count for each Pic that's displayed in a grid and the script calls for the pics to be rendered ORDER by clicks. individual tables for EACH Pic (Artist) are created to hold a click IP for a given amount of time to prevent hammering clicks (there's value for Pic placement on the grid) - other than that the DB only holds data.

so if each ARTIST only has ONE Pic but several MP3s, would you mind offering a suggestion as to how i might do something for the mp3s? you're right i can see how the array of titles in one field can be a mess. this aftermarket deisgn just came up.

the individual tables won't help - too many queries?

i really appreciate it, anyone,
glenn nall


feyd | Come on now.. I showed you the

Code: Select all

..
tags to use for SQL queries..[/color]
glennn.php
Forum Commoner
Posts: 41
Joined: Sat Jul 08, 2006 12:26 pm

Post by glennn.php »

then why not offer a hand instead of playing COP, feyd? is that all you have to do is look for people breaking the rules? you can write repreimand but you can't offer a word of help?

i really appreciate it.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

what is the deal with people getting all <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span> at feyd... umm... buddy... look at his post count. You don't get that many posts by "just playing cop"
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

"Playing cop" as you put it often takes the wind out of a wish to help. If I didn't have to spend so much time fixing people's posts, I would post more. So seriously, have some patience. Someone will likely eventually answer your questions.
glennn.php
Forum Commoner
Posts: 41
Joined: Sat Jul 08, 2006 12:26 pm

Post by glennn.php »

it's so far taken both of you twice as much time to write this crap than it would have to answer someone's needs for some assistance.

i'm not worried about his post count - i'm marvelling at the fact that he'd rather take the time to reprimand ME than answer what i'm sure he could fix in a second - or even do BOTH.

that's not hard to understand, is it?

thanks for both of your help. i'll remember what forums not to hang out in so much.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

glennn.php wrote:i'm not worried about his post count - i'm marvelling at the fact that he'd rather take the time to reprimand ME than answer what i'm sure he could fix in a second - or even do BOTH.
His job as moderator is to moderate the forum... which he, and the other moderators do well. He only asked that you use sytax code to make it clean and easy to read so that others can help you more easily, so you being a crybaby about it isn't helping anybody... not even yourself.
glennn.php wrote:thanks for both of your help. i'll remember what forums not to hang out in so much.
Oh no! :cry:
glennn.php
Forum Commoner
Posts: 41
Joined: Sat Jul 08, 2006 12:26 pm

Post by glennn.php »

you're right. he only does that. clearly he even needs you for his defense.

i don't often, in forums where most people are mainly around to learn and to help others, run into pricks.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

glennn.php wrote:you're right. he only does that. clearly he even needs you for his defense.
Yes, that's exactly it. :roll:
glennn.php wrote:i don't often, in forums where most people are mainly around to learn and to help others, run into pricks.
I've tried helping you, and apparently you ignored my advice in those posts. You've now got three threads on the same fundamental problem: your database is poorly designed, and I said nicely previously.

Calling me a prick wins you zero votes and zero incentive for me to try to help you more.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

feyd does an excellent job here, as moderator and advisor.
Post Reply