bygora
July 4, 2022, 12:00am
#1
Hi,
I'm currently using the advanced-addon tutorial as reference to develop an add-on that basically displays the viewed categories but, I'm having an issue with array_keys().
This is the index.post.php file:
if (!defined(‘BOOTSTRAP’)) {
die(‘Access denied’);
}
use Tygh\Registry;
$viewed_categories = db_get_array(‘SELECT * FROM ?:advanced_addon_data’);
if (!empty($viewed_categories)) {
foreach ($viewed_categories as $key => $category_data) {
// fn_print_r(array_keys($category_data[‘categories’]));
$category_data[‘user_name’] = fn_get_user_name($category_data[‘user_id’]);
$category_data[‘categories’] = unserialize($category_data[‘categories’]);
$category_data[‘categories’] = fn_get_category_name(array_keys($category_data[‘categories’]));
$viewed_categories[$key] = $category_data;
}
Registry::get('view')->assign('viewed_categories', $viewed_categories);
}
I'm getting this error when I activate the addon:
PHP Warning
Message
array_keys() expects parameter 1 to be array, string given
Error at
C:\xampp\htdocs\marketplace\app\addons\advanced_addon\controllers\backend\index.post.php, line: 29
Did you guys faced a similar error ?
Thanks.
Hello
Please check that $category_data [ 'categories' ] is array.
print "
";
print_r( $category_data [ 'categories' ]);
print "
";
Best regards
Robert
bygora
July 5, 2022, 12:00am
#3
Hello
Please check that $category_data [ 'categories' ] is array.
print "
";
print_r( $category_data [ 'categories' ]);
print "
";
Best regards
Robert
Hi,
It's not an array, I'm getting a string, I used the explosion function to separate the categories IDs to fix this.
if (!empty($viewed_categories)) {
foreach ($viewed_categories as $key => $category_data) {
// fn_print_r($category_data['categories']);
$category_data['user_name'] = fn_get_user_name($category_data['user_id']);
$category_data['categories'] = unserialize($category_data['categories']);
// fn_print_r(array_keys(explode(" ",$category_data['categories'])));
$category_data['categories'] = fn_get_category_name($category_id = array_keys(explode(" ", $category_data['categories'])));
$viewed_categories[$key] = $category_data;
// fn_print_r($viewed_categories);
}
Registry::get('view')->assign('viewed_categories', custom_sort($viewed_categories));
}
I also sorted the users and counted the categories numbers.
Could be done with better ways...