I'm saving the values from a checkbox in an array to the database, but in the database the field contains strange characters like: s:44:"a:2:{i:0;s:8:"0-4 years";i:1;s:8:"10th group";}";
I only need the parts 0-4 years and 10th group. How can I prevent the characters get it the way I want?
This is part of the form:
<input type="checkbox" name="wmw_age[]" value="0-4 years">
<input type="checkbox" name="wmw_age[]" value="10th group">
This is the way I get it to the database after submit:
$data=serialize($_POST['wmw_age']);
update_post_meta($post_id, 'wmw_age', $data);
If both fields are checked, this is the result in the database:
s:44:"a:2:{i:0;s:8:"0-4 years";i:1;s:8:"10th group";}";
Is it possible to only get this in the database: 0-4 years, 10th group
I hope someone can help me with this!
Thanks.
This should do the trick:
$data = join(', ', $_POST['wmw_age']);
You do not have to serialize the array, it can be concatenated with the join function, which will turn it into a string by joining all values with a given string(glue). ( ', ' in the above example )
The result will be: 0-4 years, 10th group in case both checkboxes are checked, 10th group or 0-4 years in case only one checkbox is checked and an empty string in case none is checked.
Thanks to #RiggsFolly for pointing out, that the serialized string is serialized again by update_post_meta, so a deserialization is unnecessary.
Related
I have two rows (longitude and latitude) in my MySQL table. I want to get the two rows into a 2-dimensional array such that when the values (longitude and latitude) change in the database the values will be added to the array. Not that the values will be updated.
while($mRow = $resultMarker->fetch_assoc()){
$myArrayForMarkers [] = $mRow['latitude'];
$myArrayForMarkers [] = $mRow['longitude'];
}
print_r($myArrayForMarkers);
The issue I have in the code above is that instead of the new values to be added from the database to the array, the values in the array are being updated. Any help would be appreciated.
As I said in the comments, the wording of your question makes little sens. For example, I don't undurstand what you mean by 'Not that the values will be updated.' nor how your array being 'updated' is different to 'the values will be added to the array'.
Please clarify your question, especially if my answer doesn't solve it so that those who will eventually come here while looking after a solution can understand if your issue is the same than theirs.
I get that you try to craft a 2-dimension array.
The code you provided produces 1-dimension arrays.
If you want to store your datas in a 2-dimension array, I only see two solutions :
Either your main array has two subarrays, each containing one type of data :
while ($mRow = $resultMarker->fetch_assoc()) {
$myArrayForMarkers['latitude'][] = $mRow['latitude'];
$myArrayForMarkers['longitude'][] = $mRow['longitude'];
}
or en
each entry of your array itself contains both information :
while ($mRow = $resultMarker->fetch_assoc()) {
$myArrayForMarkers[] = array('latitude' => $mRow['latitude'], 'longitude' => $mRow['longitude']);
}
That's the most I can do right now, since I don't understand the rest of your question.
I have a form that takes in data and writes to a JSON form in PHP.
I needed to submit an array as a numeric input but it keeps giving me a string. Is it possible to enable the form to submit as an array via text input box?
Form example:
<input type="text" name="arraytobepushed[]" placeholder="EG: 1000,2000,3000" />
The output is:
{
"obj": [{
"arraytobepushed": ["1000,2000,3000"]
}]
}
You could turn the text into an array by using explode() So you would have something like this:
<?PHP
$myArray = explode(',', $_POST['arraytobepushed[]']);
?>
The explode() function splits everything separated by the first argument (in this case a comma) you pass and puts it into an array.
So if your inputted was 1000, 2000, 3000 your $myArray would look like:
index 0 = "1000" ($myArray[0])
index 1 = "2000" ($myArray[1])
index 2 = "3000" ($myArray[2])
Keep in mind that the values are still strings, not integers. If you want to make them integers you can do this:
$myArray = array_map('intval', explode(',', $_POST['arraytobepushed[]']));
This makes all your elements into integers like so:
index 0 = 1000 ($myArray[0])
index 1 = 2000 ($myArray[1])
index 2 = 3000 ($myArray[2])
No. Forms submit text.
PHP special cases fields with [] in the name as fields to be expressed in an array. It has no special case feature to treat a field as a number instead of a string. You need to convert the data explicitly.
This is silly question, but I've tried on lot time and no success.
I have an array based my database record from query select ike this
User1User2User3
So, I use implode to make them separated.
In my case, I use this for auto completed on jquery focusout.
In html form input, it must be lloked like this :
<input name='userdata' data-source='["User1", "User2", "User3"]' />
This is the implode :
$data =implode(' ", ', $users);
echo $data;
the input, looked like this : User1" , User2"
For the help, it so appreciated.
Try this..
$data ='" '.implode(' ", ', $users).'"';
this is not a solution but it can be work form now.
you have to change you query which give result like User1, User2, User3 So from that you can do easily implode.
I am trying to echo out an array within a while statement, however, the results also include some columns from the database which may or may not be null.
Is it possible to echo all array where value is not null? I can't edit the mysql query as I need this to be dynamic as some of the columns may be used by another user.
My table contains around 20 columns, some are populated some aren't.
My code:
PHP
<?php
while($row = $mysqli_fetch_array($uploads)){
print_r(!is_null($row));
}
?>
Expected Output
Firstname: John
Lastname: Test
Age: 15
Current Output
1
Any help would be great, apologies for the lack of code.
Many possible ways. Here's one example: You could filter out the null elements and then print $row
while($row = $mysqli_fetch_array($uploads)) {
$row = array_filter($row); // 1
// or 2: $row = array_filter($row, 'strlen');
echo join(', ', $row), "\r\n";
}
this will type-cast each element to boolean and any keep those elements that evaluate to truthy. This might filter more elements than you'd like, see Converting to boolean
but you can tell aray_filter how to decide whether an element is in or out. In this case the return value of the function you give as the second parameter with be cast to boolean and checked. Using strlen an empy string and/or anything that get's cast to an empty string (like NULL) will result in 0->false->filtered out.
I have a "recruiter" table in my database which has different attributes and one of them is "Professions". "Professions" is a serialized array which I get from a multiple select form. And this works fine.
When I unserialize this attribute nothing is printed - no error, no text.
This is a code I was testing serialization with:
$sql = 'SELECT Company_name, Status, Size, Professions, Seniority_levels, Sector, Website, Location FROM Recruiter';
$query = mysql_query($sql, $con);
while($result = mysql_fetch_array($query, MYSQL_BOTH)){
$recruiters[] = array($result[0], $result[1], $result[2], $result[3], $result[4], $result[5], $result[6], $result[7]);
}
foreach($recruiters AS $recruiter){
$test = unserialize($recruiter[3]);
echo $test[0].'<br>';
}
So basically $test[0] prints nothing although the new lines are printed. Please help!
try printing the $test array and the $recruiters and the $recruiter arrays. See if the result is fine before the unserialisation of the data. If the query returns any data. Also try the while loop with mysql_fetch_assoc. Let me know of the results and if this solves the problem
test = unserialize($recruiter[3]); should become test = unserialize($recruiter[5]); since the sector field is the sixth column .
However what if somewhere in the future you might need to select rows where sectors equal smth ? serialize whont help you then so i suggest you have a look at a different implementation for the sector filed witch is called bitwize http://www.litfuel.net/tutorials/bitwise.htm
Edit
Asuming you hit the right column and the column contains a:1:{i:0;s:27: a:1:{i:0;s:27: a:38:{i:0;s:27: a:9:{i:0;s:39:, it looks like the serialized array is not fully saved in you're db, it's only part of it . So the unserialize function whont return you an array . Have a look at the length of the mysql field i assume you've set it smaller than you need so you're data is trimmed on insert/update .
Edit
a:1:{i:0;s:27: you're still missing the rest of the serialized array . s:27: means a string is following containint 27 characters, and you're serialized array stops there when it should look like
a:1:{i:0;s:27:"123456789012345678901234567";}
( a:1 stands for an array containing 1 value with it's content between {}, i:0; is the array key 0, s:27:""; stands for a string containing 27 characters as the value for the i:0 key ) .