Solved

Create variables in a foreach loop

Posted on 2014-03-25
20
244 Views
Last Modified: 2014-03-25
Hi,

i have a foreach loop containing names of columns which are the exact same of variables found within a form i have created. The form is dynamically created via the names of the columns in an sql table. i am curious if it is possible is possible to do something like so

$array="id, name, status, content, author";
foreach($array as $item){
   
   $new_array[]="(:".$item.") =>".$item;

}

Open in new window


where the first array item $new_array[0]  would be (:id)=>$id where the $id is the actual id variable created by the form

thanks in advance
0
Comment
Question by:M. Jayme Nagy
  • 7
  • 6
  • 4
  • +1
20 Comments
 
LVL 58

Expert Comment

by:Gary
Comment Utility
If you want to create an array from a string then just do

$array="id, name, status, content, author";
$new_array=explode(", ",$array);

Open in new window

Anything other than that then I have no idea what you are trying to do.

Kinda looks like you are trying to build up a PDO array for execution...
0
 
LVL 30

Expert Comment

by:Marco Gasi
Comment Utility
If you just want each item of the new array be a string formatted 'as' array key-value your code is right.
0
 
LVL 30

Expert Comment

by:Marco Gasi
Comment Utility
Kinda looks like you are trying to build up a PDO array for execution...
Great guess!
So you just build an array and pass it to PDO

$array="id, name, status, content, author";
foreach($array as $item){
   
   $new_array["(:".$item.")"]= $item;

}
$sth->execute($new_array);

Open in new window

0
 
LVL 6

Author Comment

by:M. Jayme Nagy
Comment Utility
Thats exactly what i am trying to accomplish

i am trying to create it dynamically so i do not need to make a separate one for each form i have.


sorry $array is an actual array not a string.
so it would look more like

array{
  [0] =>id
  [1].....
}
0
 
LVL 42

Expert Comment

by:Chris Stanyon
Comment Utility
If you are indeed building an array for PDO, and you form inputs EXACTLY match your table columns, then you don't need to build an array - you already have one - $_POST!

Just pass that into your query.

If you're building an array for some other reason, then we'll need more info. Firstly, you'll need to split out your string as Gary has pointed out. Then you'll need to tell us where you're planning on get the value of $id from.
0
 
LVL 30

Accepted Solution

by:
Marco Gasi earned 500 total points
Comment Utility
Or maybe:
$array="id, name, status, content, author";
foreach($array as $item){
   
   $new_array["(:".$item.")"]= $$item;

}
$sth->execute($new_array);

Open in new window

0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
And where are the values coming from? All that will do is set the array index name and the value to the same thing...which kinda doesn't make sense.

(Chris already asked this)
0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
Is it posted values? Something like this maybe.

$pdo= array();
foreach($column_array as $col){
	$pdo=array_merge($pdo,array(":".$col=>$_GET[$col]));
}

$stmt->execute($pdo);

Open in new window


edit
I guess Marco's comment is doing basically the same thing.
0
 
LVL 42

Expert Comment

by:Chris Stanyon
Comment Utility
Why, why, why do people insist on creating problems where there are none?

If your data is being sent from a form and that data already matches your database table, why go to all the trouble of creating another array?
0
 
LVL 6

Author Comment

by:M. Jayme Nagy
Comment Utility
because i create separate variables from the post array and format / test them independently. as well as some perform different tasks. so the $_POST array is not EXACTLY the same

the original array puts everything back together so i can use it they way i want
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 42

Expert Comment

by:Chris Stanyon
Comment Utility
OK, but if you want to pass your array into a PDO prepared statement, this won't work:

$new_array["(:".$item.")"]= $$item;
0
 
LVL 30

Expert Comment

by:Marco Gasi
Comment Utility
I would agree with Chris, but I have two little doubts: first, one has anyway to filter $_POST array from submit. Secondly, that POST array leys have to be edited to add the prefix colon for prepared statements.
This second problem is eaily solvable changing the names of your form fields: add there the prefix and you'll get the $_POST array almost ready.

For the first problem, simply use array_pop() to delete the last value of the $_POST array, the submit value.

Try this code to see how it works:
<?php
if (isset($_POST['submit']))
{
	array_pop($_POST);
	echo "<pre>";
	var_dump($_POST);
	echo "</pre>";
}
?>
<form action="" method="post">
	<input type="text" name=":id" value="1" />
	<input type="text" name=":name" value="your_name" />
	<input type="text" name=":status" value="your_status" />
	<input type="text" name=":content" value="your_content" />
	<input type="text" name=":author" value="your_author" />
	<input type="submit" name="submit" value="Submit" />
</form>

Open in new window


At this point, you pas $_POST array to pdo->execute() instead of dumping it.

And now, I think you have to request attention and at least split points between all experts :)

Cheers
0
 
LVL 42

Expert Comment

by:Chris Stanyon
Comment Utility
Firstly, just don't give your Submit button a 'name' attribute and it won't get passed in the $_POST array.

Secondly, the array to pass into PDO doesn't have to have the colon prefix - that's just for the placeholders themselves.
0
 
LVL 30

Expert Comment

by:Marco Gasi
Comment Utility
Clean and simple! :) Let me ask just a thing: I didn't test my code, but why wouldn't it work?

I mean $new_array["(:".$item.")"]= $$item;
0
 
LVL 6

Author Comment

by:M. Jayme Nagy
Comment Utility
$new_array["(:".$item.")"]= $$item;

should be

 $new_array[":".$item.""]= $$item;

that was my mistake
0
 
LVL 42

Expert Comment

by:Chris Stanyon
Comment Utility
Your array would look like this:

array(
   (:var1) => value1,
   (:var2) => value2
);


That won't work with PDO because of the brackets. You need something like this:

array(
   var1 => value1,
   var2 => value2
)


and although you don't 'need' the colon prefix, it will still work like this:

array(
   :var1 => value1,
   :var2 => value2
)
0
 
LVL 30

Expert Comment

by:Marco Gasi
Comment Utility
Thanks, Chris.

@M. Jayme Nagy: as you see, that was a my mistake too. Even not using $_POST array, I should have seen those stupid brakets and I should have deleted them! Anyway, I think you can consider to split points.

Cheers
0
 
LVL 6

Author Comment

by:M. Jayme Nagy
Comment Utility
not sure if i can go back but if you would like it that way then im fine with it
0
 
LVL 42

Expert Comment

by:Chris Stanyon
Comment Utility
I'm not worried about the points.

I just wanted it shown that the answer accepted was not correct, nor best-practice. This was more for the benefit of future-readers than yourself.
0
 
LVL 30

Expert Comment

by:Marco Gasi
Comment Utility
To split points simply request attention using the link below the question box and ask the moderator to reopen question, then assign points as needed.

Thank you
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
API Soap Calls 4 55
What's wrong with this query? 9 49
Wordpress syntax error 1 27
Link SQL table to Webpage 9 34
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
This article discusses four methods for overlaying images in a container on a web page
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

763 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

6 Experts available now in Live!

Get 1:1 Help Now