0

如果之前有人问过,请原谅我,但我找不到要搜索的相关关键字。我将尝试解释我的问题。

我有名为“ilan”和“places”的数据库表。在“ilan”表中,有 3 列保存地点的 ID - 城市、地区、街道。

我想了解如何在视图中打印 ilan.name 和相关的 places.name。

我有一个控制器,如:

class Estate_control extends CI_Controller{

public function _construct(){
    parent::_construct();   
}

function index(){

    $this->load->helper('url');
    $this->load->model("estate_model");

    $data['tum_bayiler'] = $this->estate_model->tum_bayileri_listele();
    $data['sonEmlaklar'] = $this->estate_model->sonEmlaklar();      

    $this->load->view("index",$data);

}
}

我有这样的模型:

class Estate_model extends CI_Model{

function __construct(){
        parent::__construct();
}

function sonEmlaklar(){
    $query = $this->db
            ->select()
    ->from("ilan")
    ->order_by("gunceltarih DESC")
    ->limit(36)
    ->get();

    return $query->result();            
}

}

我的数据库表:

宜兰:

ilan_id, gunceltarih, ... , ilan_city(places 表中的 ID), ilan_district(places 表中的 ID), ilan_street(places 表中的 ID), ...

地方:

地点 ID,地点名称,...

关于如何在视图中打印 ilan.name 和相关 places.name的任何想法。

感谢大家。

4

2 回答 2

1

我认为这是代码的相关部分

$query = $this->db
        ->select("ilan.*, places.name")
->from("ilan")
->join('places', 'places.id = ilan.ilan_city OR places.id = ilan.ilan_district OR places.id =ilan.ilan_street')
->order_by("gunceltarih DESC")
->limit(36)
->get();
于 2012-05-23T17:44:57.580 回答
0

我找到了出路。我不知道它的效率如何,但它对我有用。

我在控制器的索引函数中创建了一个函数,如下所示:

function index(){

    //whatever here

function getPlace($e){
    $ci = get_instance(); //to get properties of parent class
    $query = $ci->estate_model->ilanSehir($e);


    return $query;
}   

    //whatever here

}

然后我在我的模型中放置了一个函数,如下所示:

function ilanSehir($e){ 
$query = $this->db->select("*")
    ->from("places")
    ->where("places_id",$e)
    ->get();
 return $query->row();    //This will return one row so it must be row() instead of result()    

}

在我看来,我用我想要的参数调用了函数:

<?php $place = getPlace($ilan->ilan_city);
      echo $place->places_name;
?>

正如我之前所说,我不知道它的效率如何,但它对我有用。希望能帮助到你。如果我在某个地方错了,请警告我。

谢谢所有回答的人

于 2012-05-24T01:21:35.877 回答