Page 1 of 1
help with joining two arrays
Posted: Tue Aug 22, 2006 10:01 am
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
Posted: Tue Aug 22, 2006 10:07 am
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.
Posted: Tue Aug 22, 2006 10:35 am
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?
Posted: Tue Aug 22, 2006 10:39 am
by glennn.php
will i get away with all this in the array_combine() :
$pair = array_combine("label=".$titles, "src=\"audio/".$mp3s);
?
Posted: Tue Aug 22, 2006 10:49 am
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
Posted: Tue Aug 22, 2006 1:12 pm
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.
Posted: Tue Aug 22, 2006 1:34 pm
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 tags to use for SQL queries..[/color]
Posted: Tue Aug 22, 2006 4:33 pm
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.
Posted: Tue Aug 22, 2006 4:36 pm
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"
Posted: Tue Aug 22, 2006 4:42 pm
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.
Posted: Tue Aug 22, 2006 4:49 pm
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.
Posted: Tue Aug 22, 2006 4:56 pm
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!

Posted: Tue Aug 22, 2006 5:01 pm
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.
Posted: Tue Aug 22, 2006 5:17 pm
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.
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.
Posted: Wed Aug 23, 2006 8:47 am
by volka
feyd does an excellent job here, as moderator and advisor.