我从数据库表Games_Model()
中的模型match_schedule
获取匹配项,模型100%正常工作没问题。然后,我将模型方法getMatchInfo()
传递给视图,并将其分配给$data
变量$data['games']
,该变量作为view
方法中的参数传递
$data['games'] = $this->games_model->getUpcomingGames();
$this->load->view('templates/upcoming_fixtures_tbl', $data);
输出
当我在print_r($games)
中templates/upcomg_fixtures.php
时,我得到以下内容:
Array (
[0] => stdClass Object
(
[gameID] => 380
[weekNum] => 1
[gameTimeEastern] => 2018-06-14 18:00:00
[homeID] => Russia
[homeScore] =>
[visitorID] => Saudi Arabia
[visitorScore] =>
[overtime] =>
[tournament] => Fifa World Cup
[sport] => soccer
[location] => Luzhniki Stadium, Moscow
)
[1] => stdClass Object
(
[gameID] => 123
[weekNum] => 1
[gameTimeEastern] => 2018-09-06 20:20:00
[homeID] => Philadelph
[homeScore] =>
[visitorID] => Atlanta Falcons
[visitorScore] =>
[overtime] =>
[tournament] => NFL
[sport] => AM Football
[location] => Lincoln Financial Field
)
[2] => stdClass Object
(
[gameID] => 81
[weekNum] => 14
[gameTimeEastern] => 2018-05-18 00:00:00
[homeID] => Hurricanes
[homeScore] => 0
[visitorID] => Reds
[visitorScore] => 0
[overtime] =>
[tournament] => Super Rugby
[sport] => rugby
[location] => Westpac Stadium
)
)
问题
但是,一旦我尝试使用$games
变量,我就会收到错误
foreach ($games as $game){
echo $game['gameID'];
}
致命错误:无法使用stdClass类型的对象作为数组
问题
如何访问游戏变量?
相关代码如下:
class Users extends CI_Controller {
public function dashboard($page = 'dashboard')
{
if(!file_exists(APPPATH . '/views/users/' . $page . '.php')){
show_404();
}
//HERE DATA GETS PASSED AS ARRAY
$data['games'] = $this->games_model->getUpcomingGames();
$data['title'] ='WELCOME USER';
$this->load->view('templates/header', $data);
$this->load->view('users/' . $page, $data);
$this->load->view('templates/upcoming_fixtures_tbl', $data);
$this->load->view('templates/footer', $data);
模板/ upcomg_fixtures.php
foreach ($games as $game)
{
echo $game['gameID'];
}
结果
致命错误:无法使用stdClass类型的对象作为数组
视图
如何在我的视图中访问数组索引?
在帮助,建议,建设性批评中表示赞赏。
答案 0 :(得分:1)
希望这会对您有所帮助
func retrieveMonthlySpent(month:String, year:String) -> Double {
FirebaseFunctions().retrieve(from: .expense, username: self.username as! String, returning: Expenses.self) { (expenses) in
self.monthlyExpenses = expenses
}
var sum:Double = 0
for expense in self.monthlyExpenses{
if expense.modificationDate.convertToMonth() == month && expense.expense && expense.modificationDate.convertToYear() == year {
sum += expense.convertedAmount
}
}
return sum
}
是一个对象数组
在您的视图func retrieveMonthlySpent(month:String, year:String) -> Double{
FirebaseFunctions().retrieve(from: .expense, username: self.username as! String, returning: Expenses.self) { (expenses) in
self.monthlyExpenses = expenses
var sum:Double = 0
for expense in self.monthlyExpenses{
if expense.modificationDate.convertToMonth() == month && expense.expense && expense.modificationDate.convertToYear() == year{
sum += expense.convertedAmount
}
}
}
return sum
}
中执行此操作:
$games