Solved

php xml problem

Posted on 2006-11-25
3
210 Views
Last Modified: 2013-12-12
I have the following code:

<?php
      //includes
      include('conf.php');
      
      $connection = mysql_connect($host, $user, $pass)
      or die ('Unable to connect!');

      function add_to_queue( $func, $arg )
      {
            global $db;
            
            $dom = new DomDocument();
            $root = $dom->createElement( "arguments" );
            foreach( $args as $argtext )
            {
                  $arg = $dom->createElement( "argument" );
                  $arg->appendChild( $dom->createTextNode( $artext ) );
                  $root->appendChild( $arg );
            }            
            $dom->appendChild( $root );
            
            $sth = $db->prepare( "INSERT INTO queue VALUES ( 0, ?, ? )" );
            $db->execute( $sth, array( $func, $dom->saveXML() ) );
      }

      function run_queue()
      {
            global $db;
            
            $delsth = $db->prepare( "DELETE FROM queue VALUES ( 0, ?, ? )" );
            
            $res = $db->query( "SELECT id, func, args FROM queue" );
            while( $res->fetchInto($row) )
            {
                  $id = $row[0];
                  $func = $row[1];
                  $argxml = $row[2];
                  
                  $dom = new DomDocument();
                  $dom->loadXML( $argxml );
                  $arg = array();
                  foreach( $dom->getElementsByTagName( "argument" ) as $node )
                  {
                        $arg []= $node->nodeValue;
                  }
                  
                  call_user_func_array( $func, $args );
                  
                  $db->execute( $delsth, array( $id ) );
            }
      }
?>

I am using a server that is running php version 4.4.2 and I need to get this script to work... it is obviously a php 5 script (DomDocumet();).  How would I convert it?
0
Comment
Question by:gishdog
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 20

Expert Comment

by:Muhammad Wasif
ID: 18013852
Have a look at PHP4 XML functions http://www.php.net/manual/en/ref.domxml.php
0
 
LVL 8

Accepted Solution

by:
John Kawakami earned 500 total points
ID: 18014160
How important is it that the $arg be stored as XML?

If it's not important at all, use serialize($arg) to convert the PHP values into a string, and unserialize($str) to convert the string back into an array of values.

If using XML is important, then this won't help.  

The XML looks gratuitous to me :-)

     function add_to_queue( $func, $arg )
     {
          global $db;          
          $sth = $db->prepare( "INSERT INTO queue VALUES ( 0, ?, ? )" );
          $db->execute( $sth, array( $func, serialize($arg) ) );
     }

     function run_queue()
     {
          global $db;
         
          $delsth = $db->prepare( "DELETE FROM queue VALUES ( 0, ?, ? )" );
         
          $res = $db->query( "SELECT id, func, args FROM queue" );
          while( $res->fetchInto($row) )
          {
               call_user_func_array( $row[1],  unserialize($row[2]) );
               $db->execute( $delsth, array( $row[0] ) );
          }
     }
0
 

Author Comment

by:gishdog
ID: 18014398
Yea... After sleeping on it the xml does seem like overkill... I think I'm going to go for the serialize route... thanks for the help.
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question