代码点火器显示表的多个结果取决于另一个表ID

时间:2017-06-28 08:59:46

标签: codeigniter-3

我有2个表名 tbl_restaurant tbl_restaurant_cuisines 。我想要做的是我想要 tbl_restaurant 的结果,以及与 tbl_restaurant 相关的 tbl_restaurant_cuisines 的结果。

下面是两个表之间的关系

tbl_restaurant.id = tbl_restaurant_cuisines.restaurat_id

我想做什么

每家餐厅都有多种美食可供展示。

就像我们从餐厅桌上得到餐厅一样

海豚(tbl_restaurant)那么菜肴将是意大利菜,中国菜,印度菜(tbl_restaurant_cuisines)喜欢这个

餐厅美食桌有像

这样的数据
  

Image of the tbl_restaurant_cuisines

     

Image of the tbl_restaurant

其中restaurant_id是tbl_restaurant的id

我想使用Code Igniter显示此类结果。

我所做的一直是显示tbl_restaurant_cuisines的最后结果。

我的代码

模型

//Function for getting the restaurant
    public function get_restaurant_results($search_suburb)
    {
        //$this->db->_protect_identifiers=false; 
        $query = $this->db->from('tbl_restaurant as tr, tbl_restaurant_servicearea as trs')
                          ->where('tr.id = trs.restaurant_id')
                          ->where('trs.servicearea_suburb', $search_suburb)
                          ->where('tr.status', 'approved')
                          ->get();
        //SELECT * FROM tbl_restaurant as tr, tbl_restaurant_servicearea trs WHERE tr.id = trs.resturant_id and trs.servicearea_suburb = '753010 - Cuttack';
        //$query = $this->db->query("SELECT * FROM tbl_restaurant as tr, tbl_restaurant_servicearea trs WHERE tr.id = trs.restaurant_id and trs.servicearea_suburb = '".$search_suburb."' and tr.status = 'approved'");
        $result = $query->result_array();
        return $result;
    }

    //Function for getting cuisin from searched restaurant
    public function get_restaurant_cuisines($restaurant_id)
    {
        $query = $this->db->where('restaurant_id= '.$restaurant_id.'')
                          ->get('tbl_restaurant_cuisines');

        //echo $restaurant_id;
        //$query = $this->db->query("SELECT * FROM tbl_restaurant_cuisines WHERE restaurant_id = $restaurant_id");
        $result = $query->result_array();
        return $result;
    }

控制器

public function restaurant_search()
    {
        if($this->session->userdata('username'))
        {
            $q = $this->userModel->getUserData($this->session->userdata('username')); 
            $data1 = $q[0];
            $data = array(
                'title'     =>'Restaurant Results',
                'fname'     =>$data1['fname'],
            );
        }
        else
        {
            $data = array(
                'title'     =>'Restaurant Results',
            );
        }
        $this->form_validation->set_rules('servicearea_suburb', 'Enter Pincode/ Suburb', 'required');

        if($this->form_validation->run() == FALSE)
        {
            // fails
            $data['title'] = 'Home Page';
            $this->load->view('header', $data);
            $this->load->view('home');
            $this->load->view('footer');
        }
        else
        {
            $search_suburb = $this->input->post('servicearea_suburb');
            $data['restaurants']  = $this->searchRestaurantModel->get_restaurant_results($search_suburb);

            //For Cuisin ID
            $res_cuisin = $data['restaurants'];
            foreach($res_cuisin as $res_id)
            {
                $data['cuisines'] = $this->searchRestaurantModel->get_restaurant_cuisines($res_id['restaurant_id']);
            }
            $data['servicearea_suburb'] = $search_suburb;
            $data['title'] = 'Restaurant Results';
            $data['count'] = count($data['restaurants']);
            $this->load->view('header', $data);
            $this->load->view('restaurants');
            $this->load->view('footer');
        }
    }

查看

<?php
foreach($restaurants as $restaurant)
{
    echo $restaurant['restaurant_id'];
    echo $restaurant['restaurant_name']; 
    foreach($cuisines as $cuisine)
    {
        echo $cuisine['dealsin'];
    }
}
?>

我想在CI中提供这种结果,就像下面提供的核心PHP一样。

<?php
$con = mysqli_connect("localhost","root","", "restaurant");
$sql = "SELECT * FROM tbl_restaurant where status='approved'";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result))
{
    $id = $row['id'];
    echo $name = "<h1>".$row['restaurant_name']."</h1><br>";
    $sql2 = "SELECT * FROM tbl_restaurant_cuisines WHERE restaurant_id = '".$id."'";

    $result2 = mysqli_query($con, $sql2);
    while($row2 = mysqli_fetch_array($result2))
    {   
        echo $cuisine = $row2['dealsin']."<br>";
    }
}
?>

1 个答案:

答案 0 :(得分:1)

将您的restaurant_search功能替换为您的控制器,如下所示

public function restaurant_search()
{
    if($this->session->userdata('username'))
    {
        $q = $this->userModel->getUserData($this->session->userdata('username')); 
        $data1 = $q[0];
        $data = array(
            'title'     =>'Restaurant Results',
            'fname'     =>$data1['fname'],
        );
    }
    else
    {
        $data = array(
            'title'     =>'Restaurant Results',
        );
    }
    $this->form_validation->set_rules('servicearea_suburb', 'Enter Pincode/ Suburb', 'required');

    if($this->form_validation->run() == FALSE)
    {
        // fails
        $data['title'] = 'Home Page';
        $this->load->view('header', $data);
        $this->load->view('home');
        $this->load->view('footer');
    }
    else
    {
        $search_suburb = $this->input->post('servicearea_suburb');
        $get_restorent  = $this->searchRestaurantModel->get_restaurant_results($search_suburb);

        $restaurants = array();
        if(sizeof($get_restorent))
        {
          foreach($get_restorent as $res)
          {
            $temp = $res;
            $temp['cuisines'] = array();
            $get_cus = $this->searchRestaurantModel->get_restaurant_cuisines($res['restaurant_id']);
            if(sizeof($get_cus))
            {
              $temp['cuisines'] = $get_cus;
            }
            array_push($restaurants, $temp);
            $temp = array();
          }
        }

        $data['servicearea_suburb'] = $search_suburb;
        $data['title'] = 'Restaurant Results';
        $data['restaurants'] = $restaurants;
        $data['count'] = sizeof($restaurants);
        $this->load->view('header', $data);
        $this->load->view('restaurants');
        $this->load->view('footer');
    }
}

在您的视图页面测试中使用以下代码

<table border="1" cellpadding="5" cellspacing="5">
  <thead>
    <tr>
      <th>Restaurent ID</th>
      <th>Restaurent Name</th>
      <th>Available Cuisine</th>
    </tr>
  </thead>
  <tbody>
  <?php if(sizeof($restaurants)): foreach($restaurants as $res) ?>
    <tr>
      <<td><?=$res['id']?></td>
      <td><?=$res['restaurant_name']?></td>
      <td>
        <?php if(sizeof($res['cuisines'])): foreach($res['cuisines'] as $cus): ?>
          <?=$cus['dealsin'];?><br>
        <?php endforeach; else: ?>
          No Cuisines
        <?php endif; ?>
      </td>
    </tr>
  <?php endforeach; else: ?>
    <tr>
      <td colspan="3">Restaurent Not Found</td>
    </tr>
  <?php endif; ?>
  </tbody>
</table>

更新您的代码并告知我是否显示任何错误。