saucypony Posted September 10, 2009 Share Posted September 10, 2009 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... Quote Link to comment https://forums.phpfreaks.com/topic/173827-reading-from-stdclass-object-within-an-array/ Share on other sites More sharing options...
KevinM1 Posted September 10, 2009 Share Posted September 10, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/173827-reading-from-stdclass-object-within-an-array/#findComment-916267 Share on other sites More sharing options...
saucypony Posted September 10, 2009 Author Share Posted September 10, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/173827-reading-from-stdclass-object-within-an-array/#findComment-916273 Share on other sites More sharing options...
mikesta707 Posted September 10, 2009 Share Posted September 10, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/173827-reading-from-stdclass-object-within-an-array/#findComment-916279 Share on other sites More sharing options...
saucypony Posted September 10, 2009 Author Share Posted September 10, 2009 Great explanation, thank you very much for taking the time to explain that! Quote Link to comment https://forums.phpfreaks.com/topic/173827-reading-from-stdclass-object-within-an-array/#findComment-916285 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.