2013-03-16 69 views
0

我刚刚在模型中创建了此函数,以查看我的社交网络中的谁将跟随......我如何在视图中调用它?如何在视图中调用我在模型中创建的函数

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语句中的“==”后面是什么?

THE VIEW

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

回答

0

你做到以下几点:

(1)导入模型中创建您的网页或自动负载控制器,它

(2)在你看来,键入类似:

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

其中The_custom_model是模型,其中y ou定义了isfollowing()函数。

$theinputvariable是您的函数适当的参数值。请记住,你已经指定了一个对象作为你的函数的参数,所以你需要考虑这个。

2

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

首先

当你加载一个观点叫你的模型功能,并将它传递一个变量 比这个变量将被传递给查看。

控制器

$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> 
0

这是一个修正版本,以什么raheel贴表示,如果检查 - 可能没有必要为你的问题,而是给你一些东西想想...

// 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 
} 

即使网页正在运行,数据库也可能会停止很好的养成检查结果的习惯。在控制器和模型中可以执行的错误检查越多,视图文件就越干净。

0

要访问模型到你的观点,你首先将其加载到这样

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

自动加载的文件,然后在视图中,您可以在isfollowing通过使用此行代码

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

得到它你会通过你的tofollow_id

相关问题