|
|||||||
New to SitePoint Forums? Register here for free!
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
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:
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> |
|
|
|
|
|
#2 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Oct 2001
Location: Tucson, Arizona
Posts: 1,921
|
This is kind of messy, but it should work:
PHP Code:
|
|
|
|
|
|
#3 |
|
Git-R-Done
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Nov 2001
Posts: 1,195
|
Thanks!
Last edited by JohnSaunders; Apr 10, 2004 at 11:33. |
|
|
|
|
|
#4 |
|
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.
|
|
|
|
|
|
#5 |
|
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. |
|
|
|
|
|
#6 |
|
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:
|
|
|
|
|
|
#7 |
|
Git-R-Done
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Nov 2001
Posts: 1,195
|
Thanks again!
|
|
|
|
|
|
#8 |
|
Ceci n'est pas Zoef
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Nov 2002
Location: Malta
Posts: 1,120
|
You could also break out of the foreach loop:
PHP Code:
Rik |
|
|
|
|
|
#9 |
|
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:
|
|
|
|
|
|
#10 |
|
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?
|
|
|
|
|
|
#11 |
|
Ceci n'est pas Zoef
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Nov 2002
Location: Malta
Posts: 1,120
|
That's probably because the content hasn't been encoded properly. Try with &
Rik |
|
|
|
|
|
#12 |
|
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:
|
|
|
|
|
|
#13 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Oct 2001
Location: Tucson, Arizona
Posts: 1,921
|
You could try something like this:
PHP Code:
|
|
|
|
|
|
#14 |
|
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? |
|
|
|
|
|
#15 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Oct 2001
Location: Tucson, Arizona
Posts: 1,921
|
Oops--I'm still waking up over here.
![]() PHP Code:
|
|
|
|
|
|
#16 |
|
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> Coffee - nothing - nothing Any ideas? |
|
|
|
|
|
#17 |
|
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 & and see if that helps.
|
|
|
|
|
|
#18 |
|
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 &. 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 & 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:
Code:
Warning: xml_parser_free(): supplied argument is not a valid XML Parser resource |
|
|
|
|
|
#19 |
|
Git-R-Done
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Nov 2001
Posts: 1,195
|
Holy $%^# I figured it out!
It needed this:PHP Code:
|
|
|
|
![]() |
| Bookmarks |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
|
All times are GMT -7. The time now is 16:14.





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?
It needed this:


Hybrid Mode
