【问题标题】:Get values from different tables with codeigniter into a single array?使用codeigniter从不同表中获取值到单个数组中?
【发布时间】:2013-08-16 14:26:31
【问题描述】:

假设我的数据库中有两个表:sitessitetype1

sites 具有以下列:

Slug Name Url

slug1Site1site1.com

slug2Site2site2.com

sitetype1 具有以下列:

Slug Description

slug1Description for site1

slug2Description for site2

在 codeigniter 中,理想情况下我需要获取这些值,以便它们驻留在 name => slug 对中。

所以我试图得到一个看起来像这样的数组:

['Site1' => 'slug1', 'Site2' => 'slug2']

以前,我通过一种效率极低的方法来完成此操作,即从sitetype1 表中获取 slug,然后循环遍历这些数据并从sites 数组中获取相应的name 值。这似乎是一种过于复杂的处理方式。我正在查看连接以完成任务,但我对 MySQL 非常不满意,需要一些帮助。

我试过了:

$this->db->select('name','slug');
$this->db->from('sites');
$this->db->join('sitetype1', 'sitetype1.slug = sites.slug','inner');

但是它只返回相应的name 值。从这里开始有人能帮我吗?

这是它返回的示例:

array(2) {
  [0]=>
  object(stdClass)#22 (1) {
    ["name"]=>
    string(5) "Site1"
  }
  [1]=>
  object(stdClass)#23 (1) {
    ["name"]=>
    string(5) "Site2"
  }
}

【问题讨论】:

  • 您无法获得slug 列,对吧?
  • 你试过 $this->db->select('name,slug');或 $this->db->select('name,sites.slug as newSlug');

标签: php mysql codeigniter activerecord


【解决方案1】:

你应该使用like ;)

$this->db->select('sites.name, sitetype1.slug');

错误: 'name','slug'

右: 'name, slug'

【讨论】:

  • 你几乎是正确的,我发现这是正确的语法:$this->db->select('name, sitetype1.slug');
猜你喜欢
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-06
  • 1970-01-01
  • 1970-01-01
  • 2021-11-10
相关资源
最近更新 更多