user2310404
user2310404

Reputation: 1

calling function php joomla doesn't work

I wrote a simple php page integrated with joomla database. I wrote two functions and called them.

if the code within the functions is wrote as non-function (on the code itself) it works perfectly. BUT! when the code is in function and called from the page the entire screen turns white.

the code is simple... the first function get a user's id and return a sum of numbers in a table. the second function get a user's username and returns it id number. to make sure the functions work, I sent to them specific values (24 to the first function and 'admin' to the second one).

the code:

<?php
    defined('_JEXEC') or die('Restricted access');
    $items = $params->get('items', 10);
    $db =& JFactory::getDBO();

    function get_sum($uid)
    {
        $sum = 0;
        $query = "SELECT orderpayment_amount 
            FROM #__j2store_orders 
            WHERE user_id = '$uid' 
            ORDER BY id DESC";
        $db->setQuery( $query, 0 , $items );
        $rows = $db->loadObjectList();
        foreach($rows as $row)
        {
            $sum = $sum + $row->orderpayment_amount;
        }
        return "$sum";
    }

    function getTalId($u)
    { 
        $query = "SELECT id 
                  FROM #__users 
                  WHERE username = '$u' 
                  ORDER BY id DESC";
        $db->setQuery( $query, 0 , $items );
        $rows = $db->loadObjectList();
        foreach($rows as $row)
        {
            return $row->id;
        }
    }

    echo get_sum(42);
    echo getTalId('admin');
?>

I'm stuck for two days with this problem. I'd appreciate any help. thanks a-head.

Upvotes: 0

Views: 253

Answers (1)

piotr_cz
piotr_cz

Reputation: 9636

What you are experiencing is White screen of death.

Most probably you've turned error reporting off (which is recommended for production) t There's an error, but it's not being displayed on screen. You may either:

  • Change error reporting (System > Global Configuration > Server > Error Reporting: Development)
  • Check your server error logs.

As to why this is happening, I'd say that you don't have access to $db variable inside of a function (see this answer).

If the code would be inside of Jooomla MVC class model, $db = $this->getDbo() would do similar thing.

I encourage you to use classes instead of functions, you will get some extra stuff like autoloading

Upvotes: 1

Related Questions