I have a problem with a collection.
When I do
echo ($homeTeam)
I get this result [{"Hometeam":3}]
But I want only 3 back.
Then I did this.
echo($homeTeam->Hometeam)
But then I get back error Property Home does not exist...
How to do this in a correct way?
Your input [{"Hometeam":3}] looks like json. First need to decode it:
$obj = json_decode($homeTeam);
Then to understand structure of data -- print it:
print_r($obj);
// output:
// Array
// (
// [0] => stdClass Object
// (
// [Hometeam] => 3
// )
//
// )
It is array of stdClass. So you can access to property Hometeam this way:
echo $obj[0]->Hometeam;
Related
I am having a problem trying to pass a variable from the controller to the view. I think I am doing a mistake somewhere, I can't find it since I am new to it.
My controller Method is :
public function paymentdetail($order_code)
{
$this->load->model('Payment_Model');
$paymentDetail = $this->Payment_Model->getpaymentdetail($order_code);
$orderDetail = $this->Payment_Model->getOrderDetail($order_code);
// echo print_r($paymentDetail);
$datas['content'] = $this->load->view('payment/paymentDetail',
array('paymentDetail' => $paymentDetail,
'orderDetail' => $orderDetail), true);
$this->load->view('layouts/main_template', $datas);
}
The following model function getpaymentdetail() returns an array (result_array()) and this is the one I am concerned about. If I can work this one out then I can also work with other model methods.
$this->Payment_Model->getpaymentdetail($order_code);
When I type <?php echo $paymentDetail['column_name']; ?> in view file(PaymentDetail.php) I get an error
Undefined index: column_name
Why do I get this error?
model function getpaymentdetail() returns an array (result_array())
those arrays are normally structured like this:
Array
(
[0] => Array
(
[ID] => 3120
[column_name] => col1
)
)
hence you cannot access column_name via <?php echo $paymentDetail['column_name']; ?> as this index doesn't exist in your array structure.
if you move one index deeper, then it will work: <?php echo $paymentDetail[0]['column_name']; ?>
Attention, if you expect more than 1 row to be returned, above will only access the first result (indexed 0) row! You'd need a foreach loop to get all results, see Generating Query Results - result arrays
I am trying to print a number from an array. But when i do this:
echo $results[0][0];
i get error.I tried to print the whole array using print_r() function
echo print_r($results);
Then i get this result:
Array ( [0] => stdClass Object ( [lastOrderProcessedNumber] => 109089875875875 ) ) 1
I just need to print "109089875875875" this number
How can i do that?
Thank you in advance
print_r() is a great way to inspect the contents of a variable. What it is showing you is that your variable holds an array whose first element (at index 0) is an object with an lastOrderProcessedNumber attribute. In PHP, you use -> to access object properties, so you should be able to retrieve the 109089875875875 value like this:
$results[0]->lastOrderProcessedNumber
As you can see from the print_r result, the indexed result is an object:
// $results is an 'Array' (access with square brackets)
Array
(
// Index 0 is an Object (access with arrow operator)
[0] => stdClass Object
(
[lastOrderProcessedNumber] => 109089875875875
)
)
This means you have to access the property through the arrow operator, like so:
$results[0]->lastOrderProcessedNumber
If you're expecting to only have one result, or to grab the first result from $results, you can make use of reset:
reset($results)->lastOrderProcessedNumber
If you see $results is an array of objects, that means that $results[0] is an object, not an array, so you can't access its attributes as an array but instead as an object. Like this:
$results[0]->lastOrderProcessedNumber;
How can I get the array from a object? I'm trying to get to the empty array so that I can validate on it's empty state.
$object = Illuminate\Database\Eloquent\Collection Object;
print_r($object);
if(empty($object->array)){
}
output
Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
)
)
The object is of the type Eloquent\Collection which means that it has access to a set of methods.
Eloquent\Collection | available methods
Collection provides a set of methods to validate the state of an array inside the Object. Two of which are interesting in the case described by me (OP):
IsEmpty
IsNotEmpty
Implementation
$website = $this->websitedb->findOneByUrl($this->url);
if($website->isNotEmpty()){
$uniqueId = rand() . $website[0]->id;
//save scan to database
$this->scan = $this->scandb->create($website[0]->id, $uniqueId);
$this->scandb->createModule($this->scan->id, $options);
}
Try like this:
if ($object->array === array()) {
echo 'this is explicitly an empty array!';
}
I got the following array (the array is retrieved through a db query). Now, my question is, how do I get a single element like e_domains from the array mentioned below:
stdClass Object
(
[id] => 1
[uni_origin] => Aachen
[e_domains] => rwth-aachen.de
)
I got the output shown above by running the following line of codes:
if ($results ) {
foreach ( $results as $result ){
echo'<pre>'; print_r($result) ;
}
}
First off, that's not an array, that's an object. Like it says: "stdClass Object".
Access object properties like this:
$object->property_name
In your case, it would be:
$result->e_domains
There are much more to learn on the subject, like static properties, visibility etc. In your case, the above example will work.
Read more about classes and objects in the manual: http://php.net/manual/en/language.oop5.basic.php
Try this:
$e_domains = mysql_result(mysql_query("SELECT id FROM games LIMIT 1"),0);
Hope it helpt.
Im having a hard time to read some data that i get from joomla 2.5. First i have created a module that stores data on DB as a json. So first i read from DB linke:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName(array('params')));
$query->from($db->quoteName('#__modules'));
$query->where($db->quoteName('module') . ' = '. $db->quote('mod_products'));
// 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).
$results = $db->loadObjectList();
and the result that i get as an array that contain objects, and each object has json data.
below is the arrray that i get from the query:
Array
(
[0] => stdClass Object
(
[params] => {
"product_name":"Sangiovese",
"product_subtitle":"Maremma Toscana DOC",
"product_category":"Red",
"isvisible":"1"
}
)
[1] => stdClass Object
(
[params] => {
"product_name":"Syrah",
"product_subtitle":"Maremma Toscana DOC",
"product_category":"Red",
"isvisible":"0",
}
)
[2] => stdClass Object
(
[params] => {
"product_name":"Merlot",
"product_subtitle":"Maremma Toscana DOC",
"product_category":"Red",
"isvisible":"0"
}
)
[3] => stdClass Object
(
[params] => {
"product_name":"Vermentino",
"product_subtitle":"Maremma Toscana DOC",
"product_category":"White",
"isvisible":"0"
}
)
);
So what i want to do is to access the data within each param for examle:
PS: Array name is $results.
,
EX: i want to access product_name of each of the products that are on this array, or subtitle and so on.
so i did something like this, but its not working, i know i am not doing it right, but i hope someone can help me, and i would really appruciate it.
foreach( $results as $result )
{
echo $result->prams->product_name;
}
Error that shows when this code gets executed:
Notice: Trying to get property of non-object in
I really would need some advice on this.
Thank you!
Every item in your list is an object:
[0] => stdClass Object
[1] => stdClass Object
And every object has a params property which is a string containing JSON data.
You need to use json_decode built-in function to convert JSON string to an object or array.
Try this approach:
$paramsDecoded = json_decode($result->params, true);
print $paramsDecoded['product_name'];
Hello and thanks to all who helped,
I managed to make it functional.
So im gonna post here all the code for everyone else that passes on the same waters and needs help.
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName(array('params')));
$query->from($db->quoteName('#__modules'));
$query->where($db->quoteName('module') . ' = '. $db->quote('mod_products') .' AND '. $db->quoteName('language') . ' <> '. $db->quote('en-GB'));
// 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).
$results = $db->loadObjectList();
$count = count($results);
What i did to make the functionality i needed:
for ($i=0; $i < $count; $i++) {
$json = $results[$i]->params;
$product = json_decode($json);
// code here, example
echo $product->product_subtitle;
}
So, yes. I needed to decode using json_decode first, before using it on other parts of the code.
Thanks for helping. Hope this posts helps other developers who same as me, will have difficulties working with the way Joomla manipulates objects stored in database.