Solved

How to split up an array into a query

Posted on 2011-03-16
7
303 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:jeffey48
  • 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:jeffey48
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
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:jeffey48
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:jeffey48
ID: 35152625
Yep, Got it! Thanks MUCH!
0
 

Author Closing Comment

by:jeffey48
ID: 35152630
Thank you!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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 dynamically set the form action using jQuery.

744 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

15 Experts available now in Live!

Get 1:1 Help Now