【问题标题】:How do i call the function I created in my Model on the view如何在视图上调用我在模型中创建的函数
【发布时间】:2013-03-16 00:25:23
【问题描述】:

我刚刚在模型中创建了这个函数来查看我在我的社交网络中关注的人...我如何在视图中调用它??

function isfollowing($following){

        $user_id = $this->session->userdata('uid');

        $this->db->select('*');    
        $this->db->from('membership');
        $this->db->join('following', "membership.id = following.tofollow_id");
        $this->db->where("tofollow_id","$following");
        $this->db->where("user_id", "$user_id");


        $q = $this->db->get();      



    if($q->num_rows() > 0) {
        return "yes";
    } else {
        return "no"; 
    }


}   

现在在我的视图中,我如何称呼它,因为我已经创建了一个函数来获取当前登录用户的 id 并且等于 $r->id

我在这里怎么称呼它? if 语句中的“==”后面是什么?

风景

<?php if ( $r->id == ): ?>

【问题讨论】:

    标签: php database codeigniter view model


    【解决方案1】:

    从视图调用模型函数不是一个好习惯。 关于它有一些替代方案。你可以使用任何你喜欢的人。

    第一

    当您加载视图时,调用您的模型函数并将其传递给变量 比这个变量将被传递给视图。

    控制器

    $following_status   =   $this->my_model->isfollowing($following);
    
    $data['following_status']   =   $following_status;
    
    $this->load->view('my_view',$data);
    

    查看

    <p>$following_status</p>
    

    第二

    如果你想独立于模型,你可以创建助手,你可以 在应用程序的任何地方使用。您必须创建一个 CI 实例才能 让它工作。

    custom_helper.php

    function isfollowing($following)
    {
        $CI =   get_instance();
    
        $user_id = $CI->session->userdata('uid');
    
        $CI->db->select('*');    
        $CI->db->from('membership');
        $CI->db->join('following', "membership.id = following.tofollow_id");
        $CI->db->where("tofollow_id","$following");
        $CI->db->where("user_id", "$user_id");
    
        $q = $CI->db->get();      
    
        if($q->num_rows() > 0) {
            return "yes";
        } else {
            return "no"; 
        }
    }  
    

    查看

    //load the custom helper before using it (you can autoload of in autoload.php)
    //or use common way $this->load->helper('custom');
    <p>isfollowing($yourparameter)</p>
    

    【讨论】:

      【解决方案2】:

      您执行以下操作:

      (1) 将模型加载到创建页面的控制器中或自动加载

      (2) 在您的视图中,输入如下内容:

      $this->The_custom_model->isfollowing($theinputvariable)
      

      其中The_custom_model 是您定义isfollowing() 函数的模型。

      $theinputvariable 是您的函数的适当参数值。请记住,您已将对象指定为函数的参数,因此您需要考虑这一点。

      【讨论】:

        【解决方案3】:

        这是 raheel 发布的显示 if 检查的修改版本 - 可能不是您的问题所必需的,但可以让您考虑一些事情......

         // check to see if anything come back from the database?
         if ( ! $data['following_status'] = $this->my_model->isfollowing($following) ) {  
        
         // nothing came back, jump to another method to deal with it
          $this->noFollowers() ; }
        
         // else we have a result, and its already set to data, so ready to go
         else {
        
           // do more here, call your view, etc 
         } 
        

        即使网页正在运行,数据库也可能会出现故障,因此养成检查结果的习惯是件好事。您可以在控制器和模型中进行的错误检查越多,您的视图文件就会越干净。

        【讨论】:

          【解决方案4】:

          要将模型访问到您的视图中,您首先将其加载到这样的自动加载文件中

             $autoload['model'] = array('model_name');
          

          那么在视图中你可以通过使用这行代码得到它

               $this->model_name->isfollowing($following)
          

          isfollowing 中,您将传递您的 tofollow_id

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-11-28
            • 1970-01-01
            • 2014-03-06
            • 2013-04-26
            • 1970-01-01
            • 2011-11-18
            • 2018-07-30
            • 1970-01-01
            相关资源
            最近更新 更多