I am getting an error like Creating default object from empty value on line
196($settings->{$v->Name} = $setting;
). I think, it's because I have upgraded php to 5.4 version.
So that I have set $setting =(object) null instead of $setting =null. But i couldn't correct it on line 196($settings->{$v->Name} = $setting;).
$settings;
foreach($SettingsRows as $v)
{
$setting =(object) null;
$setting->ID = $v->ID;
$setting->Name = $v->Name;
$setting->Value = $v->Value;
$setting->Class = $v->ClassName;
$setting->Form = new $setting->Class($setting);
$settings->{$v->Name} = $setting;
}
How can I set $setting value to $settings->{$v->Name} ?
What I need to change here?
Thanks!
Your code looks sketchy, but apparently you need $settings = new stdClass before the foreach loop. (new stdClass is the same thing as (object) null, but more clear in my opinion.)
And it really looks like $settings ought to just be an array (e.g., $settings[$v->Name] = $setting;), but I have no idea what you are trying to do.
Work for me with PHP 5.4
$query1 = $this->db()->prepare('SELECT id,title FROM category');
$query1->execute();
$d['query1']=(object) null;
foreach($query1->fetchAll(PDO::FETCH_OBJ) as $k=>$v){
$d['query1']->$k=$v;
$query2 = $this->db()->prepare('SELECT id,title FROM page');
$query2->execute();
$d['query2']=(object) null;
foreach($query2->fetchAll(PDO::FETCH_OBJ) as $k=>$v){
$d['query2']->$k=$v;
}
}
return $d;
Related
I'm trying to remove stdClass property by reference. Because I don't know how deeply the property is nested, a reference is made in the loop. But the unset method does not remove variables by reference. How can I resolve it without just setting a null value?
<?php
$data = new stdClass();
$data->foo = new stdClass();
$data->foo->bar = 'value';
$pathToRemove = 'foo.bar';
$dataReference = &$data;
foreach (explode('.', $pathToRemove) as $field) {
$dataReference = &$dataReference->$field;
}
unset($dataReference);
var_dump($data);
Loop over all the elements except the last. Then use the last element as the field to delete.
$pathArray = explode('.', $pathToRemove);
$lastField = array_pop($pathArray);
$dataReference = &$data;
foreach ($pathArray as $field) {
$dataReference = &$dataReference->{$field};
}
unset($dataReference->{$lastField});
unset($dataReference); // don't need the reference variable any more
The apache error logs are filling up with this. I don't want to suppress all errors though, and understand I need to create an object explicitly somewhere but the syntax escapes me.
Warning: Creating default object from empty value in
libraries/cegcore2/libs/helper.php on line 22
class Helper {
use \G2\L\T\GetSet;
var $view = null;
var $_vars = array();
var $data = array();
var $params = array();
function __construct(&$view = null, $config = []){
$this->view = &$view;
$this->_vars = &$view->_vars; // <---- Line 22
$this->data = &$view->data;
if(!empty($config)){
foreach($config as $k => $v){
$this->$k = $v;
}
}
}
}
The problem is that assuming view is null, you should not refer its items. You can do it like this:
function __construct(&$view = null, $config = []){
$this->view = &$view;
if ($view) {
$this->_vars = $view->_vars; // <---- Line 22
$this->data = $view->data;
}
if(!empty($config)){
foreach($config as $k => $v){
$this->$k = $v;
}
}
}
I want to make a query that results like this in codeigniter MODEL:
$caldata = array (
15 => 'yes',
17 => 'no'
);
Is that possible to do?
Take NOTE: The 15,17 and yes,no are in the same database table.
There is no core helper function to achieve what you want in CI. But you can create your own helper function:
function pluck($arr = [], $val = '', $key = '')
{
// val - label for value in array
// key - label for key in array
$result = [];
foreach ($arr as $value) {
if(!empty($key)){
$result[$value[$key]] = $value[$val];
}else{
$result[] = $value[$val];
}
}
return $result;
}
you can use result_array() function so you can have something like:
$query = $this->db->select('id,answer')->from('users')->get();
$result = $query->result_array();
print_r($result);
After that you have your array and you can make the $key => $value relation of the fields
After a long search i found an answer. Sample way to do this:
$query = $this->db->select('start_date, class')->from('event')->like('start_date', "$year-$month", 'after')->get();
$datavariable = $query->result();
$caldata = array();
foreach($datavariable as $row){
$caldata[substr($row->start_date,8,2)] = $row->class;
}
Im very new to PHP coding.
Just im getting values from object (class Project)and im iterating all object items to calculate duration of each project.
$projectDet = new Project();
$projectList = $projectDet->Find("name = ?",array($requ['name'] ));
if($projectList==false)
return 0;
foreach($projectList as $proj)
{
$reportData = array();
$reportData[] = array("Project","TotalTime"); //Testing
$key = $proj->id;
$EstDUR[$key] = (strtotime($proj->Enddt) - strtotime($proj->Startdt));
}
return $reportData;
But reportData returns null even projectList is not null.
Initate $reportData before foreach:
$reportData = array()
foreach($projectList as $proj) {
(..)
Hey guys I have a little issue with a function that retrieves data from a MySQL Database and then I iterate over the results with a foreach loop, checking a value to see if it is null and if it is, replacing it with another value.
The problem with this function is this, that after returning the data I'm only able to view one record retrieved from the database. Probably something simple but it's beyond me.
I would like to do this before passing it to the controller or view. Maybe this isn't possible with the foreach loop? What am I missing?
Here is an example of my code.
public function get_basic_user_data(){
$sql = 'SELECT Account.First_Name, Account.Last_Name, Account.User_Name, Profile_Photos.Thumb_Url
FROM Account
LEFT JOIN Profile_Photos ON Account.idAccount = Profile_Photos.Account_Id
AND Profile_Photos.Active = 1
WHERE Account.idAccount != ?';
$account_id = $this->get_account_id();
$data = $this->db->query($sql, $account_id);
foreach($data->result() as $row){
if($row->Thumb_Url == NULL){
$image = base_url().'assets/images/no_photo_thumb.png';
}else{
$image = $row->Thumb_Url;
}
$new_data = new stdClass;
$new_data->First_Name = $row->First_Name;
$new_data->Last_Name = $row->Last_Name;
$new_data->User_Name = $row->User_Name;
$new_data->Thumb_Url = $image;
}
return $new_data;
}
Hopefully someone can help me with this? Thanks!
At the moment you are just returning the last data row. Change your code like this to return an array of all your rows from that function:
$rows = array()
foreach($data->result() as $row){
if($row->Thumb_Url == NULL){
$image = base_url().'assets/images/no_photo_thumb.png';
}else{
$image = $row->Thumb_Url;
}
$new_data = new stdClass;
$new_data->First_Name = $row->First_Name;
$new_data->Last_Name = $row->Last_Name;
$new_data->User_Name = $row->User_Name;
$new_data->Thumb_Url = $image;
$rows[] = $new_data;
}
return $rows;
This way every row returned from the database will be added to an array named $rows. At the end you have to return your new array.
You are overwriting $new_data each iteration. Try this
$new_data = new stdClass
...
$all_data[] = $new_data;
Instead of checking for null value in the code, you could just use a IFNULL statement in the SQL query, this does separate the logic a bit but it might just be worth it in this case.
The function returns only the last row in the result because the new_data variable is overwritten in every step of your loop. Declare new_data an array at the start of your function and add rows as array elements
...
$new_data[] = new stdClass;
...
Each iteration of the foreach overwrites $new_data so in the end when the function returns, only the last fetched row will be returned. To return more than one row you could store all the rows in an array and then return the array in the end. It would look something like this:
public function get_basic_user_data(){
$sql = 'SELECT Account.First_Name, Account.Last_Name, Account.User_Name, Profile_Photos.Thumb_Url
FROM Account
LEFT JOIN Profile_Photos ON Account.idAccount = Profile_Photos.Account_Id
AND Profile_Photos.Active = 1
WHERE Account.idAccount != ?';
$account_id = $this->get_account_id();
$data = $this->db->query($sql, $account_id);
$data = array();
foreach($data->result() as $row){
if($row->Thumb_Url == NULL){
$image = base_url().'assets/images/no_photo_thumb.png';
}else{
$image = $row->Thumb_Url;
}
$new_data = new stdClass;
$new_data->First_Name = $row->First_Name;
$new_data->Last_Name = $row->Last_Name;
$new_data->User_Name = $row->User_Name;
$new_data->Thumb_Url = $image;
$data[] = $new_data;
}
return $data;
}
To be able to use this function you have to change the code that uses it to loop through the array of objects.