Need to put arrays into MySQL...How?

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

Need to put arrays into MySQL...How?

Post by vincenzobar »

This is part 2 for me I finally was able to paarse the XML Sheeet and get the values i wanted out of them

Code: Select all

$row = array();
$content = 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){
									$row = strtolower($k4);
									$content = $attributes;
									
									echo "$row; $content; ";
								}
							}	
						}
					}
				}
			}
        } 
    }
} //end first foreach
now i need to get the info out of the arrays $row and $content into MySQL and i can't for the life of me figure out how. only 2 of the 9 outputs

Code: Select all

carrierdescription; ALLTEL Wireless; carrierlogo; images/carriers/AllTel_logo.gif;
carrierdescription; Cellular One; carrierlogo; images/carriers/Cellularone-Lsf.gif;
now i need Array $conent to insert into the appropriate column decided by the array $row which is the column name "carrierdescripotion" and "carrierlogo".

so table "carrier" has 3 columns
id
carrierdescription
carrierlogo

and $content needs to go in the proper columns

how do i do this.?

Thanks again!
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

serialize()

or:

implode()

Or a recursive function of your own.

EDIT: Just seen you want each indice in it's own column, if that's the case you will need to specify which indice is to go in which column, by referring to the indices explicitly (e.g. $array[0])
vincenzobar
Forum Commoner
Posts: 95
Joined: Wed Nov 02, 2005 9:57 am

Post by vincenzobar »

I did a count on $row out side of the foreach loop

echo count($row);

and it gave me "1"

why are my arrays not staying as arrays

also i am messing with

Code: Select all

$rowCount = count($row);
for ($i=0; $i < $rowCount; $i++) {
echo $row[$i];
}
and i get "c"

and i cannot figure out how to use serialize. It only gives me the output but i cannot unserialize or trim it to just the values.

HELP I am Drowning!!!!
Last edited by vincenzobar on Thu Nov 03, 2005 10:29 am, edited 1 time in total.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Do the following to show the structure of the array:

Code: Select all

print_r($array);
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Ouch! That's first bit of code :?

Code: Select all

function print_deepest($array)
{
    $tmp = array();

    $count = count($array);
    $i = 0;
    foreach ($array as $k => $v)
    {
        $i++;
        if (is_array($v))
        {
            print_deepest($v);
        }
        else
        {
            $tmp[$k] = $v;
            if (($i) >= $count)
            {
                foreach ($tmp as $tK => $tV) echo "$tK: $tV<br />";
                break;
            }
        }
    }
}
I was gonna suggest writing a recursive function until I figured it's pretty hard to know when you're at the deepest point once I started writing that. Not tested.

EDIT | DOH... Just tested... it just does a kinda of print_r lol... might fix this later
Last edited by Chris Corbyn on Thu Nov 03, 2005 10:37 am, edited 1 time in total.
vincenzobar
Forum Commoner
Posts: 95
Joined: Wed Nov 02, 2005 9:57 am

Post by vincenzobar »

ive done all that here is my code and here is my output

Code: Select all

//function parse_data($array){
$row = array();
$content = 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){
									$row = strtolower($k4);
									$content = $attributes;
									$all = serialize($row);
									echo count($row);
									echo "$row; $content; $all";
								
								
									
								}
							}	
						}
					}
				}
			}
        } 
    }
} //end first foreach
//} //end function

// loop through $row
$rowCount = count($row);
for ($i=0; $i < $rowCount; $i++) {
     echo "$row[$i]";
//$insertquery = INSERT INTO info VALUES('$prog_descr_text', '$row[$i]');
//mysql_query($insertquery);

}

print_r($row);
echo count($row);
echo count($content);

Code: Select all

1carrierdescription; ALLTEL Wireless; s:18:"carrierdescription";
1carrierlogo; images/carriers/AllTel_logo.gif; s:11:"carrierlogo";
1carrierdescription; Cellular One; s:18:"carrierdescription";
1carrierlogo; images/carriers/Cellularone-Lsf.gif; s:11:"carrierlogo";
1carrierdescription; Cingular Wireless; s:18:"carrierdescription";
1carrierlogo; images/carriers/cingularlogo.gif; s:11:"carrierlogo";
1carrierdescription; Liberty Wireless; s:18:"carrierdescription";
1carrierlogo; images/carriers/liberty_logo.gif; s:11:"carrierlogo";
1carrierdescription; Nextel; s:18:"carrierdescription";
1carrierlogo; images/carriers/nextel_logo.gif; s:11:"carrierlogo";
1carrierdescription; Sprint PCS; s:18:"carrierdescription";
1carrierlogo; images/carriers/sprint_logo.gif; s:11:"carrierlogo";
1carrierdescription; StarBox; s:18:"carrierdescription";
1carrierlogo; images/carriers/Motient_logo.gif; s:11:"carrierlogo";
1carrierdescription; T-Mobile; s:18:"carrierdescription";
1carrierlogo; images/carriers/tmobile_logo.gif; s:11:"carrierlogo";
1carrierdescription; U.S. Cellular; s:18:"carrierdescription";
1carrierlogo; images/carriers/uscellular_l.gif; s:11:"carrierlogo";
1carrierdescription; Verizon Wireless; s:18:"carrierdescription";
1carrierlogo; images/carriers/verizonLogo.gif; s:11:"carrierlogo";

c
carrierlogo
1
1
How do i put all the info from the foreach loop to ONE array so that count = 9
vincenzobar
Forum Commoner
Posts: 95
Joined: Wed Nov 02, 2005 9:57 am

Post by vincenzobar »

LOL

adding [] to row and content

gave me my array!

i willplay somemore


don't go far!!!
Post Reply