Welcome Guest, Not a member yet? Register   Sign In
Accessing variable from a different function
#1

[eluser]Unknown[/eluser]
New to the forum, (1st post actually).

I have this site I'm developing that is my online portfolio consisting of websites I developed. I have a database consisting primarily of site info and image info since a single site can have multiple images. In the 'SITE_PICS' table I reference the site_id from the 'SITES' table as a foreign key.

The site flow provides for the loading of site info which posts the data to database and then directs to a page for uploading images. When loading the images which takes place in the 'admin/upload_pics' function, I need the site_id from the 'admin/create_site' function I created previously. Furthermore, I need to retain that id for all images I upload until I say end upload images.

So far that mission has led to nothing to but headaches as I can't seem to figure out how CI can obtain that variable data and use it in another function within the same controller. Below is all the code I believe is needed to analyze and any help would be greatly appreciated.

The GOOD: I can get the right id from the last site uploaded to the db via the variable $id.
The BAD: The upload pics function cannot access it.

Controller Code:
Code:
******** Controller code - Handles admin authentication, site upload, pic upload ******
******** Accepts form inputs from views screens **********

<?php
function create_site()
    {
        $this->load->model('site_model');
        $data = array(
            'FK_status_type' => $this->input->post('site_status'),
            'FK_tech_type' => $this->input->post('site_type'),
            'site_title' => $this->input->post('site_title'),
            'description' => $this->input->post('site_descrip')
            //'site_build_date' => $this->input->post('site_build_date')
        );
        $this->site_model->add_site_record($data);
        //Get the id of the record we just created
        $id = $this->db->insert_id();
        // Admin is sent to page to confirm the addition of pics for the site
        $data['main_content'] = 'pic_add';
        $this->load->view('includes/template', $data);
        // this is just a test to validate that the id is being obtained from the db
        echo ($id);
    }
    
    function upload_pics()
    {
        $this->load->model('load_img_model');
        // We test to verify an upload, upload and manipulate the image via the model function called
        if ($this->input->post('upload'))
        {
            $this->load_img_model->do_upload();
        }
        
        $this->load->model('site_model');
        $data = array(
            // We are using an FK_site_id of '1' currently.
            // HOWEVER THIS IS WHERE I WANT TO USE THE ID OF THE SITE I CREATED //
            // IN THE FUNCTION ABOVE: CREATE_SITE90 //
            'FK_site_id' => '1',
            'pic_title' => $this->input->post('pic_title'),
            'pic_alt' => $this->input->post('pic_alt'),
            'pic_url' => $this->upload->file_name,
            'pic_url_sized' => $this->upload->file_name,
            'pic_url_thumb' => $this->upload->file_name
            //'site_build_date' => $this->input->post('site_build_date')
        );
        
        $this->site_model->add_pic_record($data);
        $data['main_content'] = 'admin_view';
        $this->load->view('includes/template', $data);
    }

Model Code
Code:
******* Model that adds records for website info, pic info and get records *******

<?php
class Site_model extends CI_Model {
    
    function add_site_record($data)
    {
        $this->db->insert('sites', $data);
        return;
    }
    
    function add_pic_record($data)
    {
        $this->db->insert('site_pics', $data);
        return;
    }

******* Model to load images, get images and manipulate images *********

<?php
class Load_img_model extends CI_Model
{
    var $gallery_path;
    var $gallery_path_url;
    
    function __construct()
    {
        parent::__construct();
        
        $this->gallery_path = realpath(APPPATH . '../images');
        $this->gallery_path_url = base_url().'images/';
    }
    //upload the image to file folder
    function do_upload()
    {
        $config = array(
            'allowed_types' => 'jpg|jpeg|gif|png',
            'upload_path' => $this->gallery_path,
            'max_size' => 2000
        );
        $this->load->library('upload', $config);
        $this->upload->do_upload();
        $image_data = $this->upload->data();
        
        $config = array(
            'source_image' => $image_data['full_path'],
            'new_image' => $this->gallery_path . '/thumbs',
            'maintain_ration' => true,
            'width' => 150,
            'height' => 100
        );
        $this->load->library('image_lib', $config);
        $this->image_lib->resize();
    }
}




Theme © iAndrew 2016 - Forum software by © MyBB