Reputation: 641
So, I am using codeigniter and I am trying to use the calendar class and pass "events" to my calendar. This is done by an array and an identifier like so:
$calendar_info = array(
'month' => '12',
'year' => '2013',
'data' => array(
3 => 'Spaghetti<br/>Meatballs<br/>Salad<br/>Canned Fruit',
4 => 'Ham<br/>Mashed Potatoes<br/>Corn<br/>Salad</br>'
)
);
Now I can't hard code the menu items into the application, I am getting them from a database. My problem lies in putting the foreach into the array, I get the following error:
Parse error: syntax error, unexpected T_FOREACH, expecting ')'
That error is only a portion so not to reveal my web address. Below is what I am trying to do right now and getting the error above.
$this->db->select('*');
$this->db->from('menus');
$this->db->where('user_id', $session_data['id']);
$query = $this->db->get();
$calendar_info = array(
'month' => '12',
'year' => '2013',
'data' => array(
foreach ($query->result() as $row)
{
$row->menu_day => $row->menu_entree.'<br/>'.$row->menu_sideone.'<br/>'.$row->menusidetwo.'<br/>'.$row->menudessert
}
),
);
UPDATE
This is exactly what I have/am trying to do below:
I have a database table that holds my menu items for each day of the month.
the columns are:
menu_day
menu_month
menu_entree
menu_sideone
menu_sidetwo
menu_dessert
the array needs to look like this:
array(
menu_day => 'menu_entree<br/>menu_sideone<br/>menu_sidetwo<br/>menu_dessert
)
Or for example:
array(
3 => 'Spaghetti<br/>Meatballs<br/>Salad<br/>Canned Fruit'
)
Upvotes: 1
Views: 8980
Reputation: 26066
You simply cannot put a foreach
inside an array the way your question has it laid out. You need to set the values ahead of time with a foreach
loop and then place that array into $calendar_info
that way:
$this->db->select('*');
$this->db->from('menus');
$this->db->where('user_id', $session_data['id']);
$query = $this->db->get();
$data_array = array();
foreach ($query->result() as $row) {
$data_array[$row->menu_day] = $row->menu_entree . '<br/>' . $row->menu_sideone . '<br/>' . $row->menusidetwo . '<br/>' . $row->menudessert;
}
$calendar_info = array(
'month' => '12',
'year' => '2013',
'data' => $data_array
);
Upvotes: 1
Reputation: 270609
You cannot loop inside an array()
declaration as you are trying to do. Instead, the right course of action would be to build your array with the loop as you've done, then assign it into $calendar_info['data']
.
Don't think of the PHP code as thing to build dynamically. It's the array data structure you are generating, not the code which defines it.
// First build the array with your loop...
// An empty array to hold the result...
$data = array();
foreach ($query->result() as $row)
{
// Add array keys to the array while looping...
$data[$row->menu_day] = $row->menu_entree.'<br/>'.$row->menu_sideone.'<br/>'.$row->menusidetwo.'<br/>'.$row->menudessert;
}
$calendar_info = array(
'month' => '12',
'year' => '2013',
// Then assign it into the appropriate place...
'data' => $data
);
You don't need to build the variable $data
just to throw away later. You could alternatively first define $calendar_info
with an empty array in $calendar_info['data']
then add keys in the foreach
loop as $calendar_info['data'][$row->menu_day]
.
Upvotes: 4