I have great problem because i need fiz this and i don´t know how i can do it
I have simple line of Data separated by "," and this data are differents, all data in the same line as this :
$data_1="orange";
$data_2="yellow";
$data_3="".$_POST['options']."";
$data_4="".$_POST['street']."";
$data_5="Jhon";
$data_total="".$data_1.",".$data_2.",".$data_3.",".$data_4.",".$data_5."";
How you can see all these Data go inside the same line $data_total, and easy for process using explode for get different values, my only problem it´s with values i get from $_POST when are Arrays, for example from fields as multiple select with 3 , 4 or more values send from this field
Yes i can use explode, and show results, but when for example POST it´s array of values, only get the word Array.
Try process result :
$explore=explode(",",$data_total);
$dt=0;
foreach($explore as $explores)
{
print "DATA ".$dt." - ".$explores."<br>";
/// But when loop for example number 3 only show word Array the question how show data inside Array from POST
$dt++;
}
My question it´s, how can show or extract the data from POST fields with array values, inside loop when explode line $data_total
I want get results as this for example:
orange
yellow
Result array content from $_POST['options']
Result simple content $_POST['street']
Jhon
If I understand correctly $_POST['options'] is an array and you want to save it to $data_3 to be used later but since it's an array it just prints "Array".
So you need to somehow convert array to string e.g.
$data_3 = implode(' ', $_POST['options']);
This will join all options with space between them and save it to $data_3
Related
I have question about explode, because i have string with differents values separated by ",", and some values take from $_POST Array, for example multiselect fields, but i don´t know if i writte right this in PHP
Take Values- in this case $_POST['opt']['selection'] it´s array :
$values="house,car,colors,".$_POST['opt']['selection']."";
As you can see in the other point in the string "$values", the different values are separated by "," and now i go to explode these values
$exp_values=explode(",",$values);
Now i try show all values :
foreach($exp_values as $exp_values_end)
{
if(is_array($exp_values_end))
{
/// In this point must show values from array for process, and aplicate other time foreach, but don´t show nothing
}
else
{
$exp_values_end
}
}
I try different ways for get values from arrays when use with explode, but finally get nothing, by this, my questin it´s about, if it´s possible do this and how i can use explode when some values really are arrays, how in the case i show in this post, thank´s in advanced
Thank´s
First you need to transform your string into array. You can create a function convertStrToArray() who's retuning arguments as array. With ...$listString notation, you can add all your string variables.
I havn't tested it but you can try and debug :)
function convertStrToArray(...$listString)
{
return func_get_args();
}
$list = convertStrToArray('house', 'car', 'colors', $_POST['opt']['selection']);
In my database I have a table named posts. In it, the columns are:
Postid - 1
PostBody - my first post
Likers - 1,2,3,4,5,
Now what am trying to do is, in the likers field, am trying to pull the individual number without pulling all the data on the field. So if I want to retrieve likers No.3, I want the output to be 3 when I echo it out.
I have tried FIND_IN_SET(), and strstr(). All I get is the whole array, 1-5, being echoed. And because of that, I cannot match it with other variables(I.e. $var = 3;) which have the matching number.
Is there a way of doing this?
Thanx
If you really want to do it this way (read my comment first), what you can do is retrieve the comma-seperated data, explode it into a php array, and then use the array_search built-in function:
$likers_attribute_data = '1,2,3,4,5,';
$likers_array = explode(',', $likers_attribute_data);
// check if user 3 is a liker:
if (array_search('3', $likers_array) {
// he has :)
} else {
// he hasn't :(
}
Again, this is a very sloppy and inefficient way of doing business. Please just go with the solution in my comment!
I´m still new in php and I´m having trouble in sending a form vars through POST.
I use a jquery to dynamically add new fields with the same name plus a auto increment ID in a form.
In the _POST I need to have a loop to get those vars, but how am I going to get the vars count for the loop?
I have something like this:
$ID1_name = $_POST["ID1_name"];
$ID2_name = $_POST["ID2_name"];
$ID3_name = $_POST["ID3_name"];
$IDX_name = $_POST["IDX_name"];
$ID1_place = $_POST["ID1_place"];
$ID2_place = $_POST["ID2_place"];
$ID3_place = $_POST["ID3_place"];
$IDX_place = $_POST["IDX_place"];
There can be unlimited variables with same name with and AI, and I have around 10 vars like that.
How can I count by the var partial name?
or
Is there a better way to get those _POST? I´m using a For loop.
I was using the same name and counting in the array. It worked, but I have 3 fields with radio and 2 with checkbox, and in the array all the vars of the same name merge. So the array for food have entries from all food checkbox from the field. Does that make sense?
So in one $ID1_place = string I might have $ID1_check = array
Thanks for the help. I´know there is a easy way, but I think I´m searching the wrong way since I found nothing.
Thanks a lot.
regards
There's two ways I can think for this. Either you can pass through a hidden element with the number of elements with your name, or you can use the preg_grep function to match all POST vars matching a regex pattern:
// Search keys:
$ID_place_keys = preg_grep('/ID[0-9]+_place/', array_keys($_POST));
// Search values:
$ID_places = preg_grep('/ID[0-9]+_place/', $_POST);
// Get array of values matching keys:
$ID_places = array_intersect_key($_POST, array_flip(preg_grep('/ID[0-9]+_place/', array_keys($_POST))));
// Gets a bit messy with all the array functions...
And here's a URL to a simple working example:
http://ideone.com/a9AhL6
Have you considered instead of sending a lot of post variables, to send only two; where all the fields are json encoded?
Example: names: ["ID1", "ID2", "ID3"] and places: ["ID1", "ID2", "ID3"]. To get the AJAX parameters like this you can stringify the arrays you have using JSON.stringify(). Ref: JSON.stringify()
Then on the back-end just do $names = json_decode($_POST["names"]) and $places = json_decode($_POST["places"])
This way you have an array with the id-s of the names and places. You can count them, loop over them, do whatever you need to.
I am passing a jquery array called 'selected' full of ids along with the opening of an ajax modal.
$('#dtDelete').on('click', function () {
$('#modal-ajax').load('/modals/m__delete.php?selected='+selected);
$('#modal-ajax').modal('show');
});
On the modal php page count($_GET['selected']); always returns 1 no matter what. I am trying to get an actual count of the number of values in the array. Turns out this is because the array is a string as noted below.
var_dump($_GET['selected']); returns something along the lines of string(69) "187419,187420,187413,187414,187415,187416,187417,187418,187421,187422" which is something I am not accustomed to (sort of new to jquery). I need to do processing in php using foreach on this array. Can I 'convert' this to a 'normal' php array so count() will work as expected and I can process it normally in php?
Lastly, this array may or may not be extremely large at times. The jquery function above opens an ajax modal (I am using the modal as a confirmation box for the user whether they really want to delete the entries in the selected array) and I know the $_GET method has limits to the amount of data it can pass. I can't do $_POST because this is a modal and I need to load it then show it... how else can I pass this data?
$_GET['selected']
returns the STRING after the attribute 'selected', and count(string) is 1 not matter what ( it's not a multi-dimension array to be greater than 1).
As for the comma separated string example you gave, you may use the following :
$selected = $_GET['selected'];
//test wether the string has contents
if(strlen($selected)!=0) {
$selected_array = explode(',',$selected); //this is the new array that you want
}
else {
//the string is empty
}
There are many string functions you may check at : http://www.php.net/manual/en/ref.strings.php
Look at the following code:
foreach($_SESSION['cart'] as $r => $stringrow) {
$rowarray=explode("#",$stringrow);
}
$_SESSION['cart'] contains the row of concatenated values the separator is "#". Which I got from the previous page (as you can see they are inserted in a session.) I am exploding $stringrow to get an array called $rowarray. Everything is fine till here. The problem is this $rowarray[] contains two array elements, each of which contain concatenated imageurls separated by "#". The concatenated image urls are in $rowarray[11] and $rowarray[12]. I am again exploding $rowarray[11] and $rowarray[12]. trying to access each imageurl. But it's not happening. till now I have used innumerable echos to see the content of the imageurl array but every time it's showing 'Array'. as the value.
To view the contents of arrays, try using either var_dump($array) or print_r($array). They'll show you the type and contents of whatever variable you feed to them. It makes debugging oddly nested arrays much easier.