douzhanglun4482 2018-08-08 10:22
浏览 13

数据库Codeigniter查询

I have a problem with extracting data from the database only for a given record in Code Igniter.

In my application I have domains to which I can add my comments - everything is overwhelmed.

I can add, edit, delete domains and notes.

However, when I step in on whatever domain card appears, all notes and I would like to display only those for the selected domain.

I think the problem is on the side of the SQL query - which must compare two tables: the id from the domains table and the id_rel_domain from the notes table.

However, I have no idea how to embrace it.

I have a linked relationship in the SQL FOREIGN KEY on ALTER TABLE database: notes.id_domain_rel with domains.id.

My code (CodeIgniter)

My controller:

public function get($id = false)
{
    $result = $this->notes_model->get($id);
    echo '{"records":' . json_encode( $result ) . '}';
}

My models:

public function get( $id = false)
    {
        if ( $id == false ) {
            $q = $this->db->get('notes');
            $q = $q->result();
        }
        else{
            $this->db->where('id', $id);
            $q = $this->db->get('notes');
            $q = $q->row();
        }
        return $q;
    }

The second condition is needed to edit notes - because it gets their ID. The first one, however, is displayed all notes for all domains.

In my opinion, I must add:

$this->db->where('notes.id_domain_rel', $domain.id);

However, I miss the idea of ​​how to do it.

Gets data for all domains, not for one I have a view of.

  • 写回答

2条回答 默认 最新

  • dsasd12314 2018-08-08 10:37
    关注

    This might be a problem with the condition checking. try using === in the if statement. or use NULL instead of boolean values.

     public function get( $id = false)
            {
                if ( $id === false ) {
                    $q = $this->db->get('notes')->result();
                }
                else{
                    $q = $this->db->where('id', $id)->get('notes')->row();
                }
                return $q;
            }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度