?
Solved

How to split up an array into a query

Posted on 2011-03-16
7
Medium Priority
?
343 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

 

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

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

752 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