Good, I created an XML document with the name and title of songs and artists within that document come also the corresponding cover. Now the link of my XML file is: http://inlivefm.96.lt/nw.xml .
I also have another XML document that gives me the name and artist title that this amounts to the moment (ADELE - HELLO) Now the link of my XML file is: http://inlivefm.96.lt/NowOnAir.xml .
Well, what I'm trying to make is that with document I created give me the cover of this song to give right now. I tried to make a code but to no avail in PHP, so I came to ask your help to get success.
Here I leave the code I am using to try to get what I want.
<?php
$xml = simplexml_load_file('http://inlivefm.6te.net/agora.xml');
$artist = urlencode($xml->Event->Song->Artist['name']);
$track = urlencode($xml->Event->Song['title']);
$url = simplexml_load_file("http://inlivefm.96.lt/nw.xml");
$largeImage = $url->xpath('/ilm/$artist- $track/image[#size="cover"]')[0];
echo '<img src="'.$largeImage.'" />';
?>
Can you please help me doing this?
Thank you all in advance.
correct xpath to take image is
(/ilm/artist/image[#size="cover"])[count(/ilm/artist/name[.="ADELE-HELLO"]/preceding-sibling::name)+1]
because you need to get image with the same position as artist's name.
In php code it should be something like this:
$XML = simplexml_load_file('http://inlivefm.6te.net/agora.xml');
$artist = $XML->Event->Song->Artist['name'];
$track = $XML->Event->Song['title'];
$URL = simplexml_load_file("http://inlivefm.96.lt/nw.xml");
$largeImage = $URL->xpath('(/ilm/artist/image[#size="cover"])[count(/ilm/artist/name[.="'. $artist .' - '. $track.'"]/preceding-sibling::name)+1]');
echo '<img src = "'. $largeImage[0]. '" />';
Related
say i have html code like this
$html = "This is some stuff right here. OH MY GOSH";
i am trying to get values of href and also on which anchor work i mean check this out text i am able to get href value by following this code
$displaybody->find('a ') as $element;
echo $element;
well it works for me but how do i get value of check this out could you guys help me out. i did search but i am not able to find it out . thanks in advance
my actual html look like this
» Download MP4 « - <b>144p (Video Only)</b> - <span> 19.1</span> MB<br />
my href look like this above code return download mp4 and i want it like downloadmp4 114p (video only) 19.1 mb how do i do that
If what you are using now is the SimpleHTMLDOM, then ->innertext works fine on that anchor elements that you have found:
include 'simple_html_dom.php';
$html = "This is some stuff right here. OH MY GOSH";
$displaybody = str_get_html($html);
foreach($displaybody->find('a ') as $element) {
echo $element->innertext . '<br/>';
}
If you were referring to PHP's DOMDocument, then its not find() function you need to use, to target each anchor element, you need to use ->getElementsByTagName(), then each selected elements you need to use ->nodeValue:
$html = "This is some stuff right here. OH MY GOSH";
$dom = new DOMDocument();
$dom->loadHTML($html);
foreach($dom->getElementsByTagName('a') as $element) {
echo $element->nodeValue . '<br/>';
}
As I want to understand Simple HTML Dom a bit I am playing around with it, to test options on my localhost.
Basically I want to take the titles and intro's of this website and display them on my page.
The title as <h2> and the intro as <p>.
What am I doing wrong?
<?php
include 'simple_html_dom.php';
// Create DOM from URL
$html = file_get_html('http://www.nu.nl/algemeen');
foreach($html->find('div[class=list-overlay]') as $article){
$title['intro'] = $article->find('span[class=title]', 0)->innertext;
$intro['details'] = $article->find('span[class=excerpt]', 0)->innertext;
echo '<h2>'. $articles . '</h2>
<p>'. $title .'</p>';
}
?>
edit: There was a double line in there.
Your soulution is somehow right. You have only few typos in variable names. Here is my editation of your code. Also I have added few comments to help you understand.
<?php
include 'simple_html_dom.php';
// Create DOM from URL
$html = file_get_html('http://www.nu.nl/algemeen');
// exctract all elements matching selector div[class=...]
foreach($html->find('div[class=list-overlay]') as $article){
// and for each extract first (0) element that matches to span[class=title]
$title = $article->find('span[class=title]', 0)->innertext;
// and do the same for intro, extract first element that belongs to selector
$intro = $article->find('span[class=excerpt]', 0)->innertext;
// and write it down simply
echo '<h2>'. $title . '</h2>';
echo '<p>' . $intro . '</p>';
}
?>
This solution isn't good though. The have bad structure of their HTML so it is not easy to select only articles, because they don't have them in div with ID articles (for example. You are lucky man anyway, because they provide you XML feed of their articles that is much easier to parse (also less data to transfer and so on). You can find it here and extract titles and intros for your website.
I'm trying to do a PHP page to shows an album cover via Last.FM API. However, the Artist Name and the Title of the music are provided by a XML file that a software updates via FTP.
Here is the code of Last.FM api:
<?php
$img = simplexml_load_file('http://ws.audioscrobbler.com/2.0/?method=track.getInfo&api_key=<APIKEY>&artist=cher&track=believe');
echo '<img src="';
echo $img->track[0]->album[0]->image[3];
echo '">';
?>
Now the link of my XML file is: http://summerblast.pt/avaplayer/rds.xml
The info I need to the Last.FM API is in 'OnAir/CurMusic'.
Well, what I am trying to do is change "Cher" and "Believe" (the artist's name and the Title's name) in the link of the "simplexml_load_file" (in the php code) with the info that my XML file provides.
Can you please help me doing this?
Thank you all in advance.
Sorry was confused to what you were asking from before. I have gone ahead and corrected to do what you wanted.
You can get the Artist and the Title from rds.xml and just pass it to the URL like the following listed below (note it is important to run urlencode on them since they can have spaces and other things to break the URL):
Updated to do error checking
$xml = simplexml_load_file('http://summerblast.pt/avaplayer/rds.xml');
$artist = urlencode($xml->OnAir->CurMusic->Artist);
$track = urlencode($xml->OnAir->CurMusic->Title);
$url = 'http://ws.audioscrobbler.com/2.0/?method=track.getInfo&api_key=XXXXXXXXXXXXXXXXXXXXXXXXXXX&artist='.$artist.'&track='.$track;
$xml2 = #simplexml_load_file($url);
if ($xml2 === false)
{
echo("Url failed"); // do whatever you want to do
}
else
{
if($xml2->track->album->image[3])
{
echo '<img src="';
echo((string) $xml2->track->album->image[3]);
echo '">';
}
else
{
echo("artist does not have a image"); // do whatever you want to do
}
}
Hello I have a webcam 'foscom' that send automatic snapshot every 30minutes a pictures to this folder in my ftp "testcamera/img"
I was able to create a code that display the latest image uploaded in php.
But i want also to display a drop down menu that display all old image that's mean i will let the viewer to choose another picture and display it.
An example of the idea i want: http://www.skileb.com/webcam/mzaarv/
My Sample look like this: www.stevendahdah.com/testcamera/index.php
So anyone can help me?
Here is a link that will get you started :
sort files by date in PHP
Getting the first or last file is merely getting the correct element from your sorted array :
$latest_file = $files[1];
$oldest_file = $files[count($files)];
You can create a image tag
echo '<img src="/dir/where/files/are/'.$latest_file . '" />';
For the rest, you can create links
for ($i = 2; $i <= count($files); $i++) {
echo '<a href="/dir/where/files/are/'.$latest_file . '" />' . $latest_file . '</a>';
}
I have a challenge with a wordpress theme I'm developing, but I think what I'm after is really just a general php/JS solution, nothing Wordpress specific.
So I have the below code with pulls in the thumbnail and description for an array of images I have uploaded. What I'd like to do is when a user clicks on the link the description and caption associated with that image is displayed in a div elsewhere on the page.
My issue is that so far the only way I can think of to do that is to set a javascript variable within my php foreach statement, but the problem with that is that this overwrites the variable value each time (as I can't change the variable name) so by the end of it all I don't have a different JS variable for each image, I just have one with the details from the last image in the array.
I know AJAX might be one way forward, but I know pretty much nothing about it. If anyone can help point me in the right direction I'd really appreciate it.
Thanks
<?php
$gallery_images = get_custom_field('galleryImages:to_array');
foreach ($gallery_images as $galleryID) {
$description = $attachment->post_content; //get image description
$caption = $attachment->post_excerpt; //get image caption
?>
link
<?php
}
?>
<div id="targetDiv"></div>
I think you're going about this the wrong way, personally. Using AJAX to interact with a WordPress site seems like overkill for the simple ability of showing some peripheral information about an image.
What I would do is have WordPress spit out the image, along with the caption info when the page is initially downloaded, but hide the caption info and then use client-side JavaScript to show/hide it when it's needed.
<?php
$button_html = "";
$caption_html = "";
$gallery_images = get_custom_field('galleryImages:to_array');
$gallery_images_count = 0;
foreach ($gallery_images as $galleryID) {
$description = $attachment->post_content; //get image description
$caption = $attachment->post_excerpt; //get image caption
$button_html .= '<div id="caption-button-' . $gallery_images_count . '" class="show-caption-button">Show Caption</div>';
$caption_html .= '<div id="caption-' . $gallery_images_count . '" style="display:none;">' . $caption . '</div>';
$gallery_images_count++;
}
echo '<div id="buttonDiv">' . $button_html . '</div>';
echo '<div id="targetDiv">' . $caption_html . '</div>';
?>
Then the JavaScript/jQuery:
$('.show-caption-button').click(function(){
var caption_id = $(this).prop('id').substring(15);
$('#caption-'+caption_id).eq(0).toggle();
});
It's hard to test without setting up a WordPress myself, but essentially what we're doing is adding caption divs with numbered id's to a string variable in PHP as we're looping through our images. Then, at the end of the loop, we echo that out to the page.
In JavaScript/jQuery, we're grabbing the corresponding id of the caption button and using it to toggle the relevant caption further down in the page.