I have a script which parses user's iTunes library files, in XML format and logs it to a DB so it can be displayed in their forum profile. Before it gets entered in the DB, some cleaning up/verification is done to the elements, i.e. song title, genre etc. I've written a function to do this and put it in a seperate functions file with some other ones. One of the verifications is to check that the current item in the XML file is not a movie file, this appears to be the only one of the verification steps that actually works. Also, the function (should) display a message which says why the track wasn't accepted, e.g. not long enough, missing song title etc. This message isn't displayed on the page. It also tries to fill in some non-essential data if it's missing, so if there's no genre defined for the track it replaces it with 'unknown'. Anyway, here's the function function checkrow(&$song, &$msg) { global $song; global $msg; if(strpos($song['Kind'], 'movie') || strpos($song['Kind'], 'video')) { return false; return $msg = 'Not added - movie file'; } else { if($song['Total Time'] <= 30000) { return $msg = $song['Song'] . ' - Not added - too short (30s)'; return false; } else { if(empty($song['Genre']) || !isset($song['Genre']) || strlen($song['Genre'] == 0)) { $song['Genre'] = 'Unknown'; return true; } else { if(empty($song['Equalizer']) || !isset($song['Equalizer']) || strlen($song['Equalizer'] == 0)) { return $song['Equalizer'] = 'Unknown'; return true; } else { if(empty($song['Artist'])) { return $msg = $song['Name'] . ' - Not added - no artist name!'; return false; } else { if(empty($song['Name'])) { return $msg = $song['Artist'] . ' - Not added - no song title!'; return false; } else { return true; } } } } } } } PHP: After a couple of tries at getting it working, I just tried doing random stuff like the globals, references etc. Didn't seem to change anything. Here's how it's called foreach ($songs as $song) { $song_name = mysql_real_escape_string($song['Name']); $song_artist = mysql_real_escape_string($song['Artist']); $song_album = mysql_real_escape_string($song['Album']); $song_genre = mysql_real_escape_string($song['Genre']); $song_kind = mysql_real_escape_string($song['Kind']); $song_size = mysql_real_escape_string($song['Size']); $song_total_time = mysql_real_escape_string($song['Total Time']); $song_year = mysql_real_escape_string($song['Year']); $song_bit_rate = mysql_real_escape_string($song['Bit Rate']); $song_sample_rate = mysql_real_escape_string($song['Sample Rate']); $song_equalizer = mysql_real_escape_string($song['Equalizer']); $song_play_count = mysql_real_escape_string($song['Play Count']); if(checkrow($song, $msg)) { $query = $db->sql_query("massive query"); echo "Added: " . stripslashes($song_artist) . " - " . stripslashes($song_name) . "<br>"; } else { echo 'Didn\'t add: ' . stripslashes($song_name) . $msg . '<br>'; } } PHP: If it's of any use, here's what kind of data the $song array will hold Array ( [Track ID] => 361 [Name] => E-Pro [Artist] => Beck [Album] => Guero [Genre] => Indie Rock [Kind] => MPEG audio file [Size] => 8100648 [Total Time] => 202422 [Track Number] => 1 [Year] => 2005 [Date Modified] => 2007-11-13T08:40:37Z [Date Added] => 2007-07-24T23:08:04Z [Bit Rate] => 320 [Sample Rate] => 44100 [Volume Adjustment] => 10 [Equalizer] => Rock [Play Count] => 2 [Play Date] => 3277830817 [Play Date UTC] => 2007-11-13T20:33:37Z [Persistent ID] => B237C9FF94338189 [Track Type] => File [Location] => file://localhost/C:/My Music/Beck/Guero/01 E-Pro.mp3 [File Folder Count] => 4 [Library Folder Count] => 1 ) Code (markup): Sorry for the massive post.. To sum up.. the f(strpos($song['Kind'], 'movie') || strpos($song['Kind'], 'video')) PHP: part is working, it doesn't accept movie files. What doesn't work is the replacing of equalizer/genre with "unknown", i'm not sure whether the other verifiers work either to be honest.
You are misinterpreting the returns in the function. You cannot have 2 returns, the second one will overlap the first. if(strpos($song['Kind'], 'movie') || strpos($song['Kind'], 'video')) { return false; return $msg = 'Not added - movie file'; } PHP: Correct way to do it is: if(strpos($song['Kind'], 'movie') || strpos($song['Kind'], 'video')) { echo 'Not added - movie file'; return false; } PHP:
Thanks, that seems to have cleaned it up a little bit. But the genre and equalizer variables aren't being rewritten by the ones in the function. I added a couple of "debug" messages which do output "missing genre/equalizer" but don't overwrite the variable. Here's what the function looks like now: function checkrow(&$song, &$msg) { // global $song; // global $msg; if(strpos($song['Kind'], 'movie') || strpos($song['Kind'], 'video')) { return false; $msg = 'Not added - movie file<br>'; } else { if($song['Total Time'] <= 30000) { $msg = $song['Song'] . ' - Not added - too short (30s)<br>'; return false; } else { if(empty($song['Genre']) || !isset($song['Genre'])) { $song['Genre'] = 'Unknown'; $msg = "Genre missing <br>"; return true; } else { if(empty($song['Equalizer']) || !isset($song['Equalizer'])) { $song['Equalizer'] = 'Unknown'; $msg = "No equalizer <br>"; return true; } else { if(empty($song['Artist'])) { $msg = $song['Name'] . ' - Not added - no artist name!<br>'; return false; } else { if(empty($song['Name'])) { $msg = $song['Artist'] . ' - Not added - no song title!<br>'; return false; } else { $msg = "added<br>"; return true; } } } } } } } PHP: I'm pretty sure it's to do with the global $song/$msg things or the references.
For a complicated if and else like that, it would look more clean using a switch. BTW, why are you using a ampersand in the function parameter?
They're references apparently, I put them in there because I was just scrambling to try and get it to work and I was trying everything basically. Anyway, i've cleaned up the ifs and it's displaying messages nicely. But, it still doesn't overwrite the genre and equalizer variables from within the function. If anyone could provide a solution I would be most greatful.