I have created a custom field named "sec1array" in my categories so that i can add an array, for example 1,2,3,4
I want to retrieve that array and output it in the loop, so I created this code.
$seconearray = array($cat_data['sec1array']);
$args = array(
'post__in' => $seconearray
);
However, it only seems to be outputting the first post in the array. Is it something to do with the way the comma is outputting?
If I print $seconearray it outputs correctly, example 1,2,3,4
What you are doing is storing a string value of "1,2,3,4" in your database which when you are trying to construct an array from it like array("1,2,3,4") you end up just assigning a single value to that new array. This is why it only contains a single value.
You need to store your value in a serializable format so it can be converted back to an array after you save it to the database. There are many ways to do this, I'm sure others will give more examples:
JSON encode it
json_encode(array(1,2,3,4)); // store this in your db
json_decode($cat_data['sec1array']); // outputs an array
Or, you can use PHP serialize
serialize(array(1,2,3,4)); // store this in your db
unserialize($cat_data['sec1array']); // outputs an array
If you want to keep your string, you can explode it:
explode(',', $cat_data['sec1array']); // outputs your array of 1,2,3,4.
Using any of these methods will work. Finally you'll end up with an example like:
$seconearray = explode(',', $cat_data['sec1array']);
$args = array(
'post__in' => $seconearray
);
Related
I would like to been appending objects to instagram value instead of overwriting them if there is a new object that isn't repeated.
So I have this piece of code:
if (is_array($api_return)) {
$api_return += ['last_updated' => time()];
$this->instagram->set_user_id($api_return['user_id']);
$this->instagram->set_username($api_return['username']);
$this->instagram->set_access_token($api_return['access_token']);
$this->instagram->set_access_token_expiration($api_return['access_token_expiration']);
$this->instagram->set_last_updated($api_return['last_updated']);
// Show our authenticated message.
$page_content .= $this->get_admin_notice_content('green',
'auth-finished', $this->instagram->get_username());
update_option('instagram', $api_return);
}
What this does, is it takes $api_return which is a array and groups it into an object and saves it inside the database as this:
We have option_name which is instagram and then the value is:
a:5:{s:8:"username";s:16:"saint";s:7:"user_id";i:17841404774727369;s:12:"access_token";s:141:"IGQVJVR1N*****";s:23:"access_token_expiration";i:1650688769;s:12:"last_updated";i:1645536110;}
What I'm attempting to do:
Now I want to be able to save multiple object where the instagram key doesn't get overwritten but each new api_return gets stored as a new object if it's not the same in an array.
Here is an example:
authenticated_users = [
{s:8:"username";s:16:"saint";s:7:"user_id";i:17841404774727369;s:12:"access_token";s:141:"IGQVJVR1N*****";s:23:"access_token_expiration";i:1650688769;s:12:"last_updated";i:1645536110;}
{s:8:"username";s:16:"test3";s:7:"user_id";i:17841404774727369;s:12:"access_token";s:141:"IGQVJVR1N*****";s:23:"access_token_expiration";i:1650688769;s:12:"last_updated";i:1645536110;}
];
What is the best approach on storing the option value as an array with multiple objects?
You have to first load the option an then manually merge the values:
$current = get_option("instagram", []);
$newData = [
$api_return,
];
// Merge array $current and $newData into single array so
// $api_return is appended to $current and the option is updated.
update_option("instagram", array_merge($current, $newData));
When you pass an associate array of data to views with Laravel, how does that associative array get converted into variables? I tried to look at the source code but I can't quite see how it's done.
// View
view('greetings', ['name' => 'Victoria', 'title' => 'Test']);
// Using the associative array data in the templates
<?php echo $name; ?>
<?php echo $title; ?>
https://laravel.com/docs/5.4/views#passing-data-to-views
Is PHP's extract function used?
Yes, it uses the extract function.
You can find this in the source code here Illuminate\View\Engines\PhpEngine.php.
After passing data to view you can display the above data as follow.
//this one will display the name
{{$greetings['name']}}
//this one will display the title
{{$greetings['title']}}
I'm trying to use a where method in laravel query. i have a string containing two values (separated by comma). I need to search with value that is after the comma. So i used explode php function to make an array . So I get an array containing two key-value pairs. i want to use 2nd value to search database. So i'm storing the second value in a variable and then passing that variable in the where method. But it's returning blank collection object
Here's the code
$vehicles_name_trim_ar = explode(',', Input::get('vehicles_name_trim'));
print_r of $vehicles_name_trim_ar is
Array
(
[0] => A3
[1] => 2.0T Premium Automatic
)
//storing both values in seperate variable
$model_name = $vehicles_name_trim_ar[0];
$model_trim = $vehicles_name_trim_ar[1];
$model = Model::where('model_trim', $model_trim)->get();
It's returning blank result. However if i'm proving static value, it return the result
$model = Model::where('model_trim', "2.0T Premium Automatic")->get();
What am i doing wrong?
You have a space at the start of the second value. try this:
$model_name = trim($vehicles_name_trim_ar[0]);
$model_trim = trim($vehicles_name_trim_ar[1]);
I have a usermeta set up for my users that saves favorite post to their profile. I am getting this usemeta(which keeps the post IDs in it). Once I get it, I have it in an one dimensional array. I want to display a list of their favorite posts. I have tried this:
$favorites //array of favorites, that has come from the databese
$query = new WP_Query( array( 'post__in' => array( 2, 5, 12, 14, 20 ) ) );
and it would work fine, if I hard coded the post IDs , but since it is an array I can't just pass in array such, it returns nothing.
$query = new WP_Query( array( 'post__in' => $favorites) );
it does not accept it, I also tried to implode the array in to a string as such:
$fav_list = implode("," , $favorites);
and i get this, which is exactly what I need as a string
"124,126,125,130,132,140,142", without the quotes. I would then use it as such:
$query = new WP_Query( array( 'post__in' => array($fav_list) ) );
But again it doesn't work, it returns nothing. Since the favorites list is being pulled from the usermeta and the user can change it, I can't hard code the list.
Can anyone help me? Is it even possible with WP_Query. Not sure why it is not taking the string or what I am doing wrong. I red through the Wordpress Documentation but haven't found a solution.
Thanks in advance.
Arrays don't get stored in the database as arrays. They get serialized. When you pull the array from the database you have to unserialize() it.
http://php.net/manual/en/function.unserialize.php
If you var_dump($favorites) right after it comes out of the database, you'll notice it's a weird-lookin' string and not an array. var_dump(unserialize($favorites)) will show you your original array.
I save my array of integers in a custom fields with the function:
update_post_meta($post->ID, "images", array(1, 2, 50));
How can I load this array now?
I try to use something like this, but without any luck:
global $post;
$custom = get_post_custom($post->ID);
$myarray = $custom["images"][0];
echo $custom["images"];
It returns something like a:3:{i:0;s:1:"1";i:1;s:1:"2";i:2;s:2:"50";}
May be I can parse this string to get an array from this?
May be I can parse this string to get an array from this?
This is a serialized array.
Use unserialize() to unpack it.