Needhelp with arrays and MySQL - VERY VERY BADLY!!! Please!!

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

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

Needhelp with arrays and MySQL - VERY VERY BADLY!!! Please!!

Post by vincenzobar »

I have spent 4 days on this at 12 hrs_+ a day and I cannot figure it out. i need some major help with this! i am begging at this point~

here is what i got

Code: Select all

$array = $p->output;
$row = array();
$contentCD = array();
$contentCL = array();
$content = array();
$hold = array();

foreach($array as $key=>$val){ 
    if(is_array($val)){ 
        foreach($val as $k=>$v){ 
            //echo "$k: $v<BR>";
			if(is_array($v)){
				foreach($v as $k2=>$child){
				//echo "$k2: $child<BR>";
					if(is_array($child)){
						foreach($child as $k3=>$name){
						//echo "$k3: $name<BR>";
							if(is_array($name)){
								foreach($name as $k4=>$attributes){
									$hold[] = strtolower($k4);
									$row = array_unique($hold);
									$content[] = $attributes;
									$contentCD[] = trim($name['CARRIERDESCRIPTION']);
									$contentCL[] = trim($name['CARRIERLOGO']);
									
									
								}
							}	
						}
					}
				}
			}
        } 
    }
} //end first foreach

if(is_array($row)){
	$column = "(" . implode(", ", $row) . ")";
	echo $column;	
}else{
 echo " Row not an array";
}
echo "<BR><BR>and<BR><BR>";
	
if(is_array($contentCD)){ 	
	$insertCD = "'" . implode("', '", $contentCD) . "'";
	echo $insertCD;	
}else{
 echo "content not an array";
}

 echo "<BR><BR>and<BR><BR>";

if(is_array($contentCL)){ 
 	$insertCL = "'" . implode("', '", $contentCL) . "'";
	echo $insertCL;	
}else{
 echo "content not an array";
}

echo "<BR><BR>and<BR><BR>";

//even did a walk through the arrray	
if(is_array($content)){ 
	for($i=0; $i < count($content); $i++){
			$value[] = $content[$i];
			print "the value is ".$value[$i]."<bR>";
			
		}
	$insert = "'" . implode("', '", $value) . "'";
	echo "<BR>";
	
	echo $insert;	
}else{
 echo "content not an array";
}
Now my output looks like this: each section of code is seperated with "and"
(carrierdescription, carrierlogo)

and

'ALLTEL Wireless', 'ALLTEL Wireless', 'Cellular One', 'Cellular One', 'Cingular Wireless', 'Cingular Wireless', 'Liberty Wireless', 'Liberty Wireless', 'Nextel', 'Nextel', 'Sprint PCS', 'Sprint PCS', 'StarBox', 'StarBox', 'T-Mobile', 'T-Mobile', 'U.S. Cellular', 'U.S. Cellular', 'Verizon Wireless', 'Verizon Wireless'

and

'images/carriers/AllTel_logo.gif', 'images/carriers/AllTel_logo.gif', 'images/carriers/Cellularone-Lsf.gif', 'images/carriers/Cellularone-Lsf.gif', 'images/carriers/cingularlogo.gif', 'images/carriers/cingularlogo.gif', 'images/carriers/liberty_logo.gif', 'images/carriers/liberty_logo.gif', 'images/carriers/nextel_logo.gif', 'images/carriers/nextel_logo.gif', 'images/carriers/sprint_logo.gif', 'images/carriers/sprint_logo.gif', 'images/carriers/Motient_logo.gif', 'images/carriers/Motient_logo.gif', 'images/carriers/tmobile_logo.gif', 'images/carriers/tmobile_logo.gif', 'images/carriers/uscellular_l.gif', 'images/carriers/uscellular_l.gif', 'images/carriers/verizonLogo.gif', 'images/carriers/verizonLogo.gif'

and

