Starting from a variable called $data, which is an associative array which includes an object, and whose printed value is this:
Array
(
[item] => stdClass Object
(
[id] => 1
[tipo] => 0
[idioma] => es
[nombre] => Artículo de prueba
[titulo] => Esto es un artículo de prueba
[alias] => articulo-de-prueba
[texto] => Lorem ipsum etc etc
[url] =>
[video] =>
[fecha_c] => 2012-11-27 10:50:37
[fecha_m] => 2012-11-27 17:00:00
[fecha_p] => 2012-11-28 00:00:00
[destacado] => 0
[status] => 1
)
[imagenes] => Array
(
)
)
I need to filter its value and assign it to another array, this way:
protected function load_form($data = '') {
$this->load->helper('form');
// If item data have been sent, pass it to the form view to edit it.
// Else display empty form for new item.
if (! empty($data)) {
// Data can be an associative array with an object and another array or just an object
if (array_key_exists('item', $data)) {
$this->_vars['item'] =& $data['item'];
}
else {
$this->_vars['item'] =& $data;
}
if (array_key_exists('imagenes', $data)) {
$this->_vars['imagenes'] = $data['imagenes'];
}
}
$view = $this->load->view(ADMIN_FORMS_PATH . $this->_controller . '_form', $this->_vars, true);
/*DEBUG*/ echo $view; // just for debugging purposes
}
The first assignment generates these errors:
A PHP Error was encountered Severity: Notice Message: Undefined index:
item Filename: core/Admin_Controller.php Line Number: 205
A PHP Error was encountered Severity: Notice Message: Object of class
stdClass could not be converted to int Filename:
core/Admin_Controller.php Line Number: 205
It behaves like the item index doesn't exist, and it does. Also, it tries to convert the object to an integer.
Why does it happen and what should I do to fix it?
EDIT:
I was doing &= instead of =&. That's the reason of the errors.
Anyway, the problem persists and the code seems to stop.
EDIT2:
Trying to redefine the problem. It might be something related to CodeIgniter, so I've added the whole function, including CodeIgniter functions.
The load_form() method can be invoked from a request to create a new item, in which case $data is empty, or from a request to edit a given item (in $data). In the first case (creation), the debug line is executed, but not in the second case (edition).
This may be your issue. Near the end you have"$datos['imagenes'];" where it should be "$data['imagenes'];"
if (! empty($data)) {
// Data can be an object, or an array with object + array of images
if (array_key_exists('item', $data)) {
$this->_vars['item'] &= $data['item'];
}
else {
$this->_vars['item'] = $data;
}
if (array_key_exists('imagenes', $data)) {
$this->_vars['imagenes'] = $data['imagenes'];
}
}
Problem solved. It was too simple.
It was inside the view. I was trying to echo:
$item['id']
instead of
$item->id
The item data is being retrieved as an object, not an array. It was causing the problem silently, without any warning, notice or error.
Anyway I appreciate your quick help.
Related
i am fetching data from two tables as below :
$form['form']=json_decode($this->db->get_where('forms',array(
'id' => $id
))->result()[0]->form_sections);
This gives me following result on printing :
Array
(
[form] => Array
(
[0] => Personal_Information
[1] => Education_
[2] => Professional_Life
)
)
Now i have details against each of these array indexes that i am trying to assign them as below
foreach ($form['form'] as $value){
$value->details=$this->db->get_where('formdata',array(
'form_section' => $value
))->result();
}
Which gives me following error
Message: Attempt to assign property 'details' of non-object
can someone please help me to sort out the issue , i have to assign the index and loop in it in my view
This should help understand and clear up the message:
// Custom data
$Person_Info = 404; // Could be any non-object
$array = array("form" => array( 0 => $Person_Info) );
foreach( $array['form'] as $value ){
$results = new stdClass();
$results->name = "User A";
$value->details = $results; // $results are you fetch request
}
// Output: Attempt to assign property 'details' of non-object in php<8.0
// Output: Uncaught Error: Attempt to assign property "details" on int php>=8.0
Try to print_r or var_dump your $value and see what you are getting. It's not an object for some of your array item.
Edit: If ever in doubt about the data. Use if (is_obejct($var)) php.net/is_object or similar checks based on what you need.
I am working on CakePHP 3.2
I have this function to find product types if exists
if ($product['product_type'] != null) {
$getProductType = $this->ProductTypes->find()
->where(['title LIKE' => '%'.$product['product_type'].'%'])
->first();
debug($product['product_type']); // this is not empty
debug($getProductType); // this shows 'id' => 1 in array
debug($getProductType->id); // this shows 1
if (!empty($getProductType)) {
$p->product_type_id = $getProductType->id; // line 11
$p->subcategory_id = $getProductType->subcategory_id;
$p->category_id = $getProductType->category_id;
return $this->saveNewBulkProduct($product, $p->category_id, $p->subcategory_id, $p->product_type_id);
}
}
But this is giving warning as
Creating default object from empty value on line 11
Edit 2 Output of print_r($getProductType)
App\Model\Entity\ProductType Object
(
[id] => 3
[category_id] => 1
....
)
I think no need to declare $p and not set property and its value in $p, you can pass values of $getProductType object as mentioned below.
if ($product['product_type'] != null) {
$getProductType = $this->ProductTypes->find()
->where(['title LIKE' => '%'.$product['product_type'].'%'])
->first();
debug($product['product_type']); // this is not empty
debug($getProductType); // this shows 'id' => 1 in array
debug($getProductType->id); // this shows 1
if (!empty($getProductType)) {
return $this->saveNewBulkProduct($product, $getProductType->category_id, $getProductType->subcategory_id, $getProductType->id);
}
}
you have to declare first $p = new stdClass(); on line 11
So I'm trying to echo specific values from the array, but I get message:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: views/drilldown.php
Line Number: 92
that many times.
Heres the result of print_r of the array:
Array ( [0] => Array ( [productCode] => S10_1949 [productName] => 1952 Alpine Renault 1300 [productLine] => Classic Cars [productScale] => 1:10 [productVendor] => Classic Metal Creations [productDescription] => Turnable front wheels; steering function; detailed interior; detailed engine; opening hood; opening trunk; opening doors; and detailed chassis. [quantityInStock] => 7305 [buyPrice] => 98.58 [MSRP] => 214.3 [image] => S10_1949.jpg ) )
View Code:
$follow = $p['0'];
foreach($follow as $item) {
echo $item->productCode;
}
Model Code:
public function getProduct($p)
{
$this->db->where('productCode', $p);
$resultset = $this->db->get('products');
return $resultset->result_array();
}
Controller Code:
function drillDown()
{
$productID=$this->uri->segment(3);
$data['p']=$this->littlemodel->getProduct($productID);
$this->load->view('drillDown',$data);
}
Can anybody please help?
I believe you just want one row from the database.
You should be using
return $resultset->row()
So in the model, try this:
public function getProduct($p)
{
$this->db->where('productCode', $p);
return $this->db->get('products')->row();
}
Keep the controller code the same.
In the view, use this:
echo $p->productCode;
This is just an optimized way of fetching your requirements. In case of any trouble, do let me know. Will be happy to help.
I'm getting the following error reports:
Severity: Notice
Message:
Trying to get property of non-object
Using gettype() I can see that it's a proper object.
A print_r() returns:
stdClass Object ( [campaigngroupid] => 3 [name] => And another one [dt_created] => 2014-02-04 17:11:21 [created_userid] => 1 [deleted] => 0 )
Echoing out $object->name for example works fine, but still, I'm getting this notice...
The object is set using CodeIgniter's ->row() from a database query result.
All I can ask is, sup?
Try this in your model, Suppose $query holds the query object:
function some_model_function(){
..........
..........
if($query->num_rows() > 0 ){
return $query->row();
}
return FALSE;
}
Also check whether the notice is from any where else.
I get some data from a table
Data is returned into an array like this
Array ( [id_widget] => 11 [id_user] => 7 [active] => 1 )
Then I am trying to use this "id_widget" and "entryemail" to insert them into another table.
"newentry" comes from an input, but I don't know how to post the "id_widget"
This is my model function
public function addentry($data) {
$this->db->insert('entries', array(
'id_widget' => $data['id_widget'],
'entryemail' => $data['entryemail']
));
}
This is my controller function:
public function entercontest() {
$entry = array(
'id_widget' => $this->widget[0]['id_widget'],
'entryemail' => $_POST['entryemail']
);
$this->model->addentry($entry);
}
It works to insert "entryemail" if I comment id_widget line everywhere.
The error doesn't occur when getting the value from the array $data['id_widget']. The error occurs in the entercontent function because you're populating the array with an undefined value because $this->widget is undefined.
Make sure $this->widget is defined and has the data before using it.