Go Back   SitePoint Forums > Forum Index > Program Your Site > PHP
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Apr 9, 2004, 19:45   #1
JohnSaunders
Git-R-Done
 
Join Date: Nov 2001
Posts: 1,195
Pulling data in an array

Can somebody tell me how to go about pulling the song name, artist, and album name from a file that contains 10 of each? I've tried doing it myself using foreach, but I couldn't get it to work. The code below will create one line, but I would like it to create multiple lines of each song, artist, and album.

PHP Code:

$file = "../data.xml";
$xml_parser = xml_parser_create();

if ((
$fp = fopen($file, "r"))) {
$data = fread($fp, filesize($file));
fclose($fp);

xml_parse_into_struct($xml_parser, $data, $vals, $index);
xml_parser_free($xml_parser);

$title = $vals[2]["value"];
$artist = $vals[5]["value"];
$album = $vals[8]["value"];

/* here's what i tried but couldn't get it to work. I know the array values are wrong, but I'm not sure how to do it the right way.
$arr = array("$vals[2]","$vals[5]","$vals[8]");
reset ($arr);
while (list(, $value) = each ($arr)) {
    echo "<li>$value</li>";
}
foreach ($arr as $value) {
    echo "<li>$value</li>";
}
*/

echo "<li>$title by $artist from $album</li>";
}
Here's an example of the code in the XML file:

Code:
<?xml version="1.0" encoding="iso-8859-1" ?>
<now_playing playing="1">
<song order="1">
	 <title>Loco</title>
	 <artist>311</artist>
	 <album>311</album>
</song>
<song order="2">
	 <title>Purpose</title>
	 <artist>311</artist>
	 <album>311</album>
</song>
<song order="3">
	 <title>Misdirected Hostility</title>
	 <artist>311</artist>
	 <album>311</album>
</song>
<song order="4">
	 <title>Don't Let Me Down</title>
	 <artist>311</artist>
	 <album>311</album>
</song>
<song order="5">
	 <title>Sweet</title>
	 <artist>311</artist>
	 <album>311</album>
</song>
</now_playing>
Any help would be greatly appreciated.
JohnSaunders is offline   Reply With Quote
Old Apr 10, 2004, 01:09   #2
mattjacob
SitePoint Wizard
 
Join Date: Oct 2001
Location: Tucson, Arizona
Posts: 1,921
This is kind of messy, but it should work:
PHP Code:

$file = "data.xml";

$xml_parser = xml_parser_create();

if (
false != ($data = file_get_contents($file)))
{
    
xml_parse_into_struct($xml_parser, $data, $vals, $index);
    
xml_parser_free($xml_parser);

    
$filter = array();

    
// Process data

    
foreach ($index as $key => $val)
    {
        if (
'TITLE' == $key || 'ARTIST' == $key || 'ALBUM' == $key)
        {
            foreach (
$val as $key1 => $val1)
            {
                
$filter[$key1][$key] = $vals[$val1]['value'];
            }
        }
    }

    
// Output data

    
echo '<ul>';

    foreach (
$filter as $f)
    {
        echo
"<li>{$f['TITLE']} by {$f['ARTIST']} from {$f['ALBUM']}</li>";
    }

    echo
'</ul>';
}
mattjacob is offline   Reply With Quote
Old Apr 10, 2004, 10:58   #3
JohnSaunders
Git-R-Done
 
Join Date: Nov 2001
Posts: 1,195
Thanks!

Last edited by JohnSaunders; Apr 10, 2004 at 11:33.
JohnSaunders is offline   Reply With Quote
Old Apr 10, 2004, 11:27   #4
mattjacob
SitePoint Wizard
 
Join Date: Oct 2001
Location: Tucson, Arizona
Posts: 1,921
I should mention that you need to be using PHP >= 4.3.0 in order for file_get_contents() to work.
mattjacob is offline   Reply With Quote
Old Apr 10, 2004, 11:33   #5
JohnSaunders
Git-R-Done
 
