Php not displaying a simple object variable - php

I have this object :
var_dump ($filtres) ;
Result:
object(stdClass)[2]
public 'filtres' =>
object(stdClass)[3]
public 'table' => string 'crm_comptes' (length=11)
I'm simply trying to echo it, it doesn't work :
echo $filtres->table;
It stays BLANK, I've tried plenty of things, like converting it to an array, all of that, it still doesn't want to display 'crm_comptes'
I've tried this :
echo $filtres['table'];
It still displays nothing.
Did I do something wrong?

As I have seen you output result, it seems your result $filtres is an object of an object for table. You should get it as:
$filtres = $filtres->filtres;
echo $filtres->table;

Related

Can't pass variable from controller to view in CodeIgniter

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

Accessing nested objects within objects in PHP not working

In PHP, I'm requesting data from my QuickBooks Online account using their Query functions.
I'm iterating through each customer object that is returned in a standard foreach($customer as $x) code block.
Using var_dump, I can tell each object has the following format:
object(QuickBooksOnline\API\Data\IPPCustomer) [34]
public 'Taxable' => string 'true' (length=4)
public 'BillAddr' =>
object(QuickBooksOnline\API\Data\IPPCustomer) [78]
public 'Id' => string '7509' (length=4)
public 'Line1' => '1234 Irrelevant Dr.' (length=19)
In PHP, if I simply try to access $x->Taxable, it works no problem, but any sub-object I try to access returns an error: "trying to get property of non-object on line X"
So:
echo $x->Taxable;
gives me the taxable status, but
echo $x->BillAddr->Line1;
gives me an error.
These are public classes, so shouldn't I be able to call them like this?
Found the problem and I have no idea why this is required, but here's how I got it to work, adding curly braces:
echo "{$x->BillAddr->Line1}"; instead of just echo "$x->BillAddr->Line1";
Same rule applies to assignment as well, apparently:
$line1 = "{$x->BillAddr->Line1}";
Thanks for your help, guys!

How to work with not standart named array in object

I don't know how to work with such object, i need to get first and second one status value, tryed to convert it to json, but it gives me nothing. I just don't get it how to open array with such "_data:MailWizzApi_Params:private" name.
Source:
// SEARCH BY EMAIL
$response = $endpoint->emailSearch($myConfig["LIST-UNIQUE-ID"], $_GET["email"]);
// DISPLAY RESPONSE
echo '<hr /><pre>';
print_r($response->body);
echo '</pre>';
I receive such answer
MailWizzApi_Params Object
(
[_data:MailWizzApi_Params:private] => Array
(
[status] => success
[data] => Array
(
[subscriber_uid] => an837jdexga45
[status] => unsubscribed
)
)
[_readOnly:MailWizzApi_Params:private] =>
)
In this case, you can't.
Because it's private field.
For public fields with "incorrect" names you can use snippet:
$name = '}|{';
$obj->$name;
So, let see to your property: [_data:MailWizzApi_Params:private].
It is private field of instance of MailWizzApi_Params class with _data name.
Let's google to it's implementation: Found
As you can see it has toArray public method. Just use it.
print_r($response->body->toArray());
It has ArrayAccess implemented also. So, $response->body['status'] or $response->body['data'] will works.
Thank you guys for fast answers, here is my dumb way if reading status value
(thanks, #Jose Manuel Abarca Rodríguez)
$toJson = json_encode((array)$response->body);
$toJson = str_replace(array("\u0000MailWizzApi_Params\u0000_"), "", $toJson);
So we receive a normal json:
{"data":{"status":"success","data":{"subscriber_uid":"an837jdexga45","status":"unsubscribed"}},"readOnly":false}
And now we need just to decode it
$json = json_decode($toJson, true);
echo $json['data']['status'];

If an Object can hold an Array, how is the Array accessed?

I currently have an Object which holds a series of Arrays, when I do print_r it does show the arrays however, when I want to get these values out I seem to be getting an error.
print_r($Obj->Example);
Returns:
Object => Example ( 'Username' => 'Example' )
My code to query through the Object is:
foreach($Obj as $single):
echo $Example['Username'];
endif;
Is it possible to query through like this because it isn't working and I get an error saying that:
$Obj is not defined as an Array
So how can I access the Example array and echo all the Usernames ?
As Example is an array that belongs to an object, the code would be like this
foreach($Obj->Example as $single):
echo $single['Username'];
endif;

Pass Dynamic Values from one function to another function in class

Hi all i have a function in a class which will print all the variable pass for this function
Class Code :
<?php
class MyPrintClass {
public function printVaraible() {
var_dump(func_get_args())
}
}
$printVar = new MyPrintClass;
?>
if i use like below its correctly working and printing all the values pass.
$print->printVaraible('value1','Value2',20);
The output i got for above cmd
array (size=1)
0 => string 'value1' (length=6)
1 => string 'value2' (length=6)
2 => string 'value3' (length=6)
if i use like blow its showing as a single array.. i want the value seperated.. how its possible ?
function getVal(){
global $printVar;
$print->printVaraible(func_get_args());
}
I need to pass the value like below
getVal('value1','Value2',20);
and i need the output as
array (size=1)
0 => string 'value1' (length=6)
1 => string 'value2' (length=6)
2 => string 'value3' (length=6)
Currently i get NULL as the output
Updated Question Based On the Answer Given By deceze
** I Also had a small change in my code**
Class Code :
<?php
class MyPrintClass {
public function printVaraible($tag,$value) {
echo $tag.' == '.$value;
var_dump(func_get_args());
}
}
$printVar = new MyPrintClass;
?>
Converted Class To Function By
<?php
function getVal($tag,$value) {
global $printVar;
call_user_func_array([$printVar, 'printVariable'], $tag,$value,func_get_args());
}
?>
If i try to use it as below i get error
<?php getVal('first','second','third,'fourth'); ?>
Warning: call_user_func_array() expects exactly 2 parameters, 4 given
call_user_func_array([$print, 'printVariable'], func_get_args());
This calls the function with separate parameters from an array. I'd question the usefulness of this though, since you'll just end up with an array from func_get_args anyway. Why not pass arrays around in the first place?
Your getting null in the return because you never return anything, from the functions above.
if you want to call a class method I suggest you look into Refection or you can also do
$a = 'printVaraible';
$class->$a(1,3); //
besides using call_user_func_array, I would also suggest looking into closures (php > 5.3 ), which will let you write them this way.
$a = function foo(){ echo 'foo'};
$a();
Last thing I would generally avoid using global, as it obscures in your code where the values come from. It is always better to inject values into the function scope and not mix that with the global, imaging hunting that global down in a large project, I just avoid them at all cost and forget they exist.

Categories