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
Related
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;
}
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();
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'];
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?
I have this data spited out from my cake php query.. This is the problem.. How can i retrieve this data so that it fits this structure. How can I do this in my view?, I am just having trouble understanding how to retrieve each value
so this part I want it in a <p></p> in my page
array
'id' => string '1' (length=1)
'category_name' => string 'Appetizers' (length=10)
'category_keywords' => string 'appetizer, appetizers' (length=21)
'category_title' => string 'Our Side Dishes' (length=15)
'category_info' => string 'Hello This is the category info' (length=30)
'dish_id' => string '1' (length=1)
This other part returned of the query i want to put it in a 3 x 2 table. like
<table>
<tr>
<td> the data from the array 0</td>
<td> the data from the array 1</td>
<td> the data from the array 2>/td>
</tr>
<tr>
<td> the data from the array 3</td>
<td> the data from the array 4</td>
<td> the data from the array 5>/td>
</tr>
here are all the those arrays. I would like to use a for loop with a mod if 3 %== 0 and then create a new <tr>, i tried it but it gave me an error...
array
'id' => string '1' (length=1)
'category_name' => string 'Appetizers' (length=10)
'category_keywords' => string 'appetizer, appetizers' (length=21)
'category_title' => string 'Our Side Dishes' (length=15)
'category_info' => string 'Hello This is the category info' (length=441)
'dish_id' => string '1' (length=1)
array
0 =>
array
'id' => string '1' (length=1)
'dish_name' => string 'Arepa Con Pollo!' (length=16)
'dish_disc' => string 'Exquisite corn patti, the best yyy' (length=34)
'dish_price' => string '2.90' (length=4)
'dish_image' => string '/img/arepa rellena con pollo.jpeg' (length=33)
'dish_category_id' => string '1' (length=1)
'dish_price_label' => string 'Delicious Arepa ' (length=16)
1 =>
array
'id' => string '2' (length=1)
'dish_name' => string 'Arepa Con Queso' (length=15)
'dish_disc' => string 'Corn patie with Cheese' (length=22)
'dish_price' => string '2.65' (length=4)
'dish_image' => string '/img/arepa rellena con queso.jpeg' (length=33)
'dish_category_id' => string '1' (length=1)
'dish_price_label' => string '' (length=0)
2 =>
array
'id' => string '3' (length=1)
'dish_name' => string 'Empanadas de Carne' (length=18)
'dish_disc' => string 'Patties with beef' (length=17)
'dish_price' => string '1.60' (length=4)
'dish_image' => string '/img/empanadas de carne.jpeg' (length=28)
'dish_category_id' => string '1' (length=1)
'dish_price_label' => string '' (length=0)
3 =>
array
'id' => string '4' (length=1)
'dish_name' => string 'Empanadas de Pollo' (length=18)
'dish_disc' => string 'Patties with Chicken' (length=20)
'dish_price' => string '1.94' (length=4)
'dish_image' => string '/img/empanadas de pollo.jpeg' (length=28)
'dish_category_id' => string '1' (length=1)
There are more arrays besides these.. but they all have the same format.
These come from working with the following controllers
class DishCategory extends AppModel{
public $hasMany = array(
'Dish' => array(
'className' => 'Dish',
'foreignKey' => 'dish_category_id'
)
);
}
class DishCategoriesController extends AppController {
function get_categories($id)
{
// find category with a dish of $id
$this->set('dishes', $this->DishCategory->find('first', array(
'conditions' => array(
'DishCategory.id' => $id
)
)));
// set master layout
$this->layout = 'master_layout';
}
}
Use this->set('variableName', $data); to pass your arrays from the controller to the view.
You can then access the example above as $variableName in your view. I would recommend doing a <?php debug($variableName); ?> first to see what your array looks like in the view. It should be the same, but it will help you know what indices to use.
For more info: http://book.cakephp.org/view/979/set