Join Date: Nov 2001
Posts: 1,195
mattjacob,

I was able to get it to work. Could you tell me how to make it so I can specify the number of songs to be listed? For example, if the file contains 100 songs, I'd just like to be able to show 10 of them.
JohnSaunders is offline   Reply With Quote
Old Apr 10, 2004, 11:35   #6
mattjacob
SitePoint Wizard
 
Join Date: Oct 2001
Location: Tucson, Arizona
Posts: 1,921
In the output section, use a for loop instead of a foreach loop. In fact, it would look something like this:
PHP Code:

for ($i = 0; $i < 10; $i++)

{
    echo
"<li>{$filter[$i]['TITLE']} by {$filter[$i]['ARTIST']} from {$filter[$i]['ALBUM']}</li>";
}
Just make sure you actually have at least 10 entries, otherwise you'll get undefined offset notices all over the place.
mattjacob is offline   Reply With Quote
Old Apr 10, 2004, 11:55   #7
JohnSaunders
Git-R-Done
 
Join Date: Nov 2001
Posts: 1,195
Thanks again!
JohnSaunders is offline   Reply With Quote
Old Apr 10, 2004, 19:37   #8
Zoef
Ceci n'est pas Zoef
 
Zoef's Avatar
 
Join Date: Nov 2002
Location: Malta
Posts: 1,120
You could also break out of the foreach loop:
PHP Code:

$max = 10;

$i = 0;
foreach (
$filter as $f)
{
    echo
"<li>{$f['TITLE']} by {$f['ARTIST']} from {$f['ALBUM']}</li>";
    
$i++;
    if (
$i >= $max) break;
}
That should not give you any notices, even if the number of songs is less then 10.

Rik
Zoef is offline   Reply With Quote
Old Apr 11, 2004, 00:20   #9
JohnSaunders
Git-R-Done
 
Join Date: Nov 2001
Posts: 1,195
In addition to selecting 10 songs from the file, I would also like to alternate the row colors of the table. I found a script that will do this using MySQL, but I'm just not sure how to convert it to work with the script I'm using. If somebody could tell me how to do this I would appreciate it.

PHP Code:

$color1 = "#CCFFCC"; 
$color2 = "#BFD8BC";
$row_count = 0;
while (
$row = mysql_fetch_array($sql_events)) {
    
$event_date = $row["event_date"];
    
$event_title = $row["event_title"];

    
/* Now we do this small line which is basically going to tell
    PHP to alternate the colors between the two colors we defined above. */

    
$row_color = ($row_count % 2) ? $color1 : $color2;

    
// Echo your table row and table data that you want to be looped over and over here.

    
echo "<tr>
    <td width="
110" bgcolor="$row_color" nowrap>
    $article_date</td>
    <td bgcolor="
$row_color">
    <a href="
articles.php?cmd=full_article&article_id=$article_id">$article_title</a></td>
    </tr>"
;

    
// Add 1 to the row count

    
$row_count++;
}
JohnSaunders is offline   Reply With Quote
Old Apr 12, 2004, 23:26   #10
JohnSaunders
Git-R-Done
 
Join Date: Nov 2001
Posts: 1,195
I figured out how to alternate the table rows, however whenever a value contains an ampersand "&" it cuts off the text in the remaining fields. Does anybody know how to make it so the & wont' screw up the values?
JohnSaunders is offline   Reply With Quote
Old Apr 13, 2004, 06:39   #11
Zoef
Ceci n'est pas Zoef
 
Zoef's Avatar
 
Join Date: Nov 2002
Location: Malta
Posts: 1,120
That's probably because the content hasn't been encoded properly. Try with &amp;

Rik
Zoef is offline   Reply With Quote
Old Apr 13, 2004, 07:39   #12
JohnSaunders
Git-R-Done
 
Join Date: Nov 2001
Posts: 1,195
I'm not really sure how to go about doing this. I tried this method but the problem is still occuring so I must not be doing something correctly.

PHP Code:

