Page 2 of 2
Posted: Wed Nov 02, 2005 1:16 pm
by vincenzobar
Kool well thanks to you all i have gotten this far
Code: Select all
name: ROOT
content:
name: CARRIER
CARRIERDESCRIPTION: ALLTEL Wireless
CARRIERLOGO: images/carriers/AllTel_logo.gif
name: CARRIER
CARRIERDESCRIPTION: Cellular One
CARRIERLOGO: images/carriers/Cellularone-Lsf.gif
name: CARRIER
CARRIERDESCRIPTION: Cingular Wireless
CARRIERLOGO: images/carriers/cingularlogo.gif
name: CARRIER
CARRIERDESCRIPTION: Liberty Wireless
CARRIERLOGO: images/carriers/liberty_logo.gif
name: CARRIER
CARRIERDESCRIPTION: Nextel
CARRIERLOGO: images/carriers/nextel_logo.gif
name: CARRIER
CARRIERDESCRIPTION: Sprint PCS
CARRIERLOGO: images/carriers/sprint_logo.gif
name: CARRIER
CARRIERDESCRIPTION: StarBox
CARRIERLOGO: images/carriers/Motient_logo.gif
name: CARRIER
CARRIERDESCRIPTION: T-Mobile
CARRIERLOGO: images/carriers/tmobile_logo.gif
name: CARRIER
CARRIERDESCRIPTION: U.S. Cellular
CARRIERLOGO: images/carriers/uscellular_l.gif
name: CARRIER
CARRIERDESCRIPTION: Verizon Wireless
CARRIERLOGO: images/carriers/verizonLogo.gif
Now i need to figure out how to get the values from CARRIERDESCRIPTION and CARRIERLOGO out to insert into MySQL
Any more brilliant ideas!!!
Thanks you all are the best!
Posted: Wed Nov 02, 2005 1:16 pm
by yum-jelly
if your just tring to insert the ( carriers, image logo ) then base your output on that...
Quick Example...
Code: Select all
<?
// first two functions are generic XML processing functions
// they can handle any XML tree, converting the data to
// a output array, it's better to keep things in serialized
// arrays than processing the slow XML data again and again!
function process_xml ( $in )
{
$xml = xml_parser_create ( 'ISO-8859-1' );
xml_parser_set_option ( $xml, XML_OPTION_SKIP_WHITE, 1 );
xml_parse_into_struct ( $xml, $in, $value, $name );
xml_parser_free ( $xml );
$x = 0;
$part = array ();
$part[$value[$x]['tag']] = get_extended ( $value, $x );
return ( $part );
}
function get_extended ( $next, &$i )
{
$node = array();
$j = 0;
if ( isset ( $next[$i]['value'] ) && $next[$i]['value'] )
{
array_push ( $node, $next[$i]['value'] );
}
$old = '';
while ( ++$i < count ( $next ) )
{
switch ( $next[$i]['type'] )
{
case 'cdata' :
array_push ( $node, $next[$i]['value'] );
break;
case 'complete' :
$node[$next[$i]['tag']] = $next[$i]['value'];
break;
case 'open' :
$j++;
if ( $old <> $next[$i]['tag'] )
{
$j = 0;
$old = $next[$i]['tag'];
}
$node[$next[$i]['tag']][$j] = get_extended ( $next, $i );
break;
case 'close' :
return ( $node );
}
}
}
// this is the data to insert in the databse
// carrier name, path and image logo name
// if you don't want to insert the image paths just remove
// it from the data array...
$data = array ( 'CARRIERDESCRIPTION', 'CARRIERLOGO' );
// the root that hold the data you want -> array()
$root = 'child';
// the child element that holds the array of data to grab -> array()
$child = 'attrs';
// builds and holds the database insert array
$hold = array ();
// get the xml file, URL or local path and name
$file = file_get_contents ( 'data.xml' );
// feed it to the XML parser!
$out = process_xml ( $file );
// process the data...
for ( $i = 0; $i < sizeof ( $out ); $i++ )
{
$next = $out[$i][$root];
for ( $h = 0; $h < sizeof ( $next ); $h++ )
{
$temp = array ();
foreach ( $data AS $v )
{
$temp[] = addslashes ( trim ( $next[$h][$child][$v] ) );
}
}
$hold[] = "( '" . implode ( "', '", $temp ) . "' )";
}
// just to show what would be inserted
// we do it this way so we do only (1) insert, no loops!
echo implode ( ', ', $hold );
/* uncomment this to do the real insert
mysql_connect ( 'localhost', 'admin', 'pass' );
mysql_select_db ( 'database_name' );
$sql = "INSERT INTO carrier VALUES " . implode ( ', ', $hold );
mysql_query ( $sql );
*/
?>
yj
Posted: Wed Nov 02, 2005 1:41 pm
by vincenzobar
implode is bring back this error
Warning: implode(): Bad arguments.
do you know why?
Posted: Wed Nov 02, 2005 3:12 pm
by vincenzobar
Nothing seems to work!!!
I know i am in over my head but this NEEDS to work! SCript above implode is broken, don't know why everything i read says it should work.
I got it to output to screen now i need it to write to a file if not insert into MySQL.
How do i get the values out of the array and parse them into a DB or File?
Posted: Wed Nov 02, 2005 3:38 pm
by Luke
vincenzobar wrote:implode is bring back this error
Warning: implode(): Bad arguments.
do you know why?
Did you do a check to make sure the second argument is an array? That's important.
Posted: Wed Nov 02, 2005 3:53 pm
by vincenzobar
that's it!! they are not arrays
Code: Select all
<? for ( $i = 0; $i < sizeof ( $out ); $i++ )
{
$next = $out[$i][$root];
for ( $h = 0; $h < sizeof ( $next ); $h++ )
{
$temp = array ();
foreach ( $data AS $v )
{
$temp[] = addslashes ( trim ( $next[$h][$child][$v] ) );
}
}
$hold[] = "( '" . implode( "', '", $temp ) . "' )";
}
// just to show what would be inserted
// we do it this way so we do only (1) insert, no loops!
echo implode( ', ', $hold ); ?>
is not making arrays... can anyone help me with this? I thought by using $temp[] makes an array???
this is killing me!
Posted: Wed Nov 02, 2005 4:16 pm
by Luke
I'm a little confused by how many loops you have in that code... I need to see a bigger picture...
Posted: Wed Nov 02, 2005 4:18 pm
by vincenzobar
it's posted above but i added some checks that told me $temp was not an array. and that is my problem I just have no idea how to fix it.
here is the code in whole:
Code: Select all
// first two functions are generic XML processing functions
// they can handle any XML tree, converting the data to
// a output array, it's better to keep things in serialized
// arrays than processing the slow XML data again and again!
function process_xml ( $in )
{
$xml = xml_parser_create ( 'ISO-8859-1' );
xml_parser_set_option ( $xml, XML_OPTION_SKIP_WHITE, 1 );
xml_parse_into_struct ( $xml, $in, $value, $name );
xml_parser_free ( $xml );
$x = 0;
$part = array ();
$part[$value[$x]['tag']] = get_extended ( $value, $x );
return ( $part );
}
function get_extended ( $next, &$i )
{
$node = array();
$j = 0;
if ( isset ( $next[$i]['value'] ) && $next[$i]['value'] )
{
array_push ( $node, $next[$i]['value'] );
}
$old = '';
while ( ++$i < count ( $next ) )
{
switch ( $next[$i]['type'] )
{
case 'cdata' :
array_push ( $node, $next[$i]['value'] );
break;
case 'complete' :
$node[$next[$i]['tag']] = $next[$i]['value'];
break;
case 'open' :
$j++;
if ( $old <> $next[$i]['tag'] )
{
$j = 0;
$old = $next[$i]['tag'];
}
$node[$next[$i]['tag']][$j] = get_extended ( $next, $i );
break;
case 'close' :
return ( $node );
}
}
}
// this is the data to insert in the databse
// carrier name, path and image logo name
// if you don't want to insert the image paths just remove
// it from the data array...
$data = array ( 'CARRIERDESCRIPTION', 'CARRIERLOGO' );
// the root that hold the data you want -> array()
$root = 'child';
// the child element that holds the array of data to grab -> array()
$child = 'attrs';
// builds and holds the database insert array
$hold = array ();
// get the xml file, URL or local path and name
$file = file_get_contents ( 'cdf.xml' );
// feed it to the XML parser!
$out = process_xml ( $file );
// process the data...
for ( $i = 0; $i < sizeof ( $out ); $i++ )
{
$next = $out[$i][$root];
for ( $h = 0; $h < sizeof ( $next ); $h++ )
{
$temp = array ();
if(is_array($data))
{
foreach ( $data AS $v )
{
$temp[] = addslashes ( trim ( $next[$h][$child][$v] ) );
}
} else
{
echo "no data in Data array";
}
}
if(is_array($temp))
{
$hold[] = "( '" . implode( "', '", $temp ) . "' )";
}else
{
echo "no data in Temp array";
}
}
// just to show what would be inserted
// we do it this way so we do only (1) insert, no loops!
echo implode( ', ', $hold );