Solved

php xml problem

Posted on 2006-11-25
3
207 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to create an extensible mechanism for linked drop downs.
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

831 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