Newbie in php, I am trying to create a module to extract orders from Joomla database and plot monthly results as a yearly curve. I am trying to store first the products in an array, followed by a monthly number of orders.
Database extraction is ok and I created a first array containing the product names and for each product, I'd like to create an array containing its 12 monthly quantities.
I created the product names array as below:
foreach ($products as $pr)
$results[] = $pr->name;
and the quantities array in the products loop as below:
foreach ($quantities as $quant)
$quant_array[] = $quant->number;
Now, how can I merge those two arrays (and also how to read data afterwards) so that the results looks like
prod_A-->1
1
2
...
prod_B-->2
3
5
...
prod_C-->3
4
2
...
Helper code is herebelow, obviously not finished:
public static function getProduits($params)
{
$user = JFactory::getUser();
$userId = (int) $user->get('id');
// Obtain a database connection
$db = JFactory::getDbo();
$query="select `name` as prodname, product_id from #__jshopping_products where product_publish=1 order by `name`";
// Prepare the query
$db->setQuery($query);
// Loading products.
$prod = $db->loadObjectList();
foreach ($prod as $pr)
{
$results[] = $pr->prodname;
$query2 = "SELECT count(product_quantity) as number, month(#__jshopping_orders.order_date) as month from #__jshopping_order_item join #__jshopping_orders on #__jshopping_orders.order_id=#__jshopping_order_item.order_id " .
"where #__jshopping_order_item.order_id in (select order_id FROM #__jshopping_orders where user_id=" . $userId . " and year(#__jshopping_orders.order_date)=2022 order by order_date desc) " .
"and product_id=" . $pr->product_id . " group by month(order_date) order by month(order_date) desc";
// Prepare the query
$db->setQuery($query2);
// Loading data
$amounts = $db->loadObjectList();
foreach ($amounts as $quant)
$quant_array[] = $quant->number;
}
**/// This is where I'd like to merge results within one array ($results)
// like $results['product']['count']**
return ($results);
}
}
$results being passed to the template file for output
It is probably a trivial task but as I said, I am a php beginner...
Thank you!
Revisiting this question, I succeeded creating a single query to achieve this. When I test results within the helper file with echo statements, it displays ok. However, when I try to get the values in the default.php output template file, I just get nothing, meaning that I probably have an issue with either the variable return or something else which I don't see, being a real php newbie...
Here's how I declared my module entry point:
<?php
// No direct access defined('_JEXEC') or die;
// Include the syndicate functions only once
require_once dirname(__FILE__) . '/helper.php';
global $prod;
$prod = modProduitsHelper::getProduits($params);
require JModuleHelper::getLayoutPath('mod_produits');
?>
I know I should not use a global declaration but I tried :-)
Helper file is ok and looks like this:
<?php
/**
* Helper class for module stats
*/
class modProduitsHelper
{
/**
*
* @param array $params An object containing the module parameters
*
* @access public
*/
public static function getProduits($params)
{
$user = JFactory::getUser();
$userId = (int) $user->get('id');
// Obtain a database connection
$db = JFactory::getDbo();
$query="SELECT
product_name as nom,
sum(if(month(order_date) = 1, product_quantity, 0)) as Janv,
sum(if(month(order_date) = 2, product_quantity, 0)) as Feb,
sum(if(month(order_date) = 3, product_quantity, 0)) as Mar,
sum(if(month(order_date) = 4, product_quantity, 0)) as Apr,
sum(if(month(order_date) = 5, product_quantity, 0)) as May,
sum(if(month(order_date) = 6, product_quantity, 0)) as Jun,
sum(if(month(order_date) = 7, product_quantity, 0)) as Jul,
sum(if(month(order_date) = 8, product_quantity, 0)) as Aug,
sum(if(month(order_date) = 9, product_quantity, 0)) as Sep,
sum(if(month(order_date) = 10, product_quantity, 0)) as Oct,
sum(if(month(order_date) = 11, product_quantity, 0)) as Nov,
sum(if(month(order_date) = 12, product_quantity, 0)) as `Dec`
FROM vpkbb_jshopping_order_item join vpkbb_jshopping_orders on vpkbb_jshopping_orders.order_id=vpkbb_jshopping_order_item.order_id
WHERE user_id = 398
AND year(order_date) = 2022
group by product_name
order by product_name;";
// Prepare the query
$db->setQuery($query);
// Chargement des produits.
$prod = $db->loadObjectList();
return ($prod);
}
}?>
and, finally, my default.php template file looks for now just like this:
<?php
// No direct access
defined('_JEXEC') or die;
foreach ($prod as $pr)
echo $pr->nom . ' ' . $pr->Jan . ' ' . $pr->Feb . ' ' . $pr->Mar . ' ' . $pr->Apr . ' ' . $pr->May . ' ' . $pr->Jun . ' ' . $pr->Jul . ' ' . $pr->Aug . ' ' . $pr->Sep . ' ' . $pr->Oct . ' ' . $pr->Nov . ' ' . $pr->Dec . '<br />';?>
I thought I had my Xmas present, but it looks like I don't, so if anybody could point me to what I do wrong, this would save this miserable attempt to achieve my goal.
Thx