Welcome Guest, Not a member yet? Register   Sign In
Global variables in controller
#1

[eluser]Ficoder[/eluser]
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?
#2

[eluser]TWP Marketing[/eluser]
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.
#3

[eluser]Ficoder[/eluser]
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:

Code:
<?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', $data, TRUE)
  );

  $this->parser->parse('layout', $template_data);
}
?>
#4

[eluser]TWP Marketing[/eluser]
[quote author="Ficoder" date="1339168204"]
...
Row ID is same as session id.
...[/quote]

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.




Theme © iAndrew 2016 - Forum software by © MyBB