Solved

How to split up an array into a query

Posted on 2011-03-16
7
318 Views
Last Modified: 2012-05-11
I have an array I need to turn into a query:
    [7] => 0|typ=spcl
    [8] => 14|typ=sem
    [10] => 1|typ=spcl
    [11] => 15|typ=sem
 I need to split it into this query:

    [7] => 0|typ=spcl
 timeslotid='7', spclactivityid-'0'

    [8] => 14|typ=sem
timeslotid='8', seminarid-'14'

    [10] => 1|typ=spcl
timeslotid='10', spclactivityid-'1'

    [11] => 15|typ=sem
timeslotid='11', seminarid-'15'

Thanks,
Jeff
0
Comment
Question by:Jeff
  • 4
  • 3
7 Comments
 
LVL 8

Expert Comment

by:Rik-Legger
ID: 35151913
I think this will help you:

    $array = array(
    	7 => '0|typ=spcl',
    	8 => '14|typ=sem',
    	10 => '1|typ=spcl',
    	11 => '15|typ=sem',
   	);
   	
   	foreach ($array as $timeslotid => $value) {
   		
   		$aValue = explode('|', $value);
   		echo 'timeslotid=\'' .$timeslotid . '\', ' . ($aValue[1] == 'typ=spcl' ? 'spclactivityid' : ($aValue[1] == 'typ=sem' ? 'seminarid' : '')) . '-\'' . $aValue[0] . '\',';
   	}

Open in new window

0
 

Author Comment

by:Jeff
ID: 35151995
Rik,
That kinda works. I failed to mention it is coming from a post with some other vars. Here is the complete post:

Array
(
    [SubmitChk] => True
    [7] => 0|typ=spcl
    [8] => 14|typ=sem
    [9] => 0|typ=sem
    [10] => 1|typ=spcl
    [11] => 15|typ=sem
    [12] => 0|typ=sem
    [FirstName] =>
    [LastName] =>
    [CompanyName] =>
    [Title] =>
    [Email] =>
    [CStreet] =>
    [CPhone] =>
    [CCity] =>
    [Cell] =>
    [CState] =>
    [Fax] =>
    [CZip] =>
    [Comments] =>
    [submit] => Submit Form
)

I am getting an error from the other vars in the array.

Notice: Undefined offset: 1 in /home/jeffmorr/public_html/tylerwebdesign.com/hel.org/custom/etcseminar.php on line 34

Notice: Undefined offset: 1 in /home/jeffmorr/public_html/tylerwebdesign.com/hel.org/custom/etcseminar.php on line 34
timeslotid='SubmitChk', -'True',timeslotid='7', spclactivityid-'0',timeslotid='8', seminarid-'14',timeslotid='9', seminarid-'0',timeslotid='10', spclactivityid-'1',timeslotid='11', seminarid-'15',timeslotid='12', seminarid-'0',
Notice: Undefined offset: 1 in /home/jeffmorr/public_html/tylerwebdesign.com/hel.org/custom/etcseminar.php on line 34

Notice: Undefined offset: 1 in /home/jeffmorr/public_html/tylerwebdesign.com/hel.org/custom/etcseminar.php on line 34
timeslotid='FirstName', -'',
Notice: Undefined offset: 1 in /home/jeffmorr/public_html/tylerwebdesign.com/hel.org/custom/etcseminar.php on line 34

etc....
0
 
LVL 8

Accepted Solution

by:
Rik-Legger earned 500 total points
ID: 35152250
Ok, so if you want them all in the query than it would be something like this,
this is just a rough example:

<?php 

    $array = array(
	    'FirstName' => 'test',
	    'LastName' => 'test',
    	7 => '0|typ=spcl',
    	8 => '14|typ=sem',
    	10 => '1|typ=spcl',
    	11 => '15|typ=sem',
   	);
   	
   	foreach ($array as $key => $value) {
   		
   		if (is_numeric($key)) {
   			$aValue = explode('|', $value);
   			echo 'timeslotid=\'' .$timeslotid . '\', ' . ($aValue[1] == 'typ=spcl' ? 'spclactivityid' : ($aValue[1] == 'typ=sem' ? 'seminarid' : '')) . '-\'' . $aValue[0] . '\',';
   		} else {
   			// Ignore other values or put them also in a query string
   			echo $key . '=\'' . $value . '\','; 
   		}
  	}

?>

Open in new window

0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:Jeff
ID: 35152368
I actually need to split the query. I will grab the customer id from a previous insert with "$participantid = mysql_insert_id ();".
I will then insert each event they will be attending. Each line will be a seperate query:

the typ tells whether it is a sem=Seminar or spcl=Special Activity (breakfast, lunch etc.)

Query 1:
[7] => 0|typ=spcl
INSERT INTO table  participantid='1', timeslotid='7', spclactivityid-'0' // 0=Not Attending

Query 2:
[8] => 14|typ=sem
INSERT INTO table  participantid='1', timeslotid='8', seminarid-'0' // 0=Not Attending

Query 3:
[9] => 14|typ=sem
INSERT INTO table  participantid='1', timeslotid='9', seminarid-'14'

Query 4:
[10] => 1|typ=spcl
INSERT INTO table  participantid='1', timeslotid='10', spclactivityid-'1'

Query 5:
[11] => 15|typ=sem
INSERT INTO table  participantid='1', timeslotid='11', seminarid-'15'

Query 6:
 [12] => 0|typ=sem
INSERT INTO table  participantid='1', timeslotid='12', seminarid-'0' // 0=Not Attending
0
 
LVL 8

Expert Comment

by:Rik-Legger
ID: 35152405
Your question got a little bit sidetracked here.
I'm not sure what you want now but you should be able to use my examples in some way to get your desired result.
0
 

Author Comment

by:Jeff
ID: 35152625
Yep, Got it! Thanks MUCH!
0
 

Author Closing Comment

by:Jeff
ID: 35152630
Thank you!
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now