I dont have the same result when i var_dump an array in my controller and in my view. This is my controller:
public function index()
{
$getFestivals = $this->festival_model->getAllFestivals();
if (!empty($getFestivals)) {
foreach ($getFestivals as $festival) {
$countFestivalEdition = $this->festival_model->countFestivalEditionByFestivalID($festival->fID);
$festival->fCountEdition = $countFestivalEdition;
}
}
$this->data['getFestivals'] = $getFestivals;
#var_dump($this->data['getFestivals']);die;
$this->render_layout('festivals/index', $this->data);
}
The var_dump (juste before last line) return me:
0 =>
object(stdClass)[24]
public 'fID' => string '6' (length=1)
public 'fName' => string 'Festival2' (length=17)
public 'fPicture' => string '3df2c152d9dec657380c53b679d0b92b.jpg' (length=36)
public 'fCity' => string 'City2' (length=9)
public 'fCountry' => string 'France' (length=6)
public 'fCountEdition' => int 2
1 =>
object(stdClass)[25]
public 'fID' => string '8' (length=1)
public 'fName' => string 'Festival1' (length=19)
public 'fPicture' => string '73a267e6c047d507b66b3a1ab1fc0059.jpg' (length=36)
public 'fCity' => string 'City1' (length=16)
public 'fCountry' => string 'France' (length=6)
public 'fCountEdition' => int 1
On this var_dump i have my 'fCountEdition'. When i make var_dump on my view, i have this (var_dump($getFestivals);die;):
0 =>
object(stdClass)[24]
public 'fID' => string '6' (length=1)
public 'fName' => string 'Festival2' (length=17)
public 'fPicture' => string '3df2c152d9dec657380c53b679d0b92b.jpg' (length=36)
public 'fCity' => string 'City2' (length=9)
public 'fCountry' => string 'France' (length=6)
1 =>
object(stdClass)[25]
public 'fID' => string '8' (length=1)
public 'fName' => string 'Festival1' (length=19)
public 'fPicture' => string '73a267e6c047d507b66b3a1ab1fc0059.jpg' (length=36)
public 'fCity' => string 'City1' (length=16)
public 'fCountry' => string 'France' (length=6)
Why he dont parse my data fCountEdition ?
For informations, my render_layout look like this:
$this->load->view($view, $this->data);
You may try this simple way to send array to view as give example bellow.
Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
public function index()
{
$data=$this->db->query("SELECT * FROM table_name");
$result=$data->result_array();
$headerData = array(
"pageTitle" => "Home",
"stylesheet" => array("home.css")
);
$footerData = array(
"jsFiles" => array("home.js")
);
$viewData = array(
"viewName" => "home",
"viewData" => array('result'=>$result),
"headerData" => $headerData,
"footerData" => $footerData
);
//Sendig all Result Data to view by Storing into array as $result
$this->load->view('template',$viewData);
}
View
<?php
var_dump($result);
?>
It's because you're not referencing $getFestivals, instead you are passing the value of it to $festival and trying to assign it a new variable.
Basically, passing the value allows you to 'copy' the array/object/etc without modifying the original.
Referencing the original allows to to modify it directly from within the loop.
Do do it the way you've written the code, you need to reference the original instead.
foreach ($getFestivals as &$festival) { <-- add ampersand before $
$countFestivalEdition = $this->festival_model->countFestivalEditionByFestivalID($festival->fID);
$festival->fCountEdition = $countFestivalEdition;
}
Related
I am using using yii2 to in basic template with MySQL database
Why This code returns an object instead of an array of selected records from Database
when i use var_damp($rooms) the output is seems an object and not an array of selected record in array format;
any body can help
public function actionIndexFiltered()
{
$query = Room::find();
$searchFilter = [
'floor' => ['operator' => '', 'value' => ''],
'room_number' => ['operator' => '', 'value' => ''],
'price_per_day' => ['operator' => '', 'value' => ''],
];
if(isset($_POST['SearchFilter']))
{
$fieldsList = ['floor', 'room_number', 'price_per_day'];
foreach($fieldsList as $field)
{
$fieldOperator = $_POST['SearchFilter'][$field]['operator'];
$fieldValue = $_POST['SearchFilter'][$field]['value'];
$searchFilter[$field] = ['operator' => $fieldOperator, 'value' => $fieldValue];
if( $fieldValue != '' )
{
$temp1=$query->andWhere([$fieldOperator, $field, $fieldValue]);
}
}
}
$room1=$temp1->all();
$rooms = $query;
return $this->render('indexFiltered', [ 'rooms' => $rooms, 'searchFilter' => $searchFilter,'room1'=>$room1 ]);
}
Output is like this and this shows that this code return a query object and not an array of query execution on database ,
so i checked the code using var_dump function the results is that it is returning a object not array
object(yii\db\ActiveQuery)[70]
public 'sql' => null
public 'on' => null
public 'joinWith' => null
public 'select' => null
public 'selectOption' => null
public 'distinct' => null
public 'from' =>
array (size=1)
0 => string 'room' (length=4)
public 'groupBy' => null
public 'join' => null
public 'having' => null
public 'union' => null
public 'params' =>
array (size=0)
empty
private '_events' (yii\base\Component) =>
array (size=0)
empty
private '_behaviors' (yii\base\Component) =>
array (size=0)
empty
public 'where' =>
array (size=3)
0 => string '=' (length=1)
1 => string 'room_number' (length=11)
2 => string '3' (length=1)
public 'limit' => null
public 'offset' => null
public 'orderBy' => null
public 'indexBy' => null
public 'emulateExecution' => boolean false
public 'modelClass' => string 'app\models\Room' (length=15)
public 'with' => null
public 'asArray' => null
public 'multiple' => null
public 'primaryModel' => null
public 'link' => null
public 'via' => null
public 'inverseOf' => null
This code return an a dataProvider .. alias the code for get models /active record
$query = Room::find();
for obtain the all the models you can use
$roomModels= Room::find()->all();
in this the result is a collection of obejct (models) of class Room
if you need an array you can use
$roomArray = Room::find()->asArray()->all();
In your code $temp1 is the same as $query. andWhere() method returns $this which means that
$temp1 = $query->andWhere([$fieldOperator, $field, $fieldValue]);
makes $temp1 to be the same as $query which is object of yii\db\ActiveQuery.
Now you are calling this:
$room1 = $temp1->all(); // the same as $room1 = $query->all();
and this holds array of Room objects while
$rooms = $query;
is just another unnecessary assignment because $rooms is the same as $query which is object of yii\db\ActiveQuery.
Finally i found the reason for this problem is that
I must call asArray() member function of ActiveRecord class to generate an array output
$room1=$temp1->all(); will changed to $room1=$temp1->asArray()->all();
Ok, so I have an array of objects created in my controller
$displayRooms[$room] = $this->getNextEvent($events, $room);
return view('schedule')->with(['displayRooms' => $displayRooms]);
Then in my view, I am able to iterate over the array and get the $room and $events
#foreach ($displayRooms as $room => $events)
{{ var_dump($events) }}
#endforeach
The var_dump($events) produces
/home/vagrant/sites/google-cal/storage/framework/views/96faa8d8afe2e0f07db5d96a536009499a3c48a5.php:22:
object(stdClass)[176]
public 'available' => boolean false
public 'room' => string 'Room 318' (length=8)
public 'summary' => string 'Chem 3090 Himmeldirk' (length=20)
public 'startTime' => string '12:00pm' (length=7)
public 'endTime' => string '3:00pm' (length=6)
public 'startDay' => string '03/21/17' (length=8)
public 'endDay' => string '03/21/17' (length=8)
/home/vagrant/sites/google-cal/storage/framework/views/96faa8d8afe2e0f07db5d96a536009499a3c48a5.php:22:null
/home/vagrant/sites/google-cal/storage/framework/views/96faa8d8afe2e0f07db5d96a536009499a3c48a5.php:22:null
/home/vagrant/sites/google-cal/storage/framework/views/96faa8d8afe2e0f07db5d96a536009499a3c48a5.php:22:
object(stdClass)[226]
public 'available' => boolean false
public 'room' => string 'Room 513 (Voinovich Room)' (length=25)
public 'summary' => string 'Reynolds' (length=8)
public 'startTime' => string '1:00pm' (length=6)
public 'endTime' => string '3:00pm' (length=6)
public 'startDay' => string '03/21/17' (length=8)
public 'endDay' => string '03/21/17' (length=8)
My question is how can I iterate over the events object in Blade in order to access the properties of the object, so that I may pass them to a partial view?
If I'm understanding what you are asking (and your data structure) properly, then this should work. You can iterate the array of event objects to access each one individually, and then iterate each individual one to access each individual property / value. This code should be nested within your first foreach (replace the dump with it).
#foreach($events as $event)
#foreach ($event as $prop => $value)
// do whatever with prop / values
#endforeach
#endforeach
If you don't need to access each property / value of the object one at a time, then I would recommend this instead.
#foreach($events as $event)
// do whatever with each event
{{ var_dump($event->available) }}
#endforeach
I get protected array from this
$user_id = Mage::getSingleton('admin/session')->getData('user');
var_dump($user_id);die;
object(Mage_Admin_Model_User)[115]
protected '_eventPrefix' => string 'admin_user' (length=10)
protected '_role' => null
protected '_hasAvailableResources' => boolean true
protected '_eventObject' => string 'object' (length=6)
protected '_resourceName' => string 'admin/user' (length=10)
protected '_resource' => null
protected '_resourceCollectionName' => string 'admin/user_collection' (length=21)
protected '_cacheTag' => boolean false
protected '_dataSaveAllowed' => boolean true
protected '_isObjectNew' => null
protected '_data' =>
array (size=17)
'user_id' => string '1' (length=1)
I want to get 'user_id'.,and tried like that $id = $user_id['_data']['user_id']; but it returns null
Try the following
$userId = Mage::getSingleton('admin/session')->getUser()->getId();
or
$userData = Mage::getSingleton('admin/session')->getUser()->getData();
$userId = $userData['user_id'];
As title, I have a library function $this->foo->bar(). How can I pass $upload_data array to $this->foo->bar() param?
I have this in my controller
$upload_data = $this->upload->data();
//$upload_data['file_name']
$this->foo->bar($upload_data);
Then I have this in my library
public function bar($arr){
$config['source_name'] = //How can I use $arr['file_name'] in here
}
And I got this error
Message: Illegal string offset 'file_name'
var_dump($upload_data)
array (size=14)
'file_name' => string '1358459309.JPG' (length=14)
'file_type' => string 'image/jpeg' (length=10)
'file_path' => string 'C:/wamp/www/foldering/assets/img/uploaded_photos/' (length=49)
'full_path' => string 'C:/wamp/www/foldering/assets/img/uploaded_photos/1358459309.JPG' (length=63)
'raw_name' => string '1358459309' (length=10)
'orig_name' => string '1358459309.JPG' (length=14)
'client_name' => string '200X CNY1.JPG' (length=13)
'file_ext' => string '.JPG' (length=4)
'file_size' => float 30.87
'is_image' => boolean true
'image_width' => int 640
'image_height' => int 480
'image_type' => string 'jpeg' (length=4)
'image_size_str' => string 'width="640" height="480"' (length=24)
My mistake, issue comes from another function
I have the following setup:
Model: User
Model: Client
User hasAndBelongsToMany Client
Client hasAndBelongsToMany User
DB Tables:
Users
id
lots of fields
Clients
id
lots of fields
Cliens_Users
id
client_id
user_id
status
Everything is working just peachy but I am wondering if there is a better way to get the status field (associated with the HABTM table) with my Client model.
Here is what I'm doing:
ClientsController.php
public function view($id = null) {
$this->Client->id = $id;
if (!$this->Client->exists()) {
throw new NotFoundException(__('Invalid client'));
}
$this->Client->contain('User.id');
$this->Client = $this->Client->read(null, $id);
$this->set('client', $this->Client);
}
the var_dump on my $this->Client looks like this:
array
'Client' =>
array
'id' => string '1' (length=1)
'first_name' => string 'Matt' (length=4)
'last_name' => string 'Moses' (length=4)
'address_1' => string '123 S Happy Lane' (length=16)
'address_2' => string '' (length=0)
'city' => string 'Yourtown' (length=5)
'state' => string 'TX' (length=2)
'zip' => string '12345' (length=5)
'phone_home' => string '8014567899' (length=10)
'phone_work' => string '8014567899' (length=10)
'phone_cell' => string '8014567899' (length=10)
'email' => string 'matt#matt.com' (length=13)
'created' => string '2012-06-13 04:41:19' (length=19)
'modified' => string '2012-06-13 04:41:19' (length=19)
'User' =>
array
0 =>
array
'id' => string '1' (length=1)
'ClientsUser' =>
array
...
Clients/view.ctp
...
<?php echo $client['User'][0]['ClientsUser']['status']; ?>
...
To me this last line screams "there is a cleaner way" but I can't seem to find anything on the matter. Any thoughts?