简体   繁体   English

检查数据库记录是否存在Codeigniter

[英]Check if database record exists Codeigniter

I have a project and have run into a problem. 我有一个项目,遇到了问题。 I use Codeigniter HMVC and therefore build modules. 我使用Codeigniter HMVC,因此构建模块。 I appreciate your help. 我感谢您的帮助。 Thanks. 谢谢。

I have two module methods where one calls another. 我有两个模块方法,其中一个调用另一个。 The other delivers a database result and loads it into a view that is then passed on to the template from the 1st module method. 另一个提供数据库结果并将其加载到视图中,然后从第一个模块方法传递到模板。 I now realize, that since I get a view and not data from the 2nd module, I don't see how I can check if there is a database record returned, and I don't want another database call just for that purpose. 我现在意识到,由于我从第二个模块中获得了视图而不是数据,所以我看不到如何检查是否返回了数据库记录,因此我不希望为此目的而进行另一个数据库调用。 The seperation between "item" and "description" modules is required. 需要在“项目”和“描述”模块之间进行分隔。 Having data sent instead would make a really messy structure that to my understanding would counteract the HMVC structure. 相反,发送数据将构成一个非常混乱的结构,据我所知,它将抵消HMVC结构。 I therefore need to keep this structure. 因此,我需要保持这种结构。 How can I check if database record exists and only run the 1st method if it does? 如何检查数据库记录是否存在,并且仅运行第一种方法? Please advise. 请指教。 Thanks 谢谢

1st module (controller method) called through URL 通过URL调用的第一个模块(控制器方法)

function view_item($item_id) 
{    

    $data['item_id'] = $item_id;       
    $data['item'] = $this->item;  
    $data['item_group'] = $this->item_group;  
    $data['content_module'] = Modules::run("descriptions/view",$item_id); 
    $data['active_menu_item'] = "descriptions";
    $data['view_file'] = "handle_view";
    $data['module'] = $this->module;
    $template = "application";      
    $this->load->module('templates');
    $this->load->templates->$template($data); 

}

2nd module method called by 1st module method 第一模块方法调用第二模块方法

function view($item_id)
{
    $data['title'] = "View description";
    $data['page_title'] = "View description";
    $data['page_subtitle'] = "This item";              
    $data['item_id'] = $item_id;
    $array = array('item_id'=> $item_id,'group_id'=> $this->group_id);
    $object = $this->get_where_custom($array);
    $num_rows = $object->num_rows;
    $descriptions =  $object->result();
    $data['description'] =  $descriptions[0];
    $this->load->view('view_view',$data); 

} 
if(isset($data['content_module']))

Would be an alternative. 将是一个替代方案。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM