EllisLab text mark
Advanced Search
     
Global variables in controller
Posted: 06 June 2012 10:35 AM   [ Ignore ]
Joined: 2010-07-13
14 posts

Hi,
I have different variables in my controller what contains information like image coordinates, resolution etc. Most of them are used only inside controller. So it would be stupid to pass them to view and store them to hidden text input’s. I thought that if i make global variables inside controller, so i could store that kind of info there. But is there any concerns, or better way to achieve this?

 
Posted: 07 June 2012 12:33 AM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Joined: 2008-12-19
612 posts

Depending on how persistent you need them to be, you could store them in a custom config file. or use a Base controller to set vars accessible in any other controller.

 Signature 

CI 2.1.3, Linux, LAMP. We also like Gold and Silver…
Blame the hammer, it is obviously the guilty party…
User Guide? Nobody told me there was a User Guide!

 
Posted: 08 June 2012 11:10 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2010-07-13
14 posts

I try to describe. First of all, i have to throw my first message to trash bin. Cause i wasn’t thinking this through. I’m using jQuery on my site, and when it makes AJAX requests, it of course loads controller again, and can’t access on those variables. So i have to store some hidden data in hidden inputs.

But i’ll explain how i managed to store data and how my site works atm. So my little ongoing project is used to create simple images, like banners. When user is visiting first time, my site creates unique id what is stored to session. Then it makes default background image and default text image to TEMP folder with GD and stores path’s, filename’s, width, height, colors, and all default stuff to MySQL temp table. Row ID is same as session id.

So next time when user is visiting site and if session is found, it uses those images and values from SQL, and doesn’t create them again. If user is changing input values, jQuery saves them to MySQL temp table onchange.

One of my “problem” ATM is that when user is in “create_image” controller, and he want’s to upload own background image. I’m not using jQuery to upload file (now i’m thinking should i?), but i’m using basic form upload that calls “upload_image” controller. But if there is error or not, i’ll have to again call MySQL to get all previous data back to “create_image” controller.

This isn’t problem, but i think that this could get quite messy, cause my current aproach is like this:

<?php
if ( $this->image_model->is_session_found$this->session->userdata('example') ) )
{
  $query 
$this->db->get_where('temp_sessions', array('id' => $this->session->userdata('example')), 1);
    
  foreach (
$query->result() as $row)
  
{
    $data[
'temp_text_path'$row->temp_text_path;
    
$data['temp_bg_path'$row->temp_bg_path;
        
    
$data['temp_text_www'$row->temp_text_www;
    
$data['temp_text_width'$row->temp_text_width;
    
$data['temp_text_height'$row->temp_text_height;
    
    
$data['temp_bg_www'$row->temp_bg_www;
    
$data['temp_bg_width'$row->temp_bg_width;
    
$data['temp_bg_height'$row->temp_bg_height;

    
$data['image_string'$row->image_string;
    
$data['font_color'$row->font_color;
    
$data['font_size'$row->font_size;
  
  
}
  
  $template_data 
= array(
    
'title' => 'Create counter',
    
'heading' => 'Create counter',
    
'content' => $this->load->view('create_counter'$dataTRUE)
  );

  
$this->parser->parse('layout'$template_data);
}
?> 
 
Posted: 08 June 2012 04:45 PM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Joined: 2008-12-19
612 posts
Ficoder - 08 June 2012 11:10 AM

...
Row ID is same as session id.
...

Sorry, I don’t use jquery or ajax, but you may have a problem because the session id changes on every login and sometimes during a session.

 Signature 

CI 2.1.3, Linux, LAMP. We also like Gold and Silver…
Blame the hammer, it is obviously the guilty party…
User Guide? Nobody told me there was a User Guide!