[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to split up an array into a query

Posted on 2011-03-16
7
Medium Priority
?
348 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 count occurrences of each item in an array.
Suggested Courses

656 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