Solved

php xml problem

Posted on 2006-11-25
3
204 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
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:
jk2001 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now