doumibi6899 2012-08-06 06:18
浏览 73
已采纳

将uri段传递给codeIgniter中的库

I'm working on a CodeIgniter project.. I want to pass a uri segment from a controller to my own library. I could not able to do it.. I tried it using session , but it won't work.please help me..

This is my controller,

<?php
class insertdata extends CI_Controller{

function __construct(){
    parent::__construct();
}

//#############################################################################

function updateprop(){

    $this->load->library('DataEntryForms_update');

            //get the uri segment
    $id = $this->uri->segment(3);

       //setting the session data
       $this->session->set_userdata('id',$id);

     $params [] = $this->uri->segment(3);


    $form_name = $this->input->post('function_name');
    switch ($function_name){

        case 1:

            $this->dataentryforms_update->function1();

            break;
        case 2:
            $this->dataentryforms_update->function2();
            break;

       }            
     }  
  }

This is my Library...

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

 class DataEntryForms_update{
protected  $CI;

 #######################################################
  //gets the CI instance    
  function __construct(){

$this->CI =& get_instance();
  }


 #############################################################

   function function1($params){

 $this->params = $params;
 print_r ($this->params);

 $project_id = $this->CI->session->userdata('id');
 echo $project_id;
 die();
    }   

}

In Library I tried to get the session data,but I'm get the session data 'id' as 0 ... When I used uri->segemnt(2) . it will get the correct value, but uri->segment(3) did not work ...please help me

  • 写回答

2条回答 默认 最新

  • douxigai8757 2012-08-06 07:19
    关注

    To access CodeIgniter's native resources within your library use the get_instance() function.

    You must have load the session library into your newly created library. You should have to include the session library into your own library. You could use the following code into your function where you are trying to access the session variable.

    $CI =& get_instance();
    
    $CI->load->helper('url');
    $CI->load->library('session');
    

    This post will surely help you to make use of sessions in your library.

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

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?