Jump to content

Reading from stdClass Object within an array


saucypony

Recommended Posts

Hello everyone! I have very limited PHP experience, and I'm assuming this is relatively trivial, but I haven't been able to google anything that's helped me yet.

 

Just some background. I'm trying to write a custom module for Joomla that pulls data from the database, and displays it. Simple. This is what I'm working with so far:

 

<?php

//don't allow other scripts to grab and execute our file

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

$db =& JFactory::getDBO();

 

 

// select the prices from the database, indexed by their product code

$query = "SELECT id, unitprice, shortdescription FROM #_sc_products`";

$db->setQuery( $query );

$data = $db->loadObjectList();

 

echo "<pre>";

print_r($data);

echo "</pre>";

 

?>

 

It returns to me this...

 

Array

(

    [0] => stdClass Object

        (

            [id] => 1

            [unitprice] => 90

            [shortdescription] => Great for hiking.

        )

 

    [1] => stdClass Object

        (

            [id] => 2

            [unitprice] => 75

            [shortdescription] => Don't forget a flashlight.

        )

 

    [2] => stdClass Object

        (

            [id] => 3

            [unitprice] => 65

            [shortdescription] => Come on PHP

        )

)

 

What I'm not able to figure out is the code to output the unitprice given an id of 3! I certainly still have a lot to learn in the ways of PHP haha...

Link to comment
Share on other sites

Arrays are 0-indexed, which means your first object with an id of 1 is array element 0.  If you're merely trying to get the unitprice based on the id, try:

 

echo $array[$id - 1]->unitprice;

 

Of course, it's best to use an accessor method to obtain an object's data members' values rather than leaving them publicly accessible.

Link to comment
Share on other sites

Right, I guess I didn't explain it quite right. The print_r($data) was just for me to see the structure of the array while in debug mode in an attempt to understand how it was organized.

 

Thanks though! That worked just about perfectly! I had to modify it slightly, but that's exactly what I needed, thank you!

 

echo $data[$id=1]->unitprice;

 

 

While I'm at it, I haven't been able to find an explanation for what the "->" operator does. Care to explain or link me up? Google won't search for two strange characters like that  :)

Link to comment
Share on other sites

if you are familiar with OOP, the -> operator in PHP is similar to, say, the "." operator in languages like C++ and java. It basically is a way to access member functions and variables in an abject. for example say I have a simple class

class foo {
public $variable = "Hello ";
private $variable2="world!";

function bar(){
echo $this->variable2;
}
}

 

Note how we used $this->variable2. that means access the variable "variable2" in "this" class. If we were to instantiate the class in a program, we would access the functions and public variable like so

$foo = new foo();
echo $foo->variable;
$foo->bar();

that would echo "Hello world!"

 

this is very similar to, say, javascript, where you can access an objects function like so

array.split();

 

but instead of "." we use ->. The dot character in PHP is used for string concatenation.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.