NEED MAJOR HELP WITH Classes and arrays
Posted: Wed Nov 02, 2005 10:06 am
Jcart | Please use
now the problem i am having is getting info rom the array from this code
I get this
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
andCode: 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.netCode: 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;
}
}
}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;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` ( `thanks a bunch!
Jcart | Please use
Code: Select all
andCode: 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]