the value is ALLTEL Wireless
the value is images/carriers/AllTel_logo.gif
the value is Cellular One
the value is images/carriers/Cellularone-Lsf.gif
the value is Cingular Wireless
the value is images/carriers/cingularlogo.gif
the value is Liberty Wireless
the value is images/carriers/liberty_logo.gif
the value is Nextel
the value is images/carriers/nextel_logo.gif
the value is Sprint PCS
the value is images/carriers/sprint_logo.gif
the value is StarBox
the value is images/carriers/Motient_logo.gif
the value is T-Mobile
the value is images/carriers/tmobile_logo.gif
the value is U.S. Cellular
the value is images/carriers/uscellular_l.gif
the value is Verizon Wireless
the value is images/carriers/verizonLogo.gif
Now you will notice in sections 2 and 3 they duplicate, Why?
...but when i loop through the unseperated array $content they only print once.

What i need is to be able to make my sql statement read properly

Code: Select all

$query = "INSERT INTO carrier $column VALUE ($value1, $value2), ($value1, $value2);";
I need carrierdescription and carrierlogo values per ()
I cannot figure out how to chop up the array to do this or peice together multiple arrays and then loop through them so i get a statement that reads like this:

...VALUES ('verizon', '/image/logo.gif'), ('tmobile', '/images/logos.gif'), and so on and so on.

Please GOD.. Some one have the answer to this!!!!! I think i have officially read every single line on PHP.net about arrays.

Thanks,
vin
yum-jelly
Forum Commoner
Posts: 98
Joined: Sat Oct 29, 2005 9:16 pm

Post by yum-jelly »

I sent you PM the other day asking for real example of your XML data so I could show you how to do what you want using a real example. I mean guessing is fun but seeing exactly what the data is, makes doing what you want easier!


post a link to the raw xml file and I will write you an example doing exactly what you want!


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

Post by vincenzobar »

It says i have no messages!! Weird!

http://www.underwaterdesign.com/xml/cdf.xml
or

Code: Select all

<root>
<carrier CarrierDescription="ALLTEL Wireless" CarrierLogo="images/carriers/AllTel_logo.gif"></carrier>
<carrier CarrierDescription="Cellular One" CarrierLogo="images/carriers/Cellularone-Lsf.gif"></carrier>
<carrier CarrierDescription="Cingular Wireless" CarrierLogo="images/carriers/cingularlogo.gif"></carrier>
<carrier CarrierDescription="Liberty Wireless" CarrierLogo="images/carriers/liberty_logo.gif"></carrier>
<carrier CarrierDescription="Nextel" CarrierLogo="images/carriers/nextel_logo.gif"></carrier>
<carrier CarrierDescription="Sprint PCS" CarrierLogo="images/carriers/sprint_logo.gif"></carrier>
<carrier CarrierDescription="StarBox" CarrierLogo="images/carriers/Motient_logo.gif"></carrier>
<carrier CarrierDescription="T-Mobile" CarrierLogo="images/carriers/tmobile_logo.gif"></carrier>
<carrier CarrierDescription="U.S. Cellular" CarrierLogo="images/carriers/uscellular_l.gif"></carrier>
<carrier CarrierDescription="Verizon Wireless" CarrierLogo="images/carriers/verizonLogo.gif"></carrier>
</root>
also my parser is

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;
       }      
   }
   
 	function contentArray(){

	foreach($this->output as $key => $value){
		$data = "" . $key . "; ";
		$this->cdata = str_replace($data, $value, $this->cdata);
		}
	print $this->cdata;
}  
   

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

Post by vincenzobar »

While i wait for your help i am still trying this on my own and i am getting soo close yet so far away

i change

Code: Select all

