Cannot echo individual values of an object in Joom

2019-09-01 07:32发布

问题:

following this tutorial here: http://docs.joomla.org/Selecting_data_using_JDatabase#Selecting_Records_from_a_Single_Table

I have created a function as follows:

function getItemForBid() {
        // Get a db connection.
        $db = JFactory::getDbo();

        // Create a new query object.
        $query = $db->getQuery(true);

        // Select item record matching the $orderID
        $query
            ->select($db->quoteName(array('id', 'created_by', 'itemtitle', 'deliverydestination', 'listprice')))
            ->from($db->quoteName('#__entrusters_items'))
            ->where('id = '.$_GET['orderID']);           
        // Reset the query using our newly populated query object.
        $db->setQuery($query);

        // Load the results as a list of stdClass objects (see later for more options on retrieving data).
$db->setQuery($query);
$bidItem = $db->loadObject();
//print_r($bidItem);
}

The print_r($bidItem); works and returns e.g.:

stdClass Object ( 
    [id] => 4 
    [created_by] => 216 
    [itemtitle] => Tennis Racket 
    [deliverydestination] => London 
    [listprice] => 5000 
)

however if I try to echo one of the values elsewhere on the page i.e. like this:

<input name="jform[item_id]" id="jform_item_id" value="<?php echo $bidItem->id; ?> " readonly="" type="text">

I get nothing. The manual says you can access the individual values by using:

$result->index // e.g. $result->email

Am I doing something very stupid?

回答1:

$bidItem is local to the function you created. Declare it as global to use it elsewhere on the page. Modify the last lines of your function like:

    global $bidItem;
    $bidItem = $db->loadObject();
}

Then before you use it in your HTML add:

<?php global $bidItem; ?>


回答2:

First of all, don't use $_GET with Joomla, you should use JInput like so:

$jinput = JFactory::getApplication()->input;
$orderID = $jinput->get('orderID', null, null);

As for you database query, try changing loadObject(); to loadObjectList();

Then also don't forget to change this:

->where('id = '.$_GET['orderID']);  

to this:

->where('id = '. $orderID);