NEED MAJOR HELP WITH Classes and arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

vincenzobar
Forum Commoner
Posts: 95
Joined: Wed Nov 02, 2005 9:57 am

NEED MAJOR HELP WITH Classes and arrays

Post by vincenzobar »

Jcart | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


hi all new here but you may be seeing alot of me!

I have to write a script that parses an XML page inserts it into a MySQL Database
I am usung a class that i got from PHP.net

Code: Select all

class xmlParser{

   var $xml_obj = null;
   var $output = array();
   var $cdata = array();
  
   function xmlParser(){
     
       $this->xml_obj = xml_parser_create();
       xml_set_object($this->xml_obj,$this);
       xml_set_character_data_handler($this->xml_obj, 'dataHandler');  
       xml_set_element_handler($this->xml_obj, "startHandler", "endHandler");
  
   } 
  
   function parse($path){
     
       if (!($fp = fopen($path, "r"))) {
           die("Cannot open XML data file: $path");
           return false;
       }
     
       while ($data = fread($fp, 4096)) {
           if (!xml_parse($this->xml_obj, $data, feof($fp))) {
               die(sprintf("XML error: %s at line %d",
               xml_error_string(xml_get_error_code($this->xml_obj)),
               xml_get_current_line_number($this->xml_obj)));
               xml_parser_free($this->xml_obj);
           }
       }
     
       return true;
   }

   function startHandler($parser, $name, $attribs){
       $_content = array('name' => $name);
       if(!empty($attribs))
         $_content['attrs'] = $attribs;
       array_push($this->output, $_content);
   }

   function dataHandler($parser, $data){
       if(!empty($data)) {
           $_output_idx = count($this->output) - 1;
           $this->output[$_output_idx]['content'] = $data;
       }
   }

   function endHandler($parser, $name){
       if(count($this->output) > 1) {
           $_data = array_pop($this->output);
           $_output_idx = count($this->output) - 1;
           $this->output[$_output_idx]['child'][] = $_data;
       }      
   }

}
now the problem i am having is getting info rom the array from this code

Code: Select all

require_once "xmlparser.class";


$p =& new xmlParser();
$p->parse('cdf.xml');
print_r($p->output);


$array = serialize($p->output);

if(!is_array($array)){
print '<BR><BR>' . $array;
}

$fileW = 'equipmentdatafeed.cvs';

$fp2 = @fopen($fileW, 'a+') or die ("Could not open file $fileW");
	fwrite($fp2, $array);
 	fclose($fp2);
	
	
	$sql= "INSERT INTO `carrier` ( `";
$j=0;
$i=count($array);
foreach( $array as $assoc_index => $value )
  {
  $j++;
  $sql.= strtolower($assoc_index);
  if($i>$j) $sql.= "` , `";
  if($i<=$j) {$sql.= "` ) VALUES ('";}
  }
 $h=0;
foreach( $array as $assoc_index => $value )
  {
  $h++;
  $sql.= utf8_decode(trim(addslashes($value)));
  if($i-1>$h) $sql.= "', '";
  if($i<=$h) $sql.= "','')";
  }
  $sql=trim($sql);
  echo $sql;
I get this

Code: Select all

