Jump to content

Another simpleXML question.


b.gossler

Recommended Posts

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.

 

 

Link to comment
Share on other sites

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);
   	}
   
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.