Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从其他数组向对象添加属性?_Php_Mysql_Codeigniter_Codeigniter 3 - Fatal编程技术网

Php 如何从其他数组向对象添加属性?

Php 如何从其他数组向对象添加属性?,php,mysql,codeigniter,codeigniter-3,Php,Mysql,Codeigniter,Codeigniter 3,在我的数据库中,我有3个表: 国家: 小组: 匹配项: 我需要在视图中显示球队和国家的名称 在控制器中,我获得一个匹配项并将其传输到视图: public function index() { $data = array(); $q = $this->db->get('matches'); $matches = $q->result(); $data['matches'] = $matches;

在我的数据库中,我有3个表:

国家:

小组:

匹配项:

我需要在视图中显示球队和国家的名称

在控制器中,我获得一个匹配项并将其传输到视图:

    public function index() {
        $data = array();

        $q = $this->db->get('matches');
        $matches = $q->result();
        $data['matches'] = $matches;

        $this->load->view('matches/index', $data);
    }
我有国家队和球队的ID,但我不知道如何得到名字

我应该连接表还是在数据库中添加3列名称

当我尝试联接表时:

$this->db->select('*');
$this->db->from('matches');
$this->db->join('countries', 'country.id = matches.country_id');
$this->db->join('teams', 'teams.id = matches.team_home_id');
$query = $this->db->get();
因此,我有:

array(1) { [0]=> object(stdClass)#24 (8) { ["id"]=> string(1) "1" ["country_id"]=> string(1) "1" ["league"]=> string(4) "test" ["team_home_id"]=> string(1) "1" ["team_away_id"]=> string(1) "3" ["date"]=> string(10) "15-04-2019" ["time"]=> string(5) "12:00" ["name"]=> string(5) "test1" } }
名称被覆盖。也许我的疑问是错的

如果我在添加第二个选项之前选择它,我必须得到名称,但我不知道如何

在控制器中,我有:

                $q = $this->db->get('countries');
                $countries = $q->result();
                $data['countries'] = $countries;

                $q = $this->db->get('teams');
                $teams = $q->result();
                $data['teams'] = $teams;

                data_match = array(
                    'country_id' => $this->input->post('country_id', true),
                    'country_name' => '',
                    'league' => $this->input->post('league', true),
                    'team_home_id' => $this->input->post('team_home_id', true),
                    'team_home_name' => '',
                    'team_away_id' => $this->input->post('team_away_id', true),
                    'team_away_name' => ''
                );
                $this->db->insert('matches', $data_match);
哪种选择更好?请提供一些提示。

不要“在数据库中添加3列名称”,因为这将违反规范化规则。 我认为为每一列使用别名可以完成以下工作:

$this->db->select('matches.*, home_team.name home_team_name, away_team.name away_team_name, country.name country_name'); // use alias for each away/home team name & country name to avoid ambiguous 'name' column
$this->db->from('matches');
$this->db->join('country', 'country.id = matches.country_id'); // changed from countries to country
$this->db->join('teams home_team', 'home_team.id = matches.team_home_id'); // use alias for the home team table
$this->db->join('teams away_team', 'away_team.id = matches.team_away_id'); // also use alias for the away team table
$query = $this->db->get();

我已将
国家/地区
表名更改为
国家/地区
,如果我错了,您可以将其还原。

很高兴我能提供帮助:)