SitePoint Sponsor

User Tag List

Page 3 of 3 FirstFirst 123
Results 51 to 64 of 64
  1. #51
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry about this, I feel such a numpty!

    If a product has no value then that is OK it just returns an empty tag. I checked this

    Would the best way to do this be to write the tags and populate each one from the code?

    I would like this to be a learning exercise as well as a good end result.

    C

  2. #52
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Okies, not a problem.

    Do you have something detailing the required XML schema?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #53
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not here, will get back to you later if that is OK

    C

  4. #54
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    It's god-awful code Colin, but it works. It could do with a healthy dose of refactoring and a few objects for my liking, if I get a few moments later I'll try to re-address.

    Anthony.

    PHP Code:
    <?php
    function build_product($product_schema)
    {
        
    extract(
            
    array_merge(
                array(
                    
    'id'            => 'n/a',
                    
    'date'            => 'n/a',
                    
    'ref'            => 'n/a',
                    
    'price'            => 'n/a',
                    
    'currency'        => 'n/a',
                    
    'ref'            => 'n/a',
                    
    'color'            => 'n/a',
                    
    'size'            => 'n/a',
                    
    'stock_level'    => 'n/a',
                    
    'desc'            => 'n/a',
                    
    'features'        => 'n/a',
                    
    'image_id'        => 'n/a',
                    
    'image_url'        => 'n/a',
                    
    'alt'            => 'n/a'
                
    ),
                
    $product_schema
            
    )
        );
        return 
    "
            <product>
                <id>
    $id</id>
                <date>
    $date</date>
                <ref>
    $ref</ref>
                <price>
    $price</price>
                <currency>
    $currency</currency>
                <category>
                    <ref>
    $ref</ref>
                </category>
                <color>
    $color<color>
                <size>
    $size</size>
                <stock_level>
    $stock_level</stock_level>
                <desc>
    $desc</desc>
                <features>
    $features</features>
                <image>
                    <image_id>
    $image_id</image_id>
                    <image_url>
    $image_url</image_url>
                    <alt>
    $alt</alt>
                </image>
            <product>
        "
    ;
    }
    include(
    'includes/conn.php');
    $result mysql_query(
        
    sprintf(
            
    'SELECT * FROM products WHERE id = %d',
            
    1
        
    )
    );
    $products = array();
    while(
    $product mysql_fetch_assoc($result))
    {
        
    $products[$product['id']][$product['name']] = $product['value'];
    }

    #replacements
    $replacement_tags = array(
        
    'Colour'    => 'color',
        
    'Stock'        => 'stock_level',
    );

    #build xml
    $xml '<?xml version="1.0" encoding="UTF-8"?>';
    $xml .= '<root>';
    foreach(
    $products as $id => $attributes)
    {
        
    $product_schema = array(
            
    'id'    => $id
        
    );
        foreach(
    $attributes as $name => $value)
        {
            if(
    true === array_key_exists($name$replacement_tags))
            {
                
    $product_schema[$replacement_tags[$name]] = $value;
            }
            else
            {
                
    $product_schema[$name] = $value;
            }
        }
        
    $xml .= build_product($product_schema)
    }
    $xml .= '</root>';

    header('Content-Type: text/xml; charset=UTF-8');
    echo 
    $xml;
    exit;
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #55
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Some OO thrown in.
    PHP Code:
    <?php
    #include objects
    require('includes/conn.php');
    require(
    'includes/StringTemplate.php');
    require(
    'includes/Product_Template.php');

    $result mysql_query(
        
    sprintf(
            
    'SELECT * FROM products WHERE id = %d',
            
    1
        
    )
    );
    $products = array();
    while(
    $product mysql_fetch_assoc($result))
    {
        
    $products[$product['id']][$product['name']] = $product['value'];
    }
    $xml '<?xml version="1.0" encoding="UTF-8"?>';
    $xml .= '<root>';
    foreach(
    $products as $id => $attributes)
    {
        
    $product = new Product_Template();
        
    $product->bind('id'$id);
        foreach(
    $attributes as $name => $value)
        {
            
    $product->bind($name$value);
        }
        
    $xml .= $product->render();
    }
    $xml .= '</root>';

    header('Content-Type: text/xml; charset=UTF-8');
    echo 
    $xml;
    exit;
    ?>
    Objects
    PHP Code:
    class StringTemplate
    {
        protected
            
    $template,
            
    $data;
        
        public function 
    setTemplate($template)
        {
            
    $this->template $template;
        }
        
        public function 
    bind($name$value)
        {
            
    $this->data[$name] = $value;
        }
        
        public function 
    render()
        {
            
    extract($this->data);
            return @
    "$this->template";
        }
    }

    class 
    Product_Template extends StringTemplate
    {
        
        protected
            
    $replacements = array(
                
    'Colour'    =>    'color',
                
    'Stock'        =>    'stock_level'
            
    );
        
        public function 
    __construct()
        {
            
    $this->template '
                <product>
                    <id>$id</id>
                    <date>$date</date>
                    <ref>$ref</ref>
                    <price>$price</price>
                    <currency>$currency</currency>
                    <category>
                        <ref>$ref</ref>
                    </category>
                    <color>$color<color>
                    <size>$size</size>
                    <stock_level>$stock_level</stock_level>
                    <desc>$desc</desc>
                    <features>$features</features>
                    <image>
                        <image_id>$image_id</image_id>
                        <image_url>$image_url</image_url>
                        <alt>$alt</alt>
                    </image>
                <product>
            '
    ;
        }
        
        protected function 
    hasReplacement($name)
        {
            return 
    array_key_exists($name$this->replacements);
        }
        
        protected function 
    getReplacement($name)
        {
            if(
    true === $this->hasReplacement($name))
            {
                return 
    $this->replacements[$name];
            }
            return 
    $name;
        }
        
        public function 
    bind($name$value)
        {
            
    parent::bind($this->getReplacement($name), $value);
        }

    Off Topic:


    I never knew you could do this:-
    PHP Code:
    return @"$this->template"
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  6. #56
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    XML Schema as requested:

    <?xml version="1.0" encoding="UTF-8"?>
    <root>
    <tienda_linea>
    <version>1</version>
    </tienda_linea>
    <product>
    <id></id>
    <date></date>
    <ref></ref>
    <price></price>
    <price_type>sale</price_type>
    <part_number></part_number>
    <stock_level></stock_level>

    <product_type>
    <en></en>
    <es></es>
    </product_type>

    <product_name></product_name>
    <stock_code></stock_code>
    <stock_detail></stock_detail>
    <unit></unit>
    <size></size>
    <color></color>

    <del_type>
    <mail></mail>
    <plot></plot>
    </del_type>

    <url></url>
    <desc>
    <en></en>
    <es></es>
    </desc>

    <promos>
    <promo></promo>
    </promos>

    // Need to loop here to include max 10 images and id's

    <images>
    <image_id></image_id>
    <url></url>
    <title>
    <en></en>
    <es></es>
    </title>
    </images>

    </product>
    </root>

    Just got back so not tried other solutions as yet.
    Colin

  7. #57
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Anthony

    This produces the following error: XML Parsing Error: mismatched tag. Expected: </color>.

    View source:

    <?xml version="1.0" encoding="UTF-8"?><root>
    <product>
    <id>$id</id>
    <date>$date</date>
    <ref>$ref</ref>
    <price>$price</price>
    <currency>$currency</currency>
    <category>
    <ref>$ref</ref>
    </category>
    <color>$color<color>
    <size>$size</size>
    <stock_level>$stock_level</stock_level>
    <desc>$desc</desc>
    <features>$features</features>
    <image>
    <image_id>$image_id</image_id>
    <image_url>$image_url</image_url>
    <alt>$alt</alt>
    </image>
    </product>
    </root>

    Changing Colour to color seems to be causing a problem. Changing stock to stock_level is fine?

  8. #58
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    It's a typo Colin, change it to </color>.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  9. #59
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have changed it but it simply changes it back??

  10. #60
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Which code example are you using?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  11. #61
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok solved that problem by changing the query to 'SELECT * FROM products'

    I now have this:

    <?xml version="1.0" encoding="UTF-8"?><root>
    <product>
    <id>$id</id>
    <date>$date</date>
    <ref>$ref</ref>
    <price>$price</price>
    <currency>$currency</currency>
    <category>
    <ref>$ref</ref>
    </category>
    <color>$color</color>
    <size>$size</size>
    <stock_level>$stock_level</stock_level>
    <desc>$desc</desc>
    <features>$features</features>
    <image>
    <image_id>$image_id</image_id>
    <image_url>$image_url</image_url>
    <alt>$alt</alt>
    </image>
    </product>
    </root>


  12. #62
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    Which code example are you using?
    The later example with the Ob O

    C

  13. #63
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    It appears the bit I thought wasn't possible, well, wasn't!

    I'll hold my hands up, I think I've put us in a bit of a corner, but give this, now convoluted approach a go Colin.

    It's far too complicated now. Time for a coffee and a sit down to rewrite.

    PHP Code:
    <?php
    class StringTemplate
    {
        protected
            
    $template,
            
    $data = array();

        public function 
    setTemplate($template)
        {
            
    $this->template $template;
        }

        public function 
    bind($name$value)
        {
            
    $this->data[$name] = $value;
        }

        public function 
    render()
        {
            
    $template $this->template;
            foreach(
    $this->data as $key => $value)
            {
                
    $template preg_replace(sprintf('~(?<=>)\$%s(?=<)~i'$key), $value$template);
            }
            return 
    $template;
        }
    }

    class 
    Product_Template extends StringTemplate
    {

        protected
            
    $replacements = array(
                
    'Colour'    =>    'color',
                
    'Stock'        =>    'stock_level'
            
    );

        public function 
    __construct()
        {
            
    $this->template '
                <product>
                    <id>$id</id>
                    <date>$date</date>
                    <ref>$ref</ref>
                    <price>$price</price>
                    <currency>$currency</currency>
                    <category>
                        <ref>$ref</ref>
                    </category>
                    <color>$color</color>
                    <size>$size</size>
                    <stock_level>$stock_level</stock_level>
                    <desc>$desc</desc>
                    <features>$features</features>
                    <image>
                        <image_id>$image_id</image_id>
                        <image_url>$image_url</image_url>
                        <alt>$alt</alt>
                    </image>
                <product>
            '
    ;
        }

        protected function 
    hasReplacement($name)
        {
            return 
    array_key_exists($name$this->replacements);
        }

        protected function 
    getReplacement($name)
        {
            if(
    true === $this->hasReplacement($name))
            {
                return 
    $this->replacements[$name];
            }
            return 
    $name;
        }

        public function 
    bind($name$value)
        {
            
    parent::bind($this->getReplacement($name), $value);
        }
    }
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  14. #64
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    #include objects
    require('includes/conn.php');
    require(
    'includes/StringTemplate.php');
    require(
    'includes/Product_Template.php');
    require(
    'includes/Product_Feed_Template.php');
    $result mysql_query(
        
    sprintf(
            
    'SELECT * FROM products WHERE id = %d',
            
    1
        
    )
    );
    $products = array();
    while(
    $product mysql_fetch_assoc($result))
    {
        
    $products[$product['id']][$product['name']] = $product['value'];
    }
    $template = new Product_Feed_Template();
    foreach(
    $products as $id => $attributes)
    {
        
    $product = new Product_Template();
        
    $product->bind('id'$id);
        foreach(
    $attributes as $name => $value)
        {
            
    $product->bind($name$value);
        }
        
    $template->addProduct($product);
    }
    header('Content-Type: text/xml');
    echo 
    $template->render();
    exit;
    ?>
    PHP Code:
    class StringTemplate
    {
        protected
            $template    = '',
            $data        = array();

        public function setTemplate($template)
        {
            $this->template = $template;
        }

        public function bind($name, $value)
        {
            $this->data[$name] = $value;
        }

        public function render()
        {
            $template = $this->template;
            foreach($this->data as $name => $value){
                $template = str_replace(sprintf('{{%s}}', $name), $value, $template);
            }
            #remove non-assigned variables.
            $template = preg_replace('~{{[0-9a-z_-]*?}}~i', null, $template);
            return trim($template);
        }
    }

    class Product_Feed_Template extends StringTemplate
    {
        public function __construct()
        {
            $this->setTemplate(
            '
            <?xml version="1.0" encoding="UTF-8"?>
            <root>
                {{products}}
            </root>
            '
            );
        }

        public function addProduct(Product_Template $product)
        {
            $current = isset($this->data['products']) ? $this->data['products'] : '' ;
            $this->bind('products', $current . $product->render());
        }
    }

    class Product_Template extends StringTemplate
    {
        public function __construct()
        {
            $this->setTemplate(
                '
                <product>
                    <id>{{id}}</id>
                    <date>{{date}}</date>
                    <ref>{{ref}}</ref>
                    <price>{{price}}</price>
                    <currency>{{currency}}</currency>
                    <category>
                        <ref>{{ref}}</ref>
                    </category>
                    <color>{{color}}</color>
                    <size>{{size}}</size>
                    <stock_level>{{stock_level}}</stock_level>
                    <desc>{{desc}}</desc>
                    <features>{{features}}</features>
                    <image>
                        <image_id>{{image_id}}</image_id>
                        <image_url>{{image_url}}</image_url>
                        <alt>{{alt}}</alt>
                    </image>
                </product>
                '
            );
        }
    }
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •