I'm doing a module for joomla 3.2,I'm trying to pass a variable from one function to another in the same class, the purpose is to use it in a query.
How can I pass the "$result" variable in the query of the function due() ?
<?php
class modUno
{
public static function Uno()
{
$db = JFactory::getDBO();
$query = "SELECT id AS memTotal FROM #__users WHERE username = 'bruno';";
$db->setQuery($query);
$result = $db->query();
return $result->fetch_object()->memTotal;
}
public static function due()
{
$result->due();
$db = JFactory::getDBO();
$query = "SELECT avatar AS memTotal FROM #__comprofiler WHERE id = '$result';";
$db->setQuery($query);
$resulta = $db->query();
return $resulta->fetch_object()->memTotal;
}
}
?>
above is the "helper.php", then there's the "mod_chat.php", finally, there is "default.php"
mod_chat
enter code here
<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
require_once( dirname(__FILE__).'/helper.php' );
$risultato = modUno::uno();
$foto = modUno::due();
require JModuleHelper::getLayoutPath('mod_chat');
?>
default.php
enter code here
<?php defined( '_JEXEC' ) or die( 'Restricted access' ); ?>
<p> risultato: <?php echo $risultato; ?></p>
<p> foto:<img src="http://somesite.com/images/comprofiler/<?php echo $foto; ?>" /></p>
returns errors !
Firstly, this is a PHP question, not a Joomla question. I've found the PHP tutorial on codeacademy.com to be an excellent resource for learning PHP.
Looks like you are trying to get a user's Community Builder avatar. You should not be using direct database queries here. Use Community Builder data API to get what you need. Read more about it here - https://www.joomlapolis.com/support/tutorials/120-api-usage/18361-obtaining-field-values-through-getfields-api (see the end of this article, the code for getting avatar is there)
If you wish to absolutely query the DB, rather than 2 different functions, you should make just 1 and use a JOIN to get the user's avatar based on his Joomla username.
<?php
class modUno
{
public static function avatar()
{
$db = JFactory::getDBO();
$query = "SELECT cb.avatar FROM #__comprofiler AS cb, #__users AS u
WHERE u.id=cb.user_id AND u.username = 'bruno'";
$db->setQuery($query);
$result = $db->loadResult();
return $result;
//return $result->fetch_object()->memTotal;
}
}
@Lodder - You should never be directly calling execute() when using select queries with Joomla.
As a side note - you need to use ModUno::due() instead of $result->due()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.