Solved

How to split up an array into a query

Posted on 2011-03-16
7
327 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Currency in SQL? 2 33
Help with PHP 13 27
Undefined variable problem 5 23
Showing multiple maps using PHP and Google Maps 12 24
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…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

808 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