1

currently I have component module, which I'd like to add some data from another table.

This is my current helper.php code:

<?php

defined('_JEXEC') or die('Direct Access to this location is not allowed.'); 

class Modprop_lastHelper

{

function getItems($ShowFeatured,$OrderBy,$Order,$amountShow,$ShowPropertyID)

{        
switch($OrderBy) {
case 0 :
$OrderBy = 'p.id';
break;
case 1 :
$OrderBy = 'p.name';
break;
case 2 :
$OrderBy = 'p.price';
break;
case 3 :
$OrderBy = 'p.hits';
break;
case 4 :
$OrderBy = 'p.refresh_time';
break;
}
switch($Order) {
case 0 :
$Order = 'ASC';
break;
case 1 :
$Order = 'DESC';
break;
}   
if($ShowPropertyID){$where= 'WHERE p.id IN ('.$ShowPropertyID.')';}else{$where=    'WHERE p.published = 1 ';}
    if($ShowFeatured){$sqlFeatured= ' AND p.featured = 1';}
    $db = &JFactory::getDBO();     
    $query = 'SELECT p.*, i.name as imagename,c.name as name_category,t.name as name_type,cy.name as name_country,s.name as name_state,l.name as name_locality,'
            . ' CASE WHEN CHAR_LENGTH(p.alias) THEN CONCAT_WS(":", p.id, p.alias) ELSE p.id END as Pslug,'
    . ' CASE WHEN CHAR_LENGTH(c.alias) THEN CONCAT_WS(":", c.id, c.alias) ELSE c.id END as Cslug,'
    . ' CASE WHEN CHAR_LENGTH(cy.alias) THEN CONCAT_WS(":", cy.id, cy.alias) ELSE cy.id END as CYslug,'
    . ' CASE WHEN CHAR_LENGTH(s.alias) THEN CONCAT_WS(":", s.id, s.alias) ELSE s.id END as Sslug,'      
    . ' CASE WHEN CHAR_LENGTH(l.alias) THEN CONCAT_WS(":", l.id, l.alias) ELSE l.id END as Lslug, ' 
    . ' CASE WHEN CHAR_LENGTH(t.alias) THEN CONCAT_WS(":", t.id, t.alias) ELSE t.id END as Tslug '
            . ' FROM #__properties_products AS p '
            . ' LEFT JOIN #__properties_country AS cy ON cy.id = p.cyid '               
            . ' LEFT JOIN #__properties_state AS s ON s.id = p.sid '
            . ' LEFT JOIN #__properties_locality AS l ON l.id = p.lid '
            . ' LEFT JOIN #__properties_category AS c ON c.id = p.cid '
            . ' LEFT JOIN #__properties_type AS t ON t.id = p.type '
            . ' LEFT JOIN #__properties_images AS i ON i.parent = p.id '
            . $where                
            . $sqlFeatured
            . ' GROUP BY p.id ' 
            . ' ORDER BY '.$OrderBy.' '.$Order
            . ' LIMIT '.$amountShow;

    $db->setQuery($query);  
    $items = $db->loadObjectList();



   // $items = ($items = $db->loadObjectList())?$items:array(); 
//echo str_replace('#_','jos',$query);


    return $items;

}  
function NameCategory()

{   
$db     =& JFactory::getDBO();  
$query = 'SELECT * FROM #__properties_category WHERE published = 1 ORDER BY id';        
    $db->setQuery($query);        
    $Categories = $db->loadObjectList();

foreach ($Categories as $row) :
$Category[$row->id] = $row->name;
endforeach; 

return $Category;
}

function NameType()
{   
$db     =& JFactory::getDBO();  
$query = 'SELECT * FROM #__properties_type WHERE published = 1 ORDER BY name';      
    $db->setQuery($query);        
    $Types = $db->loadObjectList();

foreach ($Types as $row) :
$Type[$row->id] = $row->name;
endforeach; 


    return $Type;
}

function NameLocality()
{   
$db     =& JFactory::getDBO();  
$query = 'SELECT * FROM #__properties_locality WHERE published = 1 ORDER BY name';      
    $db->setQuery($query);        
    $Localities = $db->loadObjectList();

foreach ($Localities as $row) :
$Locality[$row->id] = $row->name;
endforeach; 


    return $Locality;
}

function getItemid( $TableName )
{
    $db =& JFactory::getDBO();  
    $query = 'SELECT id FROM #__menu' .
            ' WHERE LOWER( link ) = "index.php? option=com_properties&view='.$TableName.'&id=0"';               
    $db->setQuery( $query );
    $output = $db->loadResult();

    if(!$output){
    $query = 'SELECT id FROM #__menu' .
            ' WHERE LOWER( link ) = "index.php? option=com_properties&view=properties"';                
    $db->setQuery( $query );
    $output = $db->loadResult();
    }

    if(!$output){
    $query = 'SELECT id FROM #__menu' .
            ' WHERE LOWER( link ) = "index.php?option=com_properties&view=properties&cid=0&tid=0"';             
    $db->setQuery( $query );
    $output = $db->loadResult();
    }

    return $output;
}
} 

?>

I added additional function into helper.php to get data from another table properties_users:

    function Units()
{   

$db     =& JFactory::getDBO();  
$query = ' SELECT u.* '         
        . ' FROM #__properties_units as u '                 
        . ' WHERE u.published = 1 AND u.productid = p.id'           
        . ' order by u.ordering ';      
    $db->setQuery($query);   
    $Units = $db->loadObjectList();


return $Units;
}

When I try to show something from Units table nothing appears. I use the following code in my tmp view:

<?php echo $Units->title;?>

What is wrong with the code?

  • Nobody knows the answer? – user1810168 Nov 09 '12 at 13:19
  • Nobody know what error are you getting. What is $Units->title ? You need to call the function Units() somehow. – Valentin Despa Nov 09 '12 at 13:25
  • I don't get any error, nothing happens. It's a joomla 1.5 module. In helper.php I added a function, I thought that in views helper.php is called... How should I do it correctly? For example Units is a table, and title is a field in database properties_units. – user1810168 Nov 09 '12 at 19:04

1 Answers1

0

Here is what you have to do:

  • rename your function Units() in function getUnits()
  • rename $Units = $db->loadObjectList(); in $output = $db->loadObjectList();
  • rename return $Units; in return $output;

The 3 changes above are just to make your code consistent.

Now, in your module php file you can call your helper like this:

$units = Modprop_lastHelper::getUnits();

var_dump($units);

Make sure the helper file is included (for example like):

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


  1. Also I strongly suggest that you study the Joomla! Documentation regarding to Creating a simple module.

  2. Use var_dump() to debug you code. More, do some research on the topic "How do you debug PHP scripts". You won't find help anywhere is you just say "It's not working...".

Valentin Despa
  • 40,712
  • 18
  • 80
  • 106