Array ( [0] => Array ( [name] => ROOT [content] => [child] => Array ( [0] => Array
 ( [name] => CARRIER [attrs] => Array ( [CARRIERDESCRIPTION] => ALLTEL Wireless
 [CARRIERLOGO] => images/carriers/AllTel_logo.gif ) ) [1] => Array ( [name] => CARRIER [attrs]
 => Array ( [CARRIERDESCRIPTION] => Cellular One [CARRIERLOGO] => images/carriers/
Cellularone-Lsf.gif ) ) [2] => Array ( [name] => CARRIER [attrs] => Array 
( [CARRIERDESCRIPTION] => Cingular Wireless [CARRIERLOGO] => images/carriers/
cingularlogo.gif ) ) [3] => Array ( [name] => CARRIER [attrs] => Array ( [CARRIERDESCRIPTION]
 => Liberty Wireless [CARRIERLOGO] => images/carriers/liberty_logo.gif ) ) [4] => Array ( [name] 
=> CARRIER [attrs] => Array ( [CARRIERDESCRIPTION] => Nextel [CARRIERLOGO] => images/
carriers/nextel_logo.gif ) ) [5] => Array ( [name] => CARRIER [attrs] => Array 
( [CARRIERDESCRIPTION] => Sprint PCS [CARRIERLOGO] => images/carriers/sprint_logo.gif ) ) [6]
 => Array ( [name] => CARRIER [attrs] => Array ( [CARRIERDESCRIPTION] => StarBox 
[CARRIERLOGO] => images/carriers/Motient_logo.gif ) ) [7] => Array ( [name] => CARRIER [attrs] 
=> Array ( [CARRIERDESCRIPTION] => T-Mobile [CARRIERLOGO] => images/carriers/
tmobile_logo.gif ) ) [8] => Array ( [name] => CARRIER [attrs] => Array ( [CARRIERDESCRIPTION]
 => U.S. Cellular [CARRIERLOGO] => images/carriers/uscellular_l.gif ) ) [9] => Array ( [name] =>
 CARRIER [attrs] => Array ( [CARRIERDESCRIPTION] => Verizon Wireless [CARRIERLOGO] => 
images/carriers/verizonLogo.gif ) ) ) ) ) 


