TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   xml.. (http://www.talkphp.com/absolute-beginners/1299-xml.html)

Tanax 10-12-2007 03:25 PM

xml..
 
view.php
PHP Code:

<?php

/**
||||||||||||||||||||||||||||||||||||||||||
|||| @author Tanax
|||| @copyright 2007
||||||||||||||||||||||||||||||||||||||||||
**/

    
include('config.php');
    
$id $_GET['pic'];
    
$img $path $id $format;
    
    if(!
file_exists($img)) {
        
        echo 
'This picture does not exist!';
        
    }
    
    else {
        
        echo 
'<center>';
        echo 
'<table border="1" cellpadding="10">';
        echo 
'<tr><td><img src="'.$img.'" border="0" /><br />';
        
$file simplexml_load_string(file_get_contents($xml)); 

        foreach(
$file->desc as $desc) {
            
            if(
$desc['pic'] == $id) {
                
                echo 
$desc['info'];
                
            }
            
        }
        
        
        echo 
'</td></tr>';
        echo 
'</table>';
        echo 
'<a href="testing.php">Tillbaka</a>';
        echo 
'</center>';
        
    }



?>

config.php
PHP Code:

$xml 'desc.xml'

desc.xml
Code:

<?xml version="1.0" encoding="ISO-8859-1"?>

<desc pic="1" info="Testing" />


It doesn't give me any error msgs, but it doesn't print the "testing" below the image :S

What's wrong? :(

Salathe 10-12-2007 04:22 PM

Your main problem is centered around your use of SimpleXML, I don't think you've fully grasped it yet.

With the XML document provided, when the SimpleXML document is parsed (using your simplexml_load_string) the object is created which represents your XML data. In this case, the $file variable is essentially the <desc ... /> node in the form of a PHP object. If we were to take a look at the object (using print_r($file)) here's what we would see:
Code:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [pic] => 1
            [info] => Testing
        )
)

Because it is that node (the "root node" in XML terminology), you can't access the desc by trying $file->desc! To put it simply and hopefully in a straightforward way, you would need to use $file['@attributes']['pic'] to get a hold of the pic attribute.

Changes
PHP Code:

        $file simplexml_load_string(file_get_contents($xml)); 

        foreach(
$file->desc as $desc) {
            
            if(
$desc['pic'] == $id) {
                
                echo 
$desc['info'];
                
            }
            
        } 

PHP Code:

        // See: http://php.net/simplexml_element_construct
        //      for an explanation of the arguments used here
        
$desc = new SimpleXMLElement($xmlnulltrue);
        echo 
$desc['@attributes']['info']; 


However, if you're wanting to have descriptions for multiple images in your XML document -- like below -- things will be slightly different. Let us know if that's the case!

Do you want multiple descs?
(I've just changed the names to make more sense to me.)
Code:

<?xml version="1.0" encoding="utf-8"?>
<pics>
    <pic id="1" info="Testing!" />
    <pic id="2" info="Testing again!" />
    <pic id="3" info="Testing again, again!" />
</pics>


Tanax 10-12-2007 05:06 PM

Yea, actually that is the case, I wanna have more descriptions for other images.

And thanks for the headsup, I think I understand now why it doesn't work.
But I still have no idea how to use this with more than 1 description :(

Salathe 10-12-2007 05:52 PM

With more than one description you'd need to lay out the XML document like I suggested above and then go back to looping through the descs (or pics) looking for the right id (there are other ways, but it's probably best to keep things simple for now).

Tanax 10-12-2007 05:56 PM

BUt I can still use this:

PHP Code:

        $desc = new SimpleXMLElement($xmlnulltrue);
        echo 
$desc['@attributes']['info']; 

??

Salathe 10-12-2007 07:47 PM

No. Create your XML with multiple descriptions, and the code that you think would work, then show us. :)

Tanax 10-12-2007 08:29 PM

HAH!
I solved it! :D:D

PHP Code:

$xmlstring = new SimpleXMLElement($xmlnulltrue);                
        
        foreach(
$xmlstring as $pics => $pic) {
            
            if(
$pic['id'] == $id) {
                
                echo 
$pic['info'];
                
            }
            
        } 

Code:

<?xml version="1.0" encoding="utf-8"?>
<pics>
            <pic id="1" info="Testing1" />
        <pic id="2" info="Testing2" />
        <pic id="3" info="Testing3" />
        <pic id="4" info="Testing4" />
</pics>


Thanks so much!! :D


All times are GMT. The time now is 07:29 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0