如何将值传递给自定义帮助程序。

时间:2017-03-19 12:47:56

标签: php codeigniter

这里我有一个帮助文件。我使用这个帮助文件进行会话登录。使用这个助手我可以静态获得价值。但我希望动态获得价值。意味着我想将值从控制器传递给帮助器。

这是我的帮助代码

<?php

session_start();
include 'connect_db.php';
$con = $_SESSION['connection'];

$p1 = mysqli_real_escape_string($con, $_GET['p1']);

$query = "SELECT * FROM class WHERE id_class = '$p1'";
$result = mysqli_query($con, $query) or die('Error, query failed');
$num_result = mysqli_num_rows($result);

$id_class = mysqli_real_escape_string($con, $_POST['id_class']);
$id_user = mysqli_real_escape_string($con, $_SESSION['id_user']);

if ($num_result > 0)
{
    $insert_query = "UPDATE user_program SET id_class = '$id_class' WHERE id_user = '$id_user'";

    $insert = mysqli_query($con, $insert_query) or die('Error,query failed');


    if ($insert)
    {
        echo '<html><meta charset="UTF-8"><script language="javascript">alert("OK!"); document.location="add_classes_form.php";</script></html>';
    }
    else
    {
        echo '<html><meta charset="UTF-8"><script language="javascript">alert("Not OK.")</script></html>';
        echo '<script language="javascript"> document.location="add_classes_form.php";</script>';
        exit();
    }   
}
?>

这是控制器代码

<?php
         if ( ! defined('BASEPATH')) exit('No direct script access allowed');

            if ( ! function_exists('get_user_details')){
                    function get_user_details($user_id){
                       //get main CodeIgniter object
                       $ci =& get_instance();

                       //load databse library
                       $ci->load->database();


                       //get data from database
                       $query = $ci->db->get_where('user_profile',array('id'=>$user_id));

                       if($query->num_rows() > 0){
                           $result = $query->row_array();

                           return $result;
                       }else{
                           return false;
                       }
                   }
            }

    ?>

1 个答案:

答案 0 :(得分:2)

在你的controller尝试这样......

function index(){
$this->load->helper('login');
    $id = 1;
    $data = get_user_details($id);
    if($count($data)>0)
      {
        print_r($data); //prints array of records 
      }
    else{
        echo "No record found!!";
      }

}