a:1:{i:0;a:3:{s:4:"name";s:4:"ROOT";s:7:"content";s:1:" ";s:5:"child";a:10:{i:0;a:2:{s:
4:"name";s:7:"CARRIER";s:5:"attrs";a:2:{s:18:"CARRIERDESCRIPTION";s:15:"ALLTEL 
Wireless";s:11:"CARRIERLOGO";s:31:"images/carriers/AllTel_logo.gif";}}i:1;a:2:{s:4:"name";s:
7:"CARRIER";s:5:"attrs";a:2:{s:18:"CARRIERDESCRIPTION";s:12:"Cellular One";s:11:"CARRIERLOGO";s:
35:"images/carriers/Cellularone-Lsf.gif";}}i:2;a:2:{s:4:"name";s:7:"CARRIER";s:5:"attrs";a:2:{s:18:
"CARRIERDESCRIPTION";s:17:"Cingular Wireless";s:11:"CARRIERLOGO";s:32:"images/carriers/cingularlogo.gif";}}
i:3;a:2:{s:4:"name";s:7:"CARRIER";s:5:"attrs";a:2:{s:18:"CARRIERDESCRIPTION";s:16:"Liberty Wireless";s:11:
"CARRIERLOGO";s:32:"images/carriers/liberty_logo.gif";}}i:4;a:2:{s:4:"name";s:7:"CARRIER";s:5:"attrs";a:2:
{s:18:"CARRIERDESCRIPTION";s:6:"Nextel";s:11:"CARRIERLOGO";s:
31:"images/carriers/nextel_logo.gif";}}i:5;a:2:{s:4:"name";s:7:"CARRIER";s:5:"attrs";a:2:{s:
18:"CARRIERDESCRIPTION";s:10:"Sprint PCS";s:11:"CARRIERLOGO";s:31:"images/carriers/
sprint_logo.gif";}}i:6;a:2:{s:4:"name";s:7:"CARRIER";s:5:"attrs";a:2:{s:
18:"CARRIERDESCRIPTION";s:7:"StarBox";s:11:"CARRIERLOGO";s:32:"images/carriers/
Motient_logo.gif";}}i:7;a:2:{s:4:"name";s:7:"CARRIER";s:5:"attrs";a:2:{s:
18:"CARRIERDESCRIPTION";s:8:"T-Mobile";s:11:"CARRIERLOGO";s:32:"images/carriers/
tmobile_logo.gif";}}i:8;a:2:{s:4:"name";s:7:"CARRIER";s:5:"attrs";a:2:{s:
18:"CARRIERDESCRIPTION";s:13:"U.S. Cellular";s:11:"CARRIERLOGO";s:32:"images/carriers/
uscellular_l.gif";}}i:9;a:2:{s:4:"name";s:7:"CARRIER";s:5:"attrs";a:2:{s:18:"CARRIERDESCRIPTION";s:16:
"Verizon Wireless";s:11:"CARRIERLOGO";s:31:"images/carriers/
verizonLogo.gif";}}}}}INSERT INTO `carrier` ( `
How in God's name to i get the values out fo the array using classes????????? please be specific i am new to classes!

thanks a bunch!


Jcart | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

well your print_r() is just displaying the contents of the array:

Code: Select all

print_r($p->output);
then you're serializing the array (turning it into a string)

Code: Select all

$array = serialize($p->output);
at which point you can't do much for data extraction until you unserialize() it....at least I don't think you can loop over it etc in a serialized format, but to be honest I've never tried :?

There's really no reason to serialize it unless you're going to store it to a db or the like. If you're just planning to work with the array on that page, then just keep it in it's normal format and then you can loop over the array and use its key / value pairs to do whatever you need.

you can use either a foreach loop, or a for loop to loop over the array and use its data.
vincenzobar
Forum Commoner
Posts: 95
Joined: Wed Nov 02, 2005 9:57 am

Post by vincenzobar »

It eventually need to be put into a database. but if i have to i can just write to a file in comma delimited form and write a statement to upload that file.

but my problem is i cannot call the keys!!!

how do i write the statement to call the Keys and values out of the output?

I tried

print $p->output['name']['attrs'] and i get nothing

what am i missing, my brain is fried

oh and unserialize() just returns ... Array

and

Code: Select all

foreach($p->output as $key =>$value){
	echo $value;
	}
just returns .. Array

thanks
Last edited by vincenzobar on Wed Nov 02, 2005 4:07 pm, edited 1 time in total.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

well you have a multidimensional array, so you're going to have to perform another loop within your first loop to extract the values.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

function getdetails($var){
echo "<pre>";
var_dump($var);
echo "</pre>";
}

this function helps me while debugging when I need to find out what is stored in a variable... it may help, I dunno. It's better than print_r;
vincenzobar
Forum Commoner
Posts: 95
Joined: Wed Nov 02, 2005 9:57 am

Post by vincenzobar »

Great... how in the hell do i do that?!?!?!?!


Arg! help meeeeeeee!
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Code: Select all

foreach($this as $key=>$val){
    if(is_array($val)){
        foreach($val as $k=>$v){
            echo "$k: $v";
        }
    }
    else{
        echo "$key: $val";
    }
}
Last edited by Luke on Wed Nov 02, 2005 11:18 am, edited 3 times in total.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

something like that yes...but I'd check first to make sure the $val is an array with is_array() and also use different variable names so you don't get confused and potentially write over variables in use 8O
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

good call burrito... FIXED.
vincenzobar
Forum Commoner
Posts: 95
Joined: Wed Nov 02, 2005 9:57 am

Post by vincenzobar »

Code: Select all

foreach($p->output as $key => $value){
	foreach($value as $newkey => $data){
		echo "<BR><BR>" . $data;
	}
}
I wrote that and got "ROOT" and "Array"

So if you look at the original Print_r root is the first array and i counted the arrays i and THINK it is 5 layers deep.

I think i am getting closer!! stayed tuned may need more help soon once i understand this part!!!

thanks for all the help so far!
Last edited by vincenzobar on Wed Nov 02, 2005 4:08 pm, edited 1 time in total.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

what you should do actually is write a recursive function to go as many layers deep as you need 8O
vincenzobar
Forum Commoner
Posts: 95
Joined: Wed Nov 02, 2005 9:57 am

Post by vincenzobar »

wish i new how to do that!!!
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

OK, this is just a guess, so don't take it too seriously.

Code: Select all

function whatever($array){
    foreach($array as $key => $val){
        if(is_array($val)){
            whatever($val);
        }
        else{
            echo "$val";
        }
    }
}
OK, correct me if I am wrong, burrito, but isn't that what you're talking about. I have been wondering about recursive arrays myself.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

yeah, something like that should do it :wink:
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

This might make a little more sense.

Code: Select all

<?php
function whatever($array){
    foreach($array as $key => $val){
        if(is_array($val)){
	        echo "ARRAY:<BR>";
            whatever($val);
        }
        else{
            echo "$key: $val<br>";
        }
    }
}
$array = array(array("loo", "bar"), "john eaton", 4, array("foo", array("john eaton"), 4));
whatever($array);
?>
Post Reply