I'm trying to create associative array of objects from row result set with member id as the key, but getting some error.
addATravelog() is just a function of the class UserLogsAndSOS(), whose objects i want in array.
Here is what I tried:
class UserArraySet {
private $arrayOfUsers = array();
function createArrayForTravelogs($result) {
While($row = $result->next()) {
if(array_key_exists($row['id'], $this->arrayOfUsers)) {
$this->arrayOfUsers[$row['id']] = new UserLogsAndSOS();
}
$this->arrayOfUsers[$row['id']]->addATravelog($row['title'], $row['blog']); //line 72
}
}
}
On calling createArrayForTravelogs() from the object I got the following error
Here is the error I got:
Notice: Undefined index: 1 in C:\xampp\htdocs\site\classes\userprofile.php on line 72
Fatal error: Call to a member function addATravelog() on a non-object in C:\xampp\htdocs\site\classes\userprofile.php on line 72
Can someone please let me know how to achieve this, I want something like this:
Array (
[1] => objectUserLogsAndSOS1
[5] => objectUserLogsAndSOS2
....
)
where key is the member id from $row.
I also need to check if the key exists, then call a function of that particular object to add data to its member, if not then create an object and then call a function of that particular object to add data to its member.
Thanks
just read the error message: you only create UserLogsAndSOS if there is already an entry - otherwise you call addATravelog on null.
maybe you forgot the "!" in your if clause?
Because the array stays empty.
You only create a new UserLogsAndSOS when there already is an element with the provided ID in the arrayOfUsers. The exact opposite of what you probably wanted.
You're probably missing a ! to reverse the array_key_exist result.
if(!array_key_exists($row['id'], $this->arrayOfUsers)) {
$this->arrayOfUsers[$row['id']] = new UserLogsAndSOS();
}
You missed and '!' I think this is causing the error
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'm trying to figure out a way to call a method of a specified member (Class A) coming from an array of members in class B.
<?php
class A
{
public function do_something()
{
echo "class A did something";
}
}
class B
{
private $arr = array();
private $current_index = 0;
public function add_new_A()
{
$new_a = new A;
array_push($this->arr, (object) [
$this->current_index => $new_a
]);
$this->current_index++;
}
public function get_an_A_by_index($index)
{
return $this->arr{$index};
}
public function do_something_with_A_member_inside_array($index)
{
self::get_an_A_by_index($index)->do_someting();
}
}
$b = new B;
$b->add_new_a();
$b->add_new_a();
echo print_r($b->get_an_A_by_index(0));
echo "\n";
$b->do_something_with_A_member_inside_array(0); // returns error
// console:
// stdClass Object ( [0] => A Object ( ))
// Uncaught Error: Call to undefined method stdClass::do_something();
?>
To wrap things up, I want to know if my approach is considered bad and/or if there is something I can do to fix the error. Before you disagree with my code, take a look at my php code I'm actually working on. That's all for my question.
Now about why I want to call a method of a member A inside . For my assignment I want a program that does something seperately with the method do_something of class a. So far, the only way to do that is by storing seperate members of A.
I'm not sure if I'm approaching this wrong or not, because I'm coming from Java. So, The first thing I came up with was the approach shown above. When I do this in Java, it works fine. But php is different from Java and I'm still learning how php works since I'm new to it.
Your code isn't far off, you've just got an issue with the way you're building up the collection of A objects.
array_push($this->arr, (object) [
$this->current_index => $new_a
]);
This is creating a data structure that I'm pretty sure isn't what you expect. You'll end up with an array full of stdClass objects, each with a single A member and its own internal index:
Array
(
[0] => stdClass Object
(
[0] => A Object
(
)
)
)
You're then retrieving the stdClass object and trying to run the method on that, hence the Call to undefined method stdClass::do_something... error you're seeing.
Instead, all you need to do is this:
$this->arr[$this->current_index] = $new_a;
The rest of your code is just expecting an array of A objects, nothing nested any deeper.
I've put a full example here: https://3v4l.org/ijvQa. Your existing code had a couple of other typos, which are also fixed. You'll spot them easily enough if you turn on error reporting.
This is a strange one (for me at least), I'm trying to pass an object to a function like so:
272. $runs[$key]['position'] = getTimePosition($run->times,$user['id']);
273. dd($run->times);
When I do it I get the error
message: "Trying to get property 'times' of non-object", exception: "ErrorException",…}
exception: "ErrorException"
file: "C:\Users\Test\PhpstormProjects\test\app\Helpers\Helper.php"
line: 272
message: "Trying to get property 'times' of non-object"
However, if I dump $run->times I get what seems to be correct, an Eloquent object with "times" as shown below...
What am I missing here? It's been bugging me for an hour
As I guess your result set return multiple rows so that you need to iterate them by using the loop or you can get the result by using.
$run[0]->time
If you want to get a single row from DB you should use it.
$run = DB::table('tableName')->where('condition')->first();
$run->time
It will work.
Your attribute name is $run->time, not $run->times
$runs[$key]['position'] = getTimePosition($run->time,$user['id']);
Try this one
if($run) {
$runs[$key]['position'] = getTimePosition($run->times,$user['id']);
}
I'm very fresher in CodeIgniter and practicing. I'm now developing a simple Codeigniter application just for practicing. I've Banks and its branches in the database. I just want to show branches with its bank name. Branches are showing but in the controller while getting banks, this error is showing. I tried these in SO links, but nothing found works.
"A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object"
in Line Number: 85 and 91
This is my controller
function ShowBranchList() {
$branch_list = $this->FD_Model->get_all_branches();
$get_bank = $this->FD_Model->get_bank_by_branch($branch_list->bankid); //This is Line Number 85
if($branch_list ){
$data = array(
'pagetitle' => 'Branch List View',
'branch_list_data' => $branch_list,
'br_bank' => $get_bank['bank_name'],//This is Line Number 93
);
}
Model
/*function for getting all branches*/
function get_all_branches() {
$this->db->order_by( $this->brid, $this->order );
return $this->db->get( $this->brtable )->result();
}
/*function for getting banks by branches*/
function get_bank_by_branch( $id ) {
$this->db->where( $this->bid, $id);
return $this->db->get( $this->banktable )->row();
}
And finally, this is my View
foreach ($branch_list_data as $branch_list)
{
<?php echo $branch_list->brname ?>
<?php echo $br_bank; ?>
}
Anyone know the issue?
Update
When I'm tring to view the query
echo $this->db->last_query();
Result
SELECT * FROM tbl_bankmaster WHERE bid IS NULL
The value $branch_list->bankid is null.When I'm hardcoding some values the view page is showing without any error.
do like this
$get_bank = $this->FD_Model->get_bank_by_branch($branch_list[0]->bankid);
AND
'br_bank' => $get_bank[0]->bank_name # this will work maybe
or
'br_bank' => $get_bank[0]['bank_name']
Its 0 indexed array so you need to add the key to access the child's
Change your data in view like this
foreach ($branch_list_data as $branch_list)
{
echo $branch_list['brname']; //change your data like this
echo $br_bank;
}
I finally make it happen with the help of Abdulla Nilam's answer. I just passed the value in array like
'br_bank' => $get_bank->bank_name
Now the view is working.
get_bank_by_branch($branch_list->bankid); This method returning only object.
$get_bank this variable you're accessing by an array, but it's an object.
I got two zend forms which belongs to one table in the database, broke it up in order to make sure the user does not find it lazy to finish it but I have a problem with my code in my model. I keep getting this error even though I tried numerous solutions.
Fatal error: Call to a member function save() on a non-object
public function smmedetailssmmedetails($companyname, $companytradingname)
{
$data = array(
'companyname' => $companyname,
'companytradingname' => $companytradingname,
);
return $this->insert($data);
}
public function smmesdetails2smmedetails($smmeid, $numberemployees, $ownership)
{
$row = $this->find($smmeid)->current();
$row->numberemployees = $numberemployees;
$row->ownership = $ownership;
return $row->save(); //problem lies on this line
}
Tried using the following codes/methods
$this ->row->save();
$row->save();
$this ->row->save();
return $this -> row->save();
According to the error you're getting, none of the method invocations you are trying will work.
You should first check what
$this->find($smmeid)->current();
returns using either a debugger, var_dump or print_r.
In your case, it seems that either null or something that is not an object is returned.
This means, that $row is not an object (should be), but probably is null. This means that find() method didn't get any result from database.
You should check $smmeid value and check if a such a row exists in the database.
Use var_dump function to check any variable in any line. This is how you can debug your problem.
Hey guys I got it using this method below, I marked codeblur answer because it was close thanks for the print_r information:
Example #1 print_r() example
<pre>
<?php
$a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z'));
print_r ($a);
?>
</pre>
return ($row);//correct answer