Page 1 of 1
How do I display the picture from a link within an xml field
Posted: Thu Nov 13, 2008 1:06 pm
by ben80
Hi,
I am writing a website and am new to this game. I have written some php that will extract and display the text fields from the xml data sheet, however for each product, there is a link to the image. What would be the php code for me to display the image itself and not the link to the image, on my website?
I hope this is enough information. Any help would be appreciated.
Thanks,
Ben
Re: How do I display the picture from a link within an xml field
Posted: Thu Nov 13, 2008 2:18 pm
by shiznatix
well if you have the full URL of the image, why not just use something like
Code: Select all
<img src="<?php echo $piece['imgURL'] ?>" />
replace the $piece['imgURL'] with the appropriate variable/key
Re: How do I display the picture from a link within an xml field
Posted: Thu Nov 13, 2008 5:01 pm
by ben80
Thanks for that. I think I am close, but not quite there, would you be able to help a little more?
Here is my code. You can see how where I've tried to get the image to display and the above live of text which does display fine:
<td>
<?php
$DVDs = simplexml_load_file('Feed.xml');
foreach ($DVDs->BoxsetType as $BoxsetType){
printf($BoxsetType->name);
}
?>
</td>
<td>
<?php
$DVDs = simplexml_load_file('Feed.xml');
foreach ($DVDs->BoxsetType as $BoxsetType){
$imagetest = '$BoxsetType->imageURL';
echo "<img src='$imagetest'>";
}
?>
</td>
Thanks for all you help!
Ben
Re: How do I display the picture from a link within an xml field
Posted: Fri Nov 14, 2008 7:02 am
by shiznatix
first, please oh please use the code tags when posting code, its impossible to read anything without them.
Anyway, there are several problems with your code. First, why are you using printf() on the $BoxsetType->name? Printf is used to put variables into a string, what you want is echo. echo just prints out the variable to the screen, no need to put it through a fancier function like printf when you don't gain anything from it.
Next, the main problem, you are copying a variable for reasons unknown and doing it very wrong in the first place. Have you tried viewing the source code of your page with this on it? If so you will notice that all image src's are exactly "$BoxsetType->imageURL". This is because when you put something in single quotes ' then it is treated as a string, you can't do $var = 'asdf'; $newVar = '$var'; because $newVar will equal exactly "$var" instead of equaling "asdf". There is no need for quotes if you want to copy a variable, but you can use double quotes in the manner you are thinking. Example:
Code: Select all
$var = '123';
$newVar = '$var';//notice the single quotes
echo $newVar;//outputs "$var"
//now we try this:
$var = '123';
$newVar = "$var";//notice the double quotes
echo $newVar;// outputs "123"
//but really what you want to do is this:
$var = '123';
$newVar = $var;//notice the lack of quotes, there is no need for them in a situation like this
echo $newVar;//outputs "123"
The last problem is the src=' <-- notice the single quotes. Always use double quotes " when using HTML tags. It should read
Code: Select all
echo '<img src="'.$imagetest.'" />';//notice the double quotes around where the URL will be.
Now with all that said, I believe you can fix your script. Good luck and if you still have problems post back and more advice can be given.
Re: How do I display the picture from a link within an xml field
Posted: Fri Nov 14, 2008 9:00 am
by ben80
Thanks for your help. Based on your comments I have removed the variable and simply left it as a straight link (although you have taught how to use variables correctly in future so thanks!). I have also removed the "printf" functions and replaced them with echo on your recommendation. Sorry if I'm being a complete noob, but I still cant get it to work. Below is my new version of the code. I'm sure there is something obvious that I cant see, but if you could help again, I'd be very appreciative!
Code: Select all
<td>
<?php
$DVDs = simplexml_load_file('Feed.xml');
foreach ($DVDs->BoxsetType as $BoxsetType){
echo($BoxsetType->name);
}
?>
</td>
<td>
<?php
$DVDs = simplexml_load_file('Feed.xml');
foreach ($DVDs->BoxsetType as $BoxsetType){
echo '<img src="'.$BoxsetType.imageURL.'" />';
}
?>
</td>
Thanks again,
Ben
Re: How do I display the picture from a link within an xml field
Posted: Fri Nov 14, 2008 9:27 am
by shiznatix
almost there but your problem is that you are using a . incorrectly. The $boxsetType.imageUrl should be $BoxsetType->imageUrl just like you did for the name.
Re: How do I display the picture from a link within an xml field
Posted: Fri Nov 14, 2008 10:16 am
by ben80
Thanks, I've changed that (below) but still to no avail. Is the below definitely correct then? If so, could the reason for it not displaying be the format of the link? In the xml file the field contains only a link without any quotation marks or anything else.
Code: Select all
<td>
<?php
$DVDs = simplexml_load_file('BondFeed.xml');
foreach ($DVDs->BoxsetType as $BoxsetType){
echo($BoxsetType->name);
}
?>
</td>
<td>
<?php
$DVDs = simplexml_load_file('BondFeed.xml');
foreach ($DVDs->BoxsetType as $BoxsetType){
echo '<img src="'.$BoxsetType->imageURL.'" />';
}
?>
</td>
Thanks,
Ben
Re: How do I display the picture from a link within an xml field
Posted: Sun Nov 16, 2008 11:20 am
by shiznatix
that looks fine, what is the output of that? view the source and post the output.
Re: How do I display the picture from a link within an xml field
Posted: Sat Nov 22, 2008 1:59 pm
by ben80
Hi,
Sorry about the delayed response, been away.
Thank you very much for your help. I have achieved everything I wanted to above. The only thing missing now is how I include a hyperlink to an image? I am getting muddled on this one as I have now got some php and html in there and cant get it to work! So far I have tried replicating what I have done with the text hyperlink above, but to no avail. Would you have any ideas?
Code: Select all
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Under Construction!</title>
</head>
<body>
<?php
echo "<table align='center' border='2'> <tr> <td> Title</td><td> Image</td> <td> Price (£'s)</td></tr>";
$DVDs = simplexml_load_file('BondFeed.xml');
foreach ($DVDs->BoxsetType as $BoxsetType){
echo "<tr><td>";
echo "<a href='$BoxsetType->producturl' target='_blank'>$BoxsetType->name</a>";
echo "</td> <td> ";
echo '<img src='.$BoxsetType->ImageUrl.' width=75 height=75/>';
echo "<a href='$BoxsetType->producturl' target='_blank' >img src='.$BoxsetType->ImageUrl.'</a>";
echo "</td> <td> ";
echo $BoxsetType->price;
echo "</td> </tr> ";}
echo "</table>"
?>
</body>
</html>
Thanks again!
Re: How do I display the picture from a link within an xml field
Posted: Mon Nov 24, 2008 3:54 am
by shiznatix
take a look at this line:
echo "<a href='$BoxsetType->producturl' target='_blank' >img src='.$BoxsetType->ImageUrl.'</a>";
your missing the tag basically. Take a look at the output of your page, take a look at the source code, you should be able to see that the tags just are not complete. Missing < and > and quotes around the url and whatnot. Take a look at the source of the page to see the html and you should be able to see where you are going wrong.