for ($i = 0; $i < 10; $i++) { 
$song = "{$filter[$i]['TITLE']}";
$song = preg_replace ("'\&'", "&amp;", $song);
echo
"<li>$song by {$filter[$i]['ARTIST']} from {$filter[$i]['ALBUM']}</li>";
}
Any help would be greatly appreciated.
JohnSaunders is offline   Reply With Quote
Old Apr 13, 2004, 10:13   #13
mattjacob
SitePoint Wizard
 
Join Date: Oct 2001
Location: Tucson, Arizona
Posts: 1,921
You could try something like this:
PHP Code:

for ($i = 0; $i < 10; $i++)

{
    echo
htmlspecialchars("<li>{$filter[$i]['TITLE']} by {$filter[$i]['ARTIST']} from {$filter[$i]['ALBUM']}</li>");
}
mattjacob is offline   Reply With Quote
Old Apr 13, 2004, 10:20   #14
JohnSaunders
Git-R-Done
 
Join Date: Nov 2001
Posts: 1,195
mattjacob,

Thanks for your help (again). I tried it out and it's converting the <li></li> tags so they show up in the web page. Could you tell me how to make it so it won't convert the tags?
JohnSaunders is offline   Reply With Quote
Old Apr 13, 2004, 10:22   #15
mattjacob
SitePoint Wizard
 
Join Date: Oct 2001
Location: Tucson, Arizona
Posts: 1,921
Oops--I'm still waking up over here.
PHP Code:

for ($i = 0; $i < 10; $i++)

{
    echo
'<li>';
    echo
htmlspecialchars("{$filter[$i]['TITLE']} by {$filter[$i]['ARTIST']} from {$filter[$i]['ALBUM']}");
    echo
'</li>';
}
mattjacob is offline   Reply With Quote
Old Apr 13, 2004, 10:31   #16
JohnSaunders
Git-R-Done
 
Join Date: Nov 2001
Posts: 1,195
Unfortunately, it's still cutting everything off whenever there's an &. Other things like $, -, ', etc. do not cause a problem.

Here's an example of what's going on:

xml file:
Code:
<title>Coffee & TV</title>
<artist>Blur</artist>
<album>Blur</artist>
Here's what it looks like on the webpage:

Coffee - nothing - nothing

Any ideas?
JohnSaunders is offline   Reply With Quote
Old Apr 13, 2004, 10:34   #17
mattjacob
SitePoint Wizard
 
Join Date: Oct 2001
Location: Tucson, Arizona
Posts: 1,921
I haven't done too much work with XML in PHP, but my guess is that the XML parser is eating the &. Try changing the & in the file to &amp; and see if that helps.
mattjacob is offline   Reply With Quote
Old Apr 13, 2004, 19:04   #18
JohnSaunders
Git-R-Done
 
Join Date: Nov 2001
Posts: 1,195
mattjacob,

Unfortunately, the & is coming off of a CD so I can't change it to &amp;.

According to this http://us4.php.net/manual/en/functio...ta-handler.php, this guy (about half way down) says that you need to rewrite every ampersand as &amp; in the input stream. How do I do that? I've tried the following and have moved the str_replace before the xml_parse_into_struct but I get an error each time:

PHP Code:

$xml_parser = xml_parser_create();

if (
false != ($data = file_get_contents($file))) {
    
xml_parse_into_struct($xml_parser, $data, $vals, $index);
    
$xml_parser = str_replace ("&", "&amp;", $xml_parser);
    
xml_parser_free($xml_parser);
produces this:

Code:
Warning: xml_parser_free(): supplied argument is not a valid XML Parser resource
Do you or anybody else have any idea how to do this on the input stream?
JohnSaunders is offline   Reply With Quote
Old Apr 13, 2004, 19:31   #19
JohnSaunders
Git-R-Done
 
Join Date: Nov 2001
Posts: 1,195
Holy $%^# I figured it out! It needed this:

PHP Code:

    $data= str_replace ("&", "&amp;", $data); 

JohnSaunders is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 16:14.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved