Making a XML Manager in PHP

Hello,

Having trouble with a class… new to PHP I know your probably rolling your eyes, big move from .NET.

    function end_element ($khalid, $element) {
        $element = strtolower($element);
        if ($this->ends_data[$element]) {
            $this->data[] = $this->data;
            $this->data = array();
        }
        $this->active_field = '';
    }
    
    function ccdata ($khalid, $text) {
        if ($this->data_type[$this->active_field] === 2) {
            $this->data[$this->active_field][] = $text;
        } elseif ($this->data_type[$this->active_field] === 1) {
            $this->data[$this->active_field] .=$text;
        }            
    }

function $display() {
        echo '<table border="0">
';
        foreach ($this->data as $image) {
            echo '<tr>';
            $image = join(',', $image['image']);
            printf("<th>
                        <a href='%s'>%s</a>
                    </th>
                    <td>
                        %s
                    </td>
                 </tr>
",
                 $_SERVER['PHP_SELF'] . '?pic=' . $image['pic'],
                 $image['caption'],$image);
            echo "</tr>
";
        }
    }

The snippet is basically part of a class. It displays

Parse error: syntax error, unexpected T_VARIABLE, expecting T_STRING in /localhost/admincp.php on line 392

Line 392 is the Display function

Please anyone?


    function show_image ($pic) {
        foreach ($this->data as $image) {
            if ($image['pic'] !== $pic) {
                continue;
            }
            
            $image = join(',', $image['image']);
            printf("<b>%s</b> by %s.<br>", $image['caption'], $image);
            printf("Picture %s<br>", $image['pic']);
           ##419 printf("URL %s<p>
", image['url']); 
##            Parse error:  syntax error, unexpected '[' in admincp.php on line 419
        }
?>
<a href="<?= $_SERVER['PHP_SELF'] ?>">Back</a> to full list.</p>

Thanks for the welcome. This a sweet scripting language. The compiler is pretty friendly. Is there a PHP Studio other than Zend? Something like Visual Studio to manage PHP, Pear, MySQL etc…

…issues with my error?

 printf("URL %s<p>
", image['url']); 
          

Parse error: syntax error, unexpected ‘[’ in admincp.php on line 419

Walay Kum Salam

Just to clarify, PHP is an interpreted language, NOT a compiled one. :slight_smile:

[quote=ahmednuaman;2336267]Yes that’s right. It is one of it’s failings, well I guess compared to the speed of MSIL, but it means that if your site’s on ServerX (Windows) and it went down, you could then quickly move it to ServerY (Any OS (nearly)) and get it back up again.

PHP, though, can be very fast, especially if you use a PHP cache such as:
http://www.php.net/apc
http://eaccelerator.net/
http://xcache.lighttpd.net/[/quote]


<?xml version="1.0"?>
<images>
<image>
<pic>kitten.jpg</pic>
<caption>A picture of a kitten</caption>
<url>http://www.k77eel.com</url>
</image>
</images>

This is the XML that I want to parse. It wont load. This is my class.


<?php
// HTML ETC
// LICENCE COPYRIGHT TO K77 ETC
class ImageList {
        var $parser;
        var $record;
        var $active_field = '';
        var $data_type;
        var $ends_data;
        var $data;
        
        function ImageList ($filename) {
            $this->parser = xml_parser_create();
            xml_set_object($this->parser, &$this);
            xml_set_element_handler($this->parser, 'start_element', 'end_element');
            xml_set_character_data_handler($this->parser, 'cdata');
            
            $this->data_type = array('pic' => 1,
                                     'caption' => 1,
                                     'url' => 1);
            $this->ends_data = array('image' => true);
            
            $saladin = join("", file($filename));
            xml_parse($this->parser, $saladin);
            xml_parser_free($this->parser);
    }
    
    function start_element ($khalid, $element, &$attributes) {
        $element = strtolower($element);
        if ($this->data_type[$element] != 0) {
            $this->active_field = $element;
        } else {
            $this->active_field = '';
        }
    }
    
    function end_element ($khalid, $element) {
        $element = strtolower($element);
        if ($this->ends_data[$element]) {
            $this->data[] = $this->data;
            $this->data = array();
        }
        $this->active_field = '';
    }
    
    function cdata ($khalid, $text) {
        if ($this->data_type[$this->active_field] === 2) {
            $this->data[$this->active_field][] = $text;
        } elseif ($this->data_type[$this->active_field] === 1) {
            $this->data[$this->active_field] .=$text;
        }            
    }
    
    function display() {
        echo '<table border="0">
';
        foreach ($this->data as $image) {
            echo '<tr>';
            $image = join(',', $image['image']);
            printf("<th>
                        <a href='%s'>%s</a>
                    </th>
                    <td>
                        %s
                    </td>
                 </tr>
",
                 $_SERVER['PHP_SELF'] . '?pic=' . $image['pic'],
                 $image['caption'],$image);
            echo "</tr>
";
        }
    }
    
    function show_image ($pic) {
        foreach ($this->data as $image) {
            if ($image['pic'] !== $pic) {
                continue;
            }
            
            $image = join(',', $image['image']);
            printf("<b>%s</b> by %s.<br>", $image['caption'], $image);
            printf("Picture %s<br>", $image['pic']);
            printf("URL: %s<p>
", $image['url']);            
        }
?>
<a href="<?= $_SERVER['PHP_SELF'] ?>">Back</a> to full list.</p>            
<?php
        }
    };
    
    $my_images = new ImageList("xml.xml");
    if ($_GET['pic']) {
        $my_images->show_image($_GET['pic']);
    } else {
        $my_library->display();
    }
?>


Its taking about an undefined function



Warning: join() [function.join]: Invalid arguments passed in /my/admincp.php on line 396

I don’t have a join function. My function is a method inside the function. PHP is not a language for record, its a scripting language

I don’t understand implode. Where would I place this?

according to PHP implode makes several variables to an indexed array and then to a string. Surely I want to use a string to make the call and not duck around with other siht.