b.gossler Posted December 12, 2009 Share Posted December 12, 2009 Ive searched and searched, and cant find an answer. So i turn-eth, to you all. Im using XML as a database to store newsItems for display on the front page. I can get everything to display correctly, except for images inside of the content. Im using empty elements in the same format as HTML to keep it simple for those that are going to be actually writing the news items to be displayed. Here is what i have. 112609.xml----------------------------------------------------------------------- <?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/xsl" href="xslt.xslt"?> <newsItem> <itemTitle>News Is Working!!!</itemTitle> <itemDate>11-26-2009</itemDate> <itemContent>[b]<img src="../../images/headerOverlay.jpg" />[/b]This is a test of the news... Blah blah blah.</itemContent> </newsItem> handlers.php------------------------------------------------------------- function getNewsItems(){ $path = 'path/to/xml/dir/'; if ($handle = opendir($path)) { while (false !== ($file = readdir($handle))) { if ($file !== "." && $file !== ".." && $file !== "xslt.xslt") { $xml = simpleXML_load_file($path.$file); $content = $xml->itemContent; echo "<div id='contentItem'>"; echo "<img src='images/newsItemHeader.png' id='spacer' /><img src='images/newsItemHeader2.png' id='spacer2' />"; echo "<span id='contentHeader'>" . $xml->itemTitle . "</span><span id='date'>" . $xml->itemDate . "</span>"; echo "<div id='contentDetail'>" . $xml->itemContent . "</div></div>"; foreach ($xml->itemContent->children() as $child){ print($child->img); } } } closedir($handle); } } Please note: handlers.php is included on the front page, which then in turn calls the xml files. I can get a print_r of $xml to show the img element, but past that, it will not display. If you want to see the output as it stands now, http://www.cohoproductions.org/dev/2010/index.php That xml file refers to the first news item on the page. my overall question is how do i get those elements inside itemContent to display on the front page IF they are present? do they have to be named each time i wish to use them? or can i use children() and echo them as they come? Forgive my terminology if it is not correct, or confusing. Im relatively new at this, and tbh, i dont write php very often. Quote Link to comment https://forums.phpfreaks.com/topic/184907-another-simplexml-question/ Share on other sites More sharing options...
ngreenwood6 Posted December 12, 2009 Share Posted December 12, 2009 I think you are confused. you do not use img tags in an xml file. your xml file should look something like this: <?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/xsl" href="xslt.xslt"?> <newsItem> <itemTitle>News Is Working!!!</itemTitle> <itemDate>11-26-2009</itemDate> <itemImage>../../images/headerOverlay.jpg</image> <itemContent>This is a test of the news... Blah blah blah.</content> </newsItem> then your php file should look like this: function getNewsItems(){ $path = 'path/to/xml/dir/'; if ($handle = opendir($path)) { while (false !== ($file = readdir($handle))) { if ($file !== "." && $file !== ".." && $file !== "xslt.xslt") { $xml = simpleXML_load_file($path.$file); $content = $xml->newsItem; foreach($content as $article){ echo "<div id='contentItem'>"; echo "<img src='images/newsItemHeader.png' id='spacer' /><img src='images/newsItemHeader2.png' id='spacer2' />"; echo "<span id='contentHeader'>" . $xml->itemTitle . "</span><span id='date'>" . $xml->itemDate . "</span>"; echo "<div id="contentimage"><img src='".$xml->itemImage."' /></div>"; echo "<div id='contentDetail'>" . $xml->itemContent . "</div></div>"; } } closedir($handle); } } Quote Link to comment https://forums.phpfreaks.com/topic/184907-another-simplexml-question/#findComment-976131 Share on other sites More sharing options...
b.gossler Posted December 12, 2009 Author Share Posted December 12, 2009 Thats the problem I am facing however. I need the ability to include an empty element ie:<img src="blah" /> within the itemContent tag. The reason for this is that images will change from item to item, or sometimes not be present at all. This would be a simple matter if i knew what the images would be for every item (ie: using the code you supplied) but as this is used for news, i dont want to have to go back and change the xml every time an image needs to be displayed. Another problem with using that format you supplied is that there isnt a simple way of formating where the image is displayed within the itemContent. Thank you for your response. Quote Link to comment https://forums.phpfreaks.com/topic/184907-another-simplexml-question/#findComment-976139 Share on other sites More sharing options...
salathe Posted December 12, 2009 Share Posted December 12, 2009 Use CDATA, like <itemContent><![CDATA[blah <img src="mypic"/> ... ]]></itemContent> Quote Link to comment https://forums.phpfreaks.com/topic/184907-another-simplexml-question/#findComment-976145 Share on other sites More sharing options...
ngreenwood6 Posted December 12, 2009 Share Posted December 12, 2009 I am not sure that I correctly understand what you mean but you could nest the items for the itemContent: <?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/xsl" href="xslt.xslt"?> <newsItem> <itemTitle>News Is Working!!!</itemTitle> <itemDate>11-26-2009</itemDate> <itemContent> <image>someimage.jpg</image> <content>This is a test of the news... Blah blah blah.</content> </itemContent> </newsItem> Then you would change your code to this: function getNewsItems(){ $path = 'path/to/xml/dir/'; if ($handle = opendir($path)) { while (false !== ($file = readdir($handle))) { if ($file !== "." && $file !== ".." && $file !== "xslt.xslt") { $xml = simpleXML_load_file($path.$file); $content = $xml->newsItem; foreach($content as $article){ echo "<div id='contentItem'>"; echo "<img src='images/newsItemHeader.png' id='spacer' /><img src='images/newsItemHeader2.png' id='spacer2' />"; echo "<span id='contentHeader'>" . $content->itemTitle . "</span><span id='date'>" . $content->itemDate . "</span>"; foreach($content->itemContent as $item){ echo '<div id="contentimage"><img src='.$item->itemImage.' /></div>'; echo "<div id='contentDetail'>" . $item->itemContent . "</div></div>"; } } } closedir($handle); } } Is that what you mean? If not could you explain a little better. Quote Link to comment https://forums.phpfreaks.com/topic/184907-another-simplexml-question/#findComment-976150 Share on other sites More sharing options...
b.gossler Posted December 12, 2009 Author Share Posted December 12, 2009 The cdata response did exactly what i need it to... except that it wont display images that aren't already loaded on the page. Thats something i will attempt to figure out before posting here though. -- Again I would run into the problem of not being able to position the image without going into css and all of that. I want whoever writes the news item to be able to place an image wherever he or she wants. Something like posting on this very forum. You can have text, blah blah, then put an image, then more text. I guess what im trying to say is that i want the image existance and placement in the newsItem to be dynamic, and thats not possible, as far as i can see, in a perfectly formed xml document. Maybe im wrong, but thats what my new eye to php / xml is telling me. Thank you for the responses. I will mark this solved. Quote Link to comment https://forums.phpfreaks.com/topic/184907-another-simplexml-question/#findComment-976163 Share on other sites More sharing options...
ngreenwood6 Posted December 12, 2009 Share Posted December 12, 2009 ok now I understand what you were getting at. you are correct your best bet is to use cdata. I didnt understand what you were trying to do but now I do. you want the image to be in the text where the user puts it not just an image in every article. Quote Link to comment https://forums.phpfreaks.com/topic/184907-another-simplexml-question/#findComment-976172 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.