I am trying to implode inputnames from $_POST with commas and no comma at the end. I have been able to successfuly implode $_POST values but am unable to do the same for inputnames from $_POST. Please help
if(isset($_POST['submit1'])){
$curry = "";
foreach ($_POST as $name => $val)
{
$curry .= $name;
}
echo $curry;
echo implode(",",(array_slice($_POST,0,-2))); //this works for value
echo implode(',',$curry); // this doesnt work for input names
This is because $curry isn't an array which you can implode! It is a string.
So if you change these lines:
$curry = "";
//...
$curry .= $name;
to this:
$curry = array();
//...
$curry[] = $name;
Then it is a array which you can implode.
array_keys() returns an array containing the keys of the input array. You'd use it as follow:
if(isset($_POST['submit1'])){
echo implode(",",(array_slice($_POST,0,-2)));
echo implode(',',(array_slice(array_keys($_POST),0,-2)));
Related
I'm having issues building a proper URL parameter sent to a service. I need to send dynamic query results in one parameter. Here's what I have reached so far:
<a href="https://serviceurl/?language=EN&variable1=<?php
$mobiles = array();
while($row_query = mysql_fetch_assoc($query))
{
$data = $row_query['rowwithvalues'];
$myArray = implode(',', $data);
foreach($myArray as $my_Array){
echo $my_Array;
}
}
?>&variable2=Hello">Test</a>
The above returns a blank value for variable1 in the url.
Thanks in advance!
When you calling $myArray = implode(',', $data); it makes $myArray string from implode doc:
Returns a string containing a string representation of all the array elements in the same order, with the glue string between each element.
So you cannot loop on it - I guess you wanted to use explode. Try this:
<a href="https://serviceurl/?language=EN&variable1=
<?php
while($row_query = mysql_fetch_assoc($query)) {
$data = $row_query['rowwithvalues'];
$myArray = explode(',', $data);
foreach($myArray as $my_Array){
echo $my_Array;
}
}
?>&variable2=Hello">Test</a>
on an PHP POST, is there a way of automatically setting each POST variable to its own named variable?
For example if I post
name = "henry"
age = "20"
location = "earth"
Instead of doing:
$name = $_POST['name'];
$age = $_POST['age'];
$location = $_POST['location'];
is there a way of looping through all POST variables and setting it to the same named standard variable?
Extract function can help with this issue:
$_POST = ['foo' => 'bar'];
extract($_POST);
var_dump($foo) //returns 'bar'
http://php.net/manual/en/function.extract.php
It is not recommended to set $_POST variables programmatically. However, if you want it you can use extract function
extract($_POST,EXTR_OVERWRITE,'prefix');
You can be use this:
if ($_POST) {
foreach ($_POST as $key => $value) {
$name = "{$key}";
$$name = $value;
echo "<pre>";
echo $name;
}
echo "<pre>";
echo $name;
echo "<pre>";
echo $age;
echo "<pre>";
echo $location;
}
I am trying get a form submitted values in array after isset() but it store only last isset() value in array. What is the right way to get all not null values in array to pass to insert function.
$table = 'booking';
if(isset($_POST['tourID'])){
$data = array('tour_fk' => $this->input->post('tourID'));
}
if(isset($_POST['bookingNumber'])){
$data = array('booking_number' => $this->input->post('bookingNumber'));
}
$query = $this->dashboard_model->insert($table, $data);
The right way is to add new keys to $data instead of reassigning it:
if (isset($_POST['tourID'])){
$data['tour_fk'] = $this->input->post('tourID');
}
if (isset($_POST['bookingNumber'])){
$data['booking_number'] = $this->input->post('bookingNumber');
}
You can achieve like this.Make a array of all not null keys with values.Not need to write isset() for each posted items.Just make use of foreach loop.And achieve your result.
$table = 'booking';
foreach($_POST as $key=>$value) {
if(isset($_POST[$key])) {
$data[$key]=$this->input->post($key);
}
}
//print_r($data);
Here is an example for you..
$array = array('tour_fk'=>1,'booking_number'=>11,'empty_field'=>NULL);
foreach($array as $key=>$value) {
if(isset($array[$key])) {
$data[$key]=$value;
}
}
print_r($data);
Output:
Array ( [tour_fk] => 1 [booking_number] => 11 )//without null values
I have a add-to-cart form to $_POST all data, and needed to store into a two dimensional array and assign to a session:
for example print_r($_POST) is:
Array("prod"=>"ZIU%3D","price"=>"68.00","alt-variation-1"=>"Red","alt-variation-2"=>"L","qty"=>"1")
to loop each $_POST:
foreach($_POST as $field => $value){
$f[] = $field;
$v[] = $value;
}
I looking for a way to assign above $f and $v into an array such as:
$new_product = array(array($f => $v));
and store in a session like:
$_SESSION['products'] = $new_product;
or any alternate way instead?
$_SESSION['products'][] = $_POST; would append the entire post array to the session products array, but you need to validate the data posted by the user.
A better way would be:
$data = $_POST;
// sanitise and validate $data here
$_SESSION['products'][] = $data;
An example for #HamzaZafeer:
foreach($_SESSION['products'] as $product){
echo $product['price'];
}
you can encode your array to JSON and store it in a session with:
$myJson = json_encode($_POST);
$_SESSION['myJson'] = serialize($myJson);
I am using a URL to fetch data stored/shown within URL. I get all the value of variable using $_REQUEST['v_name'] but if there is a array in URL how can i retrieve that value.
For Example:
WWW.example.com/rooms?&hid=213421&type=E
I got the value hid and type using
$hid=$_REQUEST['hid'];
but in URL like:
WWW.example.com/rooms?&rooms=2&rooms[0].adults=2&rooms[0].children=0&rooms[1].adults=2&rooms[1].children=0
how can i retrieve value of adults and children in each room.
please help.
Thanks in Advance
You could also try something like this, since most of your original $_REQUEST isn't really an array (because of the .s in between each key/value pair):
<?php
$original_string = rawurldecode($_SERVER["QUERY_STRING"]);
$original_string_split = preg_split('/&/', $original_string);
$rooms = array();
foreach ($original_string_split as $split_one) {
$splits_two[] = preg_split('/\./', $split_one);
}
foreach ($splits_two as $split_two) {
if (isset($split_two[0]) && isset($split_two[1])) {
$split_three = preg_split('/=/', $split_two[1]);
if (isset($split_three[0]) && isset($split_three[1])) {
$rooms[$split_two[0]][$split_three[0]] = $split_three[1];
}
}
}
// Print the output if you want:
print '<pre>' . print_r($rooms, 1) . '</pre>';
$valuse = $_GET;
foreach ($valuse as $key=>$value)
{
echo $key .'='. $value. '<br/>';
}