CodeIgniter Forums
Need help better method - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: Need help better method (/thread-62398.html)



Need help better method - ichadhr - 07-09-2015

Hi, I'm new in this forum and need some advice regarding to solve my problem. Here I'm using CI 2.2.2 and have 2 controllers, in main controller (dashboard) I have code to get list of projects:

PHP Code:
class Dashboard extends MY_Controller {

 
   function __contruct()
 
   {
 
       parent::__contruct()
 
       $project_id = array('projectid' '');

 
       // remove project id session userdata
 
       $this->session->unset_userdata($project_id);
 
   }

 
   function index()
 
   {
 
       $this->load->model("content_model");

 
       // getting project list
 
       $this->data['projects'] = $this->content_model->get_content();

 
       // rendering view
 
       $this->_render_page('dashboard/index'$this->data);
 
   }


and here is my dashboard index view:

PHP Code:
<html>
<?
php foreach ($projects as $project):?>
    <?php echo anchor("projects/index/".$project->id'Project''title="List Project"');?>
<?php 
endforeach;?>
</html> 

above code will generate links to projects controller as shown below:

PHP Code:
<a href="http://me.com/projects/index/1" title="List Project">Project</a>
<
a href="http://me.com/projects/index/2" title="List Project">Project</a>
<
a href="http://me.com/projects/index/3" title="List Project">Project</a

1, 2, 3 ..etc is ids of projects, so in my second controller I set this ids to session userdata:

PHP Code:
class Projects extends MY_Controller {

 
   function __construct()
 
   {
 
       parent::__construct();
 
       $this->load->model("project_model");
 
   }

 
   function index($projectid NULL)
 
   {
 
       // checking for id
 
       if ($projectid !== null && !is_numeric($projectid))
 
       {
 
           header("refresh:4;url=".base_url('dashboard'));
 
           return show_404();
 
       }

 
       // set project ids to session userdata
 
       $ses = array('projectid' => $projectid);
 
       $this->session->set_userdata($ses);

 
       // querying get data of project
 
       $this->project_model->data_project($projectid);
 
   }


After set this ids to userdata it will be easy to me querying data related projects from database:

Code:
// get project id from session
$projectid = $this->session->userdata('projectid');

// querying save goods to related project
$this->project_model->add_goods_project($projectid, $form_data);

// querying remove goods from related project
$this->project_model->remove_goods_project($projectid, $goodid);

But this method will not effective if user open multiple projects link in diffrent tabs, it will mess up querying to database, since session only support one data.

If I set userdata as variabel:
Code:
$ses = array($projectid);
$this->session->set_userdata($ses);

It make me confused how call this userdata in other functions, since I don't know which ids projects user have.

I need someone suggest me (advise) a better method so user can enjoy open multiple projects?

Thanks


RE: Need help better method - skunkbad - 07-09-2015

It sounds like you need your users to have an account, where they sign in. Once authenticated, they would have access to their own projects, which you would store in your database. This is pretty easy if you choose a CI Auth library or package. Set that up and you will need to create something so your users can register. Easy stuff really.


RE: Need help better method - ichadhr - 07-10-2015

@skunkbad
I am sorry for any bad explanation may have caused you not catch properly my problem.
I already have users management, so my problem not here.
Maybe this one can make more clear.

 example: in dashboard index view I have links:
PHP Code:
<a href="http://me.com/projects/index/1" title="List Project">Project</a>
<
a href="http://me.com/projects/index/2" title="List Project">Project</a>
<
a href="http://me.com/projects/index/3" title="List Project">Project</a>  

Say users open above first link which has id '1', it will direct users to second controller (projects controller).
In index function I catch this id and store it as projectid to session userdata:

projects controller:
PHP Code:
function index($projectid NULL)
{
        // set project ids to session userdata
        $ses = array('projectid' => $projectid);
        $this->session->set_userdata($ses);

        // querying get data of project
        $this->project_model->data_project($projectid);


Next, it will make me easy if need querying to database in other function.
example I have function 'list goods' in (projects controller);
PHP Code:
function list_goods()
{
        // get project id from session
        $projectid $this->session->userdata('projectid');

        // querying get list of goods related project
        $this->project_model->goods_project($projectid);


The problem came if users open different projects in multiple tabs browser:
Let say users open project which has id '1' in (first tab) and,
open project has id '3' in (second tab),
then users switch to (first tab) and refresh the page, session data projectid in first tab will changed to '3'.

I need a better method so users can enjoy open multiple projects in multiple tabs browser..