if(is_array($name)){
	foreach($name as $k4=>$attributes){
		$hold[] = strtolower($k4);
		$row = array_unique($hold);
		$content[] = $attributes;
			if($k4 == 'CARRIERDESCRIPTION'){
				$contentCD[] = $attributes;
			}
			else if($k4 == 'CARRIERLOGO'){
				$contentCL[] = $attributes;
			}
And this stopped the duplicates!!!!!!

theni did this

Code: Select all

if(is_array($contentCL) && is_array($contentCD)){
for($i=0; $i < count($contentCD); $i++){
			$valueCD[] = $contentCD[$i];
			//print "the value is ".$valueCD[$i]."<bR>";
			$valueCL[] = $contentCL[$i];
			//print "the value is ".$valueCL[$i]."<bR>";
			
 $insert = "('" . $valueCD[$i].", ".$valueCL[$i]."') ";
 echo $insert;
 
 }
 
	
}else{
 echo "content not an array";
}
but this is where i loose it again because as soon as i take $insert out of the for loop i only retrieve the last values in the arrays

I'll keep trying but let me know if you get anything!
yum-jelly
Forum Commoner
Posts: 98
Joined: Sat Oct 29, 2005 9:16 pm

Post by yum-jelly »

Download....

http://24.91.34.102/xml.zip

After downloding look it over then run * test.php * in your browser. After delete the output echo in test.php and setup your db stuff, then uncomment the mysql_query() and insert it!


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

Post by vincenzobar »

looking over it now thanks so far!!!
vincenzobar
Forum Commoner
Posts: 95
Joined: Wed Nov 02, 2005 9:57 am

Post by vincenzobar »

well that worked out perfectly. Now for one last favor. can you explain a little on what you did inthere because now i need to grap the table names from the array as well like i did previously but because there are no comments i am having a hard time following it.

Like this line:
$temp[] = addslashes ( trim ( ( ! empty ( $temp ) ? strtolower ( $dv ) : $dv ) ) );

what is that about? "... :$dv) don't know that programming!

And is xml.php specifically for this file or can it be used universally because after i figure this out i got 3 more to do and they contain different stuff. That was why i was using that Class from PHP.net.

But So far you are a life saver~~~

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

Post by vincenzobar »

BTW I added this

Code: Select all

foreach ( $av AS $dn => $dv ){
	$hold1[] = strtolower($dn);
	$column = array_unique($hold1);
										
		$temp[] = addslashes ( trim ( ( ! empty ( $temp ) ? strtolower ( $dv ) : $dv ) ) );
}
	$table = "(".implode(", ", $column). ")";
		$hold[$x] = "( '" . implode ( "', '", $temp ) . "' )";
		$x++;
and got exactly what i needed
INSERT INTO (carrierdescription, carrierlogo) VALUES ( 'ALLTEL Wireless', 'images/carriers/alltel_logo.gif' ), ( 'Cellular One', 'images/carriers/cellularone-lsf.gif' ), ( 'Cingular Wireless', 'images/carriers/cingularlogo.gif' ), ( 'Liberty Wireless', 'images/carriers/liberty_logo.gif' ), ( 'Nextel', 'images/carriers/nextel_logo.gif' ), ( 'Sprint PCS', 'images/carriers/sprint_logo.gif' ), ( 'StarBox', 'images/carriers/motient_logo.gif' ), ( 'T-Mobile', 'images/carriers/tmobile_logo.gif' ), ( 'U.S. Cellular', 'images/carriers/uscellular_l.gif' ), ( 'Verizon Wireless', 'images/carriers/verizonlogo.gif' )
Thanks a bunch now its time to transfer it to all the other files!!!
vincenzobar
Forum Commoner
Posts: 95
Joined: Wed Nov 02, 2005 9:57 am

Post by vincenzobar »

one last thing... here is my DB stuff and it isn't inserting

Code: Select all

/db_connect() or exit();
$conn = mysql_connect('localhost', 'vincenzo_xml', '00000');
$carrier_dbt = mysql_select_db('vincenzo_xml');
//check connect
if($conn && $carrier_dbt){
print "<BR><BR>connected<BR>";
}else{
echo "<BR><BR>could not connect to mysql<BR>";
}

//insert SQL Statement
	$sql = "INSERT INTO 'carrier' $rows VALUES " . implode ( ', ', $hold ).";";

	echo $sql;
	//mysql_query($sql);

	//Stick it in
if(mysql_query($sql)){
	print "<BR>info inserted successfully";
}else{
	print "<BR>did not insert";
}

//187 connection
mysql_close();
my string looks right, what's up???
connected
INSERT INTO 'carrier' ('carrierdescription', 'carrierlogo') VALUES ( 'ALLTEL Wireless', 'images/carriers/alltel_logo.gif' ), ( 'Cellular One', 'images/carriers/cellularone-lsf.gif' ), ( 'Cingular Wireless', 'images/carriers/cingularlogo.gif' ), ( 'Liberty Wireless', 'images/carriers/liberty_logo.gif' ), ( 'Nextel', 'images/carriers/nextel_logo.gif' ), ( 'Sprint PCS', 'images/carriers/sprint_logo.gif' ), ( 'StarBox', 'images/carriers/motient_logo.gif' ), ( 'T-Mobile', 'images/carriers/tmobile_logo.gif' ), ( 'U.S. Cellular', 'images/carriers/uscellular_l.gif' ), ( 'Verizon Wireless', 'images/carriers/verizonlogo.gif' );
did not insert
Any Ideas... anyone?
yum-jelly
Forum Commoner
Posts: 98
Joined: Sat Oct 29, 2005 9:16 pm

Post by yum-jelly »

The loop is generic in that you want to stay away from using [keys] even if you know them, because data even XML data is never alway valid. By keeping the loops generic the loop can even loop through simple errors, such as spelling or missing elements because it bases it's self on one rule, go to what I am looking for and keep doing that until the document is done!...

What that means is it can handle multi ROOTS or multi CHILDREN, it looks only where it should find the data it needs...

So if root had children it would look inside each child root and if it found the array matching each of the roots children it would follow the children down until it found what it was looking for, if it doesn't find any children it auto breaks to begin the next look through! Checking all nodes for what it is looking for!

this is.... Conditional Operator (Ternary ?:)

Code: Select all

$temp[] = addslashes ( trim ( ( ! empty ( $temp ) ? strtolower ( $dv ) : $dv ) ) );
Is the same thing as doing this....

Code: Select all

if ( ! empty ( $temp ) )
{
$temp[] = addslashes ( trim ( strtolower ( $dv ) ) );
}
else
{
$temp[] = addslashes ( trim ( $dv ) );
}
But it's little different in that you don't have to assign it a value and it can be used in any other operator or function(). It evals from left -> right. If left returns TRUE, it returns the value held on the left side of * : * if it evals FALSE it then returns the value on the right side of * : *!

Here are few examples

// assign and return a value

Code: Select all

<?

$x = 1;

$out = ( $x == 1 ? 'hi' : 'bye' );

echo $out; // prints: hi

?>
// return a value (this is different in that it returns a value for it operation, if the eval is false it returns NULL)

Code: Select all

echo "<option value='1'" . ( isset ( $option ) ? ' selected' : null ) . ">test</option>";
Think of it as a tool to ask another question with in another question!

So I an using it this way....

If array $temp is set (not empty/has value) then lowercase that value which in this case is the value that holds the image URL/PATH. If it is empty (has no value) then just trim the value which in this case are the names of each carrier!


is the xml.php specifically for this file or can it be used universally!

Universally...

If you need to tell what * attribute * name it is working on that will be found in!

Code: Select all

$dn <- key

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

Post by vincenzobar »

You the man .. thanks for explaining that!

I also found my problem with the SQL statement... been working for 14 hours straight and forgot about NO ' on table or columns!!!

durrrrrrr

Thanks alot for all your help, I owe ya big time!
yum-jelly
Forum Commoner
Posts: 98
Joined: Sat Oct 29, 2005 9:16 pm

Post by yum-jelly »

see what the error is!

Code: Select all

mysql_query ( $sql ) or die ( mysql_error () );

Ok I will tell you what the error is!....

Code: Select all

'carrier'
If you must quote your table_name or column names use * ` * not single quotes * ' *. Anyways it not needed unless you got names that are reserved words which you should change anyways!


yj
Post Reply