Solved

php xml problem

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
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…

719 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