douwen2158 2013-03-06 23:41
浏览 68
已采纳

CI - 将变量从1个控制器传递给两个模型/视图

Can anyone explain why I cannot get a variable called $siteID to pass to another function within the same controller?

In the third function called "get_orders_by_site" I have loaded a different model, which returns information about 'orders' raised at the currently viewed building/site/property.

The sites controller works perfectly, first function lists a table with all my properties, then when one is clicked - the second function gets the siteID of that selection, and returns with further 'detail/data' - sites controller function1/2 all relate to the same model, and return information from the SAME table.

I'm trying to implement a third function, which will do a similar task, but return with information/data from a different table (the site.siteID, is also a FK in the orders.siteID table i've created in phpmyadmin).

If I need to explain further please let me know - Many thanks!

amended code

Sites Controller

<?php
class Sites extends CI_Controller {

//Searches for a list of sites
public function search($sort_by = 'site_title', $sort_order = 'asc', $offset = 0)
{
$limit = 20;
$data['columns'] = array(
    'site_title' => 'Site Name',
    'site_uprn' => 'Unique Property Reference'
);
$this->load->model('site_model');
    $results = $this->site_model->get_sites($limit, $offset, $sort_by, $sort_order);

    $data['sites'] = $results['rows'];
    $data['num_results'] = $results['num_rows'];


    //pagination for list returned

    $this->load->library('pagination');
    $config = array ();
    $config['base_url'] = site_url("Sites/search/$sort_by/$sort_order");
    $config['total_rows'] = $data['num_results'];
    $config['per_page'] = $limit;
    $config['uri_segment'] = 5;
    $this->pagination->initialize($config);
    $data['pagination'] = $this->pagination->create_links();

    $data['sort_by'] = $sort_by;
    $data['sort_order'] = $sort_order;

    $this->load->view('search', $data);

}

//Displays individual site details

//passes selected siteID to the model, and returns only database/info for that particular building/property
public function details($siteID){
$this->load->model('site_model');

    $data['site']=$this->site_model->get_site($siteID);

    $this->load->view('site', $data);
    $this->load->view('orders', $data);     
}

// this second function should do a similar method as above, however I've loaded a different model, as i'm getting information from a different database table - but I still want the data returned to be limited by the building/site ID which the user selects.

public function orders_by_site($siteID, $sort_by = 'orderID', $sort_order = 'asc', $offset = 0)
    {   
        $this->load->model('site_model');
        $this->load->model('order_model');
        $limit = 20;
        $data['columns'] = array(
            'orderID' => 'Order No.',
            'initiated_date' => 'Initiated Date',
            'target_date' => 'Target Date',
            'status' => 'Status',
            'priority' => 'Priority',
            'trade_type' => 'Trade Type'
        );



            $results = $this->site_model->get_site($siteID);
            $results = $this->order_model->get_orders($siteID, $limit, $offset, $sort_by, $sort_order);

            $data['orders'] = $results['rows'];
            $data['num_results'] = $results['num_rows'];


    //pagination for orders table

        $this->load->library('pagination');
        $config = array ();
        $config['base_url'] = site_url("Orders/orders_by_site/$sort_by/$sort_order");
        $config['total_rows'] = $data['num_results'];
        $config['per_page'] = $limit;
        $config['uri_segment'] = 6;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();

        $data['sort_by'] = $sort_by;
        $data['sort_order'] = $sort_order;


        $this->load->view('orders', $data);
}
}       

End Sites Controller

Site Model

//Get all site data

function get_sites($limit, $offset, $sort_by, $sort_order){

    $sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
    $sort_columns = array('site_title', 'site_uprn');
    $sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'site_title';

     $q = $this->db->select('siteID, site_title, site_uprn')
        ->from('sites')
        ->limit($limit, $offset)
        ->order_by($sort_by, $sort_order);

    $ret['rows'] = $q->get()->result();

     //count query for sites

    $q = $this->db->select('COUNT(*) as count', FALSE)
        ->from('sites');

    $tmp = $q->get()->result();

    $ret['num_rows'] = $tmp[0]->count;

    return $ret;
    }

//Get individual site data

function get_site($siteID){

    $this->db->select()->from('sites')->where(array('siteID' => $siteID));
    $query = $this->db->get();
    return $query->first_row('array');
    }

}

Orders Model

//order table

function get_orders($siteID, $orderID, $limit, $offset, $sort_by, $sort_order){

    $sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
    $sort_columns = array('orderID', 'initiated_date', 'target_date','completion_date','status','priority','total_amount','job_description','requestor_name','requestor_telno','trade_type');
    $sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'orderID';

    $q = $this->db->select()->from('orders')->where(array('siteID' => $siteID))->limit($limit, $offset)->order_by($sort_by, $sort_order);

    $ret['rows'] = $q->get()->result();
    }

//order details
function get_order($orderID){

$this->db->select()->from('orders')->where(array('orderID' => $orderID));
$query = $this->db->get();
return $query->first_row('array');
}

}

Site View - only showing the extract where I'm trying to embed the orders view

<h5>Order Details</h5>
 <?php include('orders.php')?>
 </div>  
      </div>
</div>

Orders View

 <div id="site_filter">
  <div class="result_counter">
  <h5>Found <?php echo $num_results; ?> Orders</h5>
  </div>
   <div class="pagination">
   <?php if(strlen($pagination)): ?>
Page: <?php echo $pagination; ?>
    <?php endif; ?>
    </div>  
  </div>
  <div class="clear_float"></div>

    <table class="table">
    <thead>
        <?php foreach($columns as $column_name => $column_display): ?>
        <th <?php if ($sort_by == $column_name) echo "class=\"sort_$sort_order\"" ?>>
            <?php echo anchor("Sites/orders_by_site/$column_name/" .
                (($sort_order == 'asc' && $sort_by == $column_name) ? 'desc' : 'asc') ,
                $column_display); ?>
        </th>
        <?php endforeach; ?>
    </thead>

        <tbody>
        <?php foreach($orders as $order): ?>
        <tr>
            <td><a href="<?php echo base_url(); ?>Orders/order_detail/<?php echo $order->orderID; ?>"><?php echo $order->orderID; ?></a></td>
            <td><?php echo $order->initiated_date; ?></td>
            <td><?php echo $order->target_date; ?></td>
            <td><?php echo $order->status; ?></td>
            <td><?php echo $order->priority; ?></td>
            <td><?php echo $order->trade_type; ?></td>
        </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
  • 写回答

2条回答 默认 最新

  • duaedf6509 2013-03-22 09:46
    关注

    @swatkins -

    Thank you very much for your help, you highlighted some issues I had overlooked - I've gone about this in a different fashion now.

    Originally I was trying to use Active Record (i believe) to select data from one table, based on the selection of a record from a different table - and then pass this selection to another model and use it in a get_where statement.

    I've managed to get this to work using the $this->uri->segment() method in my controller, and then passing this to the corresponding model.

    Now I'm able to utilise the user selection of a 'building/propery' name, with it's address etc - and then I have a second model, which retrieves the 'orders/jobs' that have been raised at that building.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测