Page 1 of 1

Function which returns an array appears to return empty..?

Posted: Wed Aug 18, 2010 9:57 pm
by adamjoiner
Hey guys!

I just typed this code up from scratch and, as I am not perfect with php syntax yet, I KNEW I was going to have some errors.. Can you tell me if anything is wrong with this??

First of all, I am working with Joomla 1.5 and I have created a new module.

"helper.php" contains the following class and function:

Code: Select all

class modComHelper
{
   function getComData($params, $user)
   {
      	$db =& JFactory::getDBO();
	$userid = $user->get('id');
		
	$query = "SELECT * FROM `ext_usertable` WHERE `refid` = '".$userid."'";
	$db->setQuery($query);
	$data['totalref'] = mysql_num_rows($db->query()); // This line counts the number of rows that contain instructors id as refid
		
	$query = "SELECT `totalmade` FROM `ext_usertable` WHERE `id` = '".$userid."'";
	$db->setQuery($query);
	$totalmade = mysql_fetch_array($db->query());
	$data['totalmade'] = $totalmade['totalmade'];
		
	$query = "SELECT `totalcol` FROM `ext_usertable` WHERE `id` = '".$userid."'";
	$db->setQuery($query);
	$totalcol = mysql_fetch_array($db->query());
	$data['totalcol'] = $totalcol['totalcol'];
		
	$query = "SELECT `ppemail` FROM `ext_usertable` WHERE `id` = '".$userid."'";
	$db->setQuery($query);
	$ppemail = mysql_fetch_array($db->query());
	$data['ppemail'] = $ppemail['ppemail'];
		
	$query = "SELECT `accountbal` FROM `ext_usertable` WHERE `id` = '".$userid."'";
	$db->setQuery($query);
	$accountbal = mysql_fetch_array($db->query());
	$data['accountbal'] = $accountbal['accountbal'];

	return $data;
   }
}
Then, in my "mod_mynewmodule.php", I include the "helper.php" file and call the function, assigning it to a variable:

Code: Select all

require_once( dirname(__FILE__).DS.'helper.php' );
require( JModuleHelper::getLayoutPath( 'mod_mynewmodule' ) );

$user =& JFactory::getUser();
$comdata = modComHelper::getComData($params, $user);
Now, for the part I wasn't completely syntax-confident about, I echo the variable contents onto my page:

Code: Select all

<?php echo $comdata['totalref']; ?>

Is there anything that stands out as being a problem?? I have checked the php manual for all of my code and I am pretty sure this is the proper way to do this. However, my echo statement echos nothing...

Re: Function which returns an array appears to return empty.

Posted: Thu Aug 19, 2010 1:29 am
by cpetercarter
Nothing wrong with your syntax. The problem must be either that the page script does not have access to $comdata, or that $comdata is not an array, or that it does not contain an offset 'totalref', or that 'totalref' is set but has no value. Call

Code: Select all

print_r($comdata);
to find out what $comdata actually contains.

Re: Function which returns an array appears to return empty.

Posted: Thu Aug 19, 2010 10:53 am
by adamjoiner
It returns blank. As far as the array and its offsets, do I need to declare these first? This is the first time this array is being used. Nothing was ever defined...