Issue with default.php template when creating a module

For Joomla! 3.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Locked
Phil91
Joomla! Intern
Joomla! Intern
Posts: 54
Joined: Wed Jun 10, 2009 7:51 am

Issue with default.php template when creating a module

Post by Phil91 » Wed Jan 04, 2023 5:22 pm

First off, pls understand that I am a php newbie and not completely familiar with Joomla coding.

This said, I try to create a module to extract Joomshopping product names and quantities per month.
My SQL query is ok and data can be correctly displayed when I test data within my helper file.
However, if I try to "conventionnally" pass data to a default.php template file, nothing is displayed, meaning probably that I return data in a wrong way.

Here's my module entry point:
<?php
// No direct access defined('_JEXEC') or die;
// Include the syndicate functions only once

require_once dirname(__FILE__) . '/helper.php';

// I know I should not use this but I tried anyway :-)
global $prod;

$prod = modProduitsHelper::getProduits($params);
require JModuleHelper::getLayoutPath('mod_produits');
?>
Here's my helper file:
<?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 Jan,
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 here's, for now, how my default.php "template file" looks like:
<?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 />';
?>
It looks to me like I was doing ok but I'm wrong, so if anybody could help me out on this I'd appreciate it.

It's probably a trivial issue but I just don't see it.

Thanks

Philippe
Last edited by toivo on Thu Jan 05, 2023 7:57 am, edited 1 time in total.
Reason: mod note: removed word in CAPS from subject - please observe the forum rules!

User avatar
imanickam
Joomla! Master
Joomla! Master
Posts: 28206
Joined: Wed Aug 13, 2008 2:57 am
Location: Chennai, India

Re: NEWBIE: Issue with default.php template when creating a module

Post by imanickam » Thu Jan 05, 2023 7:16 am

I would suggest reviewing the document https://docs.joomla.org/Selecting_data_ ... List.28.29 to check how to reference the values of rows and columns in the result set created by LoadObjectList().
Ilagnayeru (MIG) Manickam | இளஞாயிறு மாணிக்கம்
Joomla! - Global Moderators Team | Joomla! Core - Tamil (தமிழ்) Translation Team Coordinator
Former Joomla! Translations Coordination Team Lead
Eegan - Support the poor and underprivileged

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 17445
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: Issue with default.php template when creating a module

Post by toivo » Fri Jan 06, 2023 3:41 am

Change the level of error reporting in Global Configuration to 'Maximum' and PHP will report a syntax error from the foreach loop. Curly braces are needed in the statement:

Code: Select all

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 />';
}
Toivo Talikka, Global Moderator

Phil91
Joomla! Intern
Joomla! Intern
Posts: 54
Joined: Wed Jun 10, 2009 7:51 am

Re: Issue with default.php template when creating a module

Post by Phil91 » Fri Jan 06, 2023 8:03 am

Thak you so much!
Will try this today. I thought that curly braces were only needed when more than one statement…
Will have to take a serious php course…

Phil91
Joomla! Intern
Joomla! Intern
Posts: 54
Joined: Wed Jun 10, 2009 7:51 am

Re: Issue with default.php template when creating a module

Post by Phil91 » Fri Jan 06, 2023 10:17 am

Hmmm,
Curly braces are not enough...Looks like $prod is empty which means that my function does not seem to return any value....
Setting error level to maximum gives me this warning:
Warning: Undefined variable $prod in xxxxxxxx/modules/mod_produits/tmpl/default.php on line 9
where line 9 is:
foreach ($prod as $pr) {
, so....

Phil91
Joomla! Intern
Joomla! Intern
Posts: 54
Joined: Wed Jun 10, 2009 7:51 am

Re: NEWBIE: Issue with default.php template when creating a module

Post by Phil91 » Fri Jan 06, 2023 2:44 pm

imanickam wrote:
Thu Jan 05, 2023 7:16 am
I would suggest reviewing the document https://docs.joomla.org/Selecting_data_ ... List.28.29 to check how to reference the values of rows and columns in the result set created by LoadObjectList().
Referencing the values of rows/columns is not the issue here since when I use the very same display code within the helper file, it works as expected...
It just has something to do with the way the resulting data are passed back to the default.php file as I can see it from error reporting:
Warning: Undefined variable $prod in xxxxxxxx/modules/mod_produits/tmpl/default.php on line 9
and yet, $prod is defined in the module entry point...


Locked

Return to “Joomla! 3.x Coding”