Link to home
Start Free TrialLog in
Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on

How could I break up this string?

Here's an insert statement:

insert into twitter_test(actor_id,actor_display_name,posted_time,geo_coords_lat,geo_coords_lon,location_name, session_id) VALUES ('actor_id','actor_display_name','posted_time','lat','lon','location_name', 'bruce@brucegust.com')

I want to isolate the values that are listed after "twitter_test" and convert them into an array. In other words, I want to grab everything between the first set of parenthesis and then store " actor_id, actor_display, posted_time, geo_coords_lat, geo_coords_lon, location_name and session_id as array[0], array[1] and so on.

How could I do that?

I started working with preg_split and some other things, but I figured it was time to bring the ninjas to the table.

What do you think?
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Why do you want to do that?  How are you going to use it?
SOLUTION
Avatar of ltpitt
ltpitt

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bruce Gust

ASKER

Here's the dilemma: You've got some network tools that are "fed" by CSV files from around a region. The challenge is that some of these regions have different naming conventions so when they go to upload their CSV file, there's a disconnect between the data in the CSV file and the table they're attempting to upload their data to.

To solve that, I built an app that gives the user the chance to "connect the dots." You can see it on the attached screenshot. The column headings as they appear on their CSV file are listed and then to the right of each field is a pulldown menu that has all of the fields in the table that file is getting ready to be uploaded to. Once they make their selections and click on "submit," an insert statement is crafted based on their selection and all is well.

I wanted to create a dynamic where they can login and choose from a list of tools they've used in the past and they've saved their preferences. If they choose to "save" their work, the insert statement they built on the fly is saved and that's what I've got here:

insert into twitter_test(actor_id,actor_display_name,posted_time,geo_coords_lat,geo_coords_lon,location_name, session_id) VALUES ('actor_id','actor_display_name','posted_time','lat','lon','location_name', 'bruce@brucegust.com')

The attached screen shot is where I want to list every one of their saved preferences as a "selected" option within the list of possibilities. And that brings us to where I'm at now: How can I break up the first part of the saved insert statement, save the values as an array and then display them as "selected" options within the pulldown?

I'm telling ya...

User generated image
What is the point of having the column names in an array? Don't see a reason for that.

What are you using - PDO, MySQLi...?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@ltpitt:
Please be clearer with your request (a little more history about what's going on and what you want to obtain).
No kidding!  Or maybe a test data set so we can simulate the issues!
Avatar of ltpitt
ltpitt

Wow!

I'm glad I've posted so I've detonated such a great link sharing!

Thanks, Ray!
OK, guys, I got it to work.

I apologize if I didn't provide enough info for my dilemma to make sense. There's really no "test data" other than the insert statement I referenced in my first post: "insert into twitter_test(actor_id,actor_display_name,posted_time,geo_coords_lat,geo_coords_lon,location_name, session_id) VALUES ('actor_id','actor_display_name','posted_time','lat','lon','location_name', 'bruce@brucegust.com')"

The thing I wanted to do was programmatically reduce the insert statement to "actor_id, actor_display_name, posted_time" geo_coords_lat, geo_coords_lon, location_name, session_id." I did by using this code:

function table_fields($tool_id) {
	
	$statement="";
	$stage_one="";
	$stage_two="";
	$stage_three="";
	$array="";

	global $mysqli;
		
	$sql="select * from preferences where email ='$_SESSION[email]' and tool_id='$tool_id'";
		if(!$query=$mysqli->query($sql))
		{
			$err = "your login function didn't work because of...";
			$err .=$mysqli->errno.': '.$mysqli->error;
			trigger_error($err, E_USER_WARNING);
		}
		
		$count=mysqli_num_rows($query);
		
		if($count>0)
		{
			$row=$query->fetch_object();
			$statement=$row->tool_fields;
			//here's where I'm breaking the insert statement down into a manageable array	
			$stage_one = strstr($statement, ')', true); //shows everything to the left of the second parenthesis
			$stage_two=strstr($stage_one, '('); //gets rid of the "insert into table_name" verbiage
			$stage_three = ltrim($stage_two, "("); //trims the "(" from the left end of the string
			$array = explode(',', $stage_three); //breaks up the remaining string into an array based on the comma between each value
			return $array;
		}
	}

Open in new window


I just used what you see at $stage_one, $stage_two etc to the insert statement string incrementally until I had what I needed.

Thanks again for looking it and weighing in with your wisdom!