TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-12-2007, 03:25 PM   #1 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default 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? :(
Tanax is offline  
Reply With Quote
Old 10-12-2007, 04:22 PM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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>
Salathe is offline  
Reply With Quote
Old 10-12-2007, 05:06 PM   #3 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

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 :(
Tanax is offline  
Reply With Quote
Old 10-12-2007, 05:52 PM   #4 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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).
Salathe is offline  
Reply With Quote
Old 10-12-2007, 05:56 PM   #5 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

BUt I can still use this:

PHP Code:
        $desc = new SimpleXMLElement($xmlnulltrue);
        echo 
$desc['@attributes']['info']; 
??
Tanax is offline  
Reply With Quote
Old 10-12-2007, 07:47 PM   #6 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

No. Create your XML with multiple descriptions, and the code that you think would work, then show us. :)
Salathe is offline  
Reply With Quote
Old 10-12-2007, 08:29 PM   #7 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

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
Tanax is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 05:42 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design