本文介绍了关系数据库和CodeIgniter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用CodeIgniter构建一个网站,我想从数据库表中显示一个建设项目的列表,我们将简单地调用 project_table 。对于每个项目,我还有一个地址,存储在另一个表中, address_table ,每个地址有一个 project_id



我在我的项目模型中创建了一个函数get_projects,用于获取项目信息并将其传递给项目视图,例如例如:

  public function index(){
$ data ['projects'] = $ this-> project_model - > get_projects();
$ data ['title'] ='Ejendomme';
$ this-> load-> view('templates / header',$ data);
$ this-> load-> view('projects / index',$ data);
$ this-> load-> view('templates / footer');我的问题是我如何得到地址读取,链接到正确的项目,并显示。我想我可以做一个函数,从视图调用,加载基于 project_id 的地址,但是据我理解,这是真的不好的做法。是否有一种方法从控制器调用 get_address 函数,并将其传递给视图,而不会丢失哪个地址属于哪个项目的跟踪?



更新:
此处的每个请求是函数 get_project()信息从数据库。我已经考虑调用 get_address()函数里面,但我不知道如何从函数返回地址。

  //从数据库读取所有项目的函数
public function get_projects(){
$ query = $ this-> db-> get ('project_table');
return $ query-> result_array();
}


解决方案

ve发布了模型的get_projects方法。无论如何,诀窍是利用模型 - 视图 - 控制器(MVC)架构,因此你把从数据库中选择的模型。
下面是一个从这两个表中提取数据的方法的示例:

  public function get_projects b $ b {
//标准mySQL
//只选择您需要的db字段
$ query =SELECT pt。*,at。* FROM project_table as pt,address_table as at WHERE pt.project_id = at.project_id;
$ db_result = $ this-> db-> query($ query);
$ result_object = $ db_result-> result();
/ *
这里你可以为结果添加一个检查(例如检查返回是否为空)
* /
return $ result_object;
}

现在将结果解析为视图,然后与数据一起播放。 / p>

I'm using CodeIgniter to build a website, and I want to show a list of construction projects from a database table, which we will simply call project_table. For each project I also have an address, stored in another table, address_table, each address has a project_id, which links it to a project.

I have made a function, get_projects, in my projects model, which is used to get the project information and pass it to the project view, like such:

public function index() {
    $data['projects'] = $this->project_model->get_projects();
    $data['title'] = 'Ejendomme';
    $this->load->view('templates/header', $data);
    $this->load->view('projects/index', $data);
    $this->load->view('templates/footer');
}

My question is how I get the addresses read, linked to the correct projects, and shown. I suppose I could make a function which is called from the view, which loads the address based on project_id, but as I understand it, this is really bad practice. Is there a way to call a get_address function from the controller, and pass it on to the view, without losing track of which address belongs to which project?

Update:Per request here is the function get_project(), which gets the project information from the database. I have considered calling a get_address() function inside this, but I am not sure how I would return the addresses from the function.

// Function to read all projects from database
public function get_projects() {
    $query = $this->db->get('project_table');
    return $query->result_array();
}
解决方案

Was more useful if you've posted the get_projects method from models. Anyway, the trick is to make use of Model-View-Controller(MVC) architecture, therefore you put into the model the selection from database.Here is an example with a method to extract your data from those two tables:

public function get_projects()
{
//for standard mySQL
//select only the db fields that you need
$query = "SELECT pt.*, at.* FROM project_table as pt, address_table as at WHERE pt.project_id = at.project_id";
$db_result = $this->db->query($query);
$result_object = $db_result->result();
/*
here you can add a check for the result (for instance to check if the return is not empty)
*/
return $result_object;
}

Now parse the result to a view and play from there with the data.

这篇关于关系数据库和CodeIgniter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 22:19
查看更多