Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-17-2008, 01:55 PM   PM User | #1
rf9
New Coder

 
Join Date: Jun 2008
Posts: 11
Thanks: 1
Thanked 0 Times in 0 Posts
rf9 is an unknown quantity at this point
Array data to MySQL

Hi all,
I've got an array of data and I need to figure out how to deal with it and insert it into a database. The array looks like this:
Code:
array(2) { 
	[0]=>  array(3) { 
		["labelName"]=>  string(9) "First name" 
		["inputType"]=>  string(22) "textfield" 
		["validate"]=>  string(7) "no" } 
	[1]=>  array(3) { 
		["labelName"]=>  string(9) "Last name" 
		["inputType"]=>  string(28) "textfield" 
		["validate"]=>  string(7) "no" } }
So the array above would create 2 rows in a database. I have dealt with arrays before but this one is a little more complex to me. I guess some kind of loop will be needed to get the values and then pass them to an insert query. Any advice much appreciated.

Many thanks in advance.
rf9 is offline   Reply With Quote
Old 07-17-2008, 02:13 PM   PM User | #2
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
try this:
PHP Code:
function extr($a){
   return (
'('.join(",",array_values($a)).')');
}
$values join(',',array_map("extr",$oldarr)); 
not tested, but if is correct is must return something like this:
Quote:
("First name", "textfield", "no"), ("Last name", "textfield", "no")
regards
oesxyl is offline   Reply With Quote
Users who have thanked oesxyl for this post:
rf9 (07-17-2008)
Old 07-17-2008, 03:44 PM   PM User | #3
rf9
New Coder

 
Join Date: Jun 2008
Posts: 11
Thanks: 1
Thanked 0 Times in 0 Posts
rf9 is an unknown quantity at this point
Hi again
Thanks for the help, the code works great and outputs as you expected.

Just to clarify, $a is my array.
Not sure about $oldarr, I replaced this with $a and it works fine.

I just need to figure out the query now and it should be sorted.
rf9 is offline   Reply With Quote
Old 07-17-2008, 04:30 PM   PM User | #4
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by rf9 View Post
Hi again
Thanks for the help, the code works great and outputs as you expected.

Just to clarify, $a is my array.
Not sure about $oldarr, I replaced this with $a and it works fine.

I just need to figure out the query now and it should be sorted.
no, $oldarr is your array. $a is the argument for extr function, you don't use it.
$values is last part of the query( see the thread from mysql forum).

PHP Code:
$query 'insert into yourtablename (labelName, inputType, validate) values '.$values;
echo 
$query
regards
oesxyl is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:36 AM.


Advertisement
Log in to turn off these ads.