Welcome Guest, Not a member yet? Register   Sign In
Variable in view not the same in all pages.
#11

[eluser]Cristian Gilè[/eluser]
In your update function you pass $ad_id as parameter but where do you define $ad_id in your index function?

In your index function $ad_id is not defined so:

Code:
$data['ads'] = $this->adModel->get_by_id($ad_id)->row();

is not valid.

Cristian Gilè
#12

[eluser]sb05[/eluser]
Too bad. That didn't work either. Not only did my two errors stay 1. Undefined variable: ad_id and 2. Trying to get property of non-object.
But I got a third one: Missing argument 1 for Ads::index()

and in the line
Code:
function index($ad_id){

Here is my index function, where I added $ad_id as a parameter.

Code:
function index($ad_id){
        

        // offset
        $uri_segment = 3;
        $offset = $this->uri->segment($uri_segment);
        
        // load data
        $ads = $this->adModel->get_paged_list($this->limit, $offset)->result();
        
    //    $data['ads'] = $this->adModel->get_by_id($ad_id)->row();
    //    $results = $this->adModel->get_by_id($ad_id);
        
        // generate pagination
        $this->load->library('pagination');
        $config['base_url'] = site_url('ads');
         $config['total_rows'] = $this->adModel->count_all();
         $config['per_page'] = $this->limit;
        $config['uri_segment'] = $uri_segment;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        
        // generate table data
        $this->load->library('table');
        $this->table->set_empty(" ");
        $this->table->set_heading('ID','Name', 'Image_url','Link', 'Client Name');
        $i = 0 + $offset;
        foreach ($ads as $ad){
            $this->table->add_row(++$i, $ad->name, $ad->image_url, $ad->link, $ad->client_name,
                anchor('ads/view/'.$ad->ad_id,'view',array('class'=>'view')).' '.
                anchor('ads/update/'.$ad->ad_id,'update',array('class'=>'update')).' '.
                anchor('ads/delete/'.$ad->ad_id,'delete',array('class'=>'delete','onclick'=>"return confirm('Are you sure want to delete this News ad?')"))
            );
        }
        $data['table'] = $this->table->generate();
                $this->load->vars($data);
                
    

         //  print_r($data['ads']);

                       $data['ads'] = $this->adModel->get_by_id($ad_id)->row();


        // load view
            $this->load->vars($this->data);
            

        
            $this->load->view('banner_view', $data);
            $this->load->view('left_column_view', $data);
            $this->load->view('right_column_view', $data);
            $this->load->view('adList', $data);
            $this->load->view('footer_view', $data);
    }

However, not putting $ad_id as a parameter in my function is not the issue I think. As there are some functions where it works and others don't. I didn't put all my functions in the other posts cuz of the post limit. But here is a function where it also works without putting $ad_id as a parameter.

Code:
function addAd(){
        // set common properties
        $data['title'] = 'Add new ad';
        $data['action'] = site_url('ads/addAd');
        $data['link_back'] = anchor('ads','Back to list of ads',array('class'=>'back'));
        
        // set validation properties
        $this->_set_fields();
        $this->_set_rules();
        
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '10000';
            $config['max_width']  = '10240';
            $config['max_height']  = '7680';
            $config['upload_path'] = './uploads';
            $this->load->library('upload', $config);
            

            $path_to_uploads='./uploads';
            
            
        //    $config['upload_path'] = $path_to_uploads;
                $this->load->library('upload', $config);

                       if (!$this->upload->do_upload()){
                            $error = $this->upload->display_errors();
                            echo $error;
                        }else{
                            $upload_data=$this->upload->data();
                            $file_name=$upload_data['file_name'];
                            $full_file_path = $path_to_uploads.'/'.$file_name;
                        }
        
        // run validation
        
        if ($this->validation->run() == TRUE){
            $data['message'] = 'It is not going to the database';
        }else{
            // save data
            $ad = array('name' => $this->input->post('name'),
                            'image_url' => $full_file_path,
                            'link' => $this->input->post('link'),
                            'client_name' => $this->input->post('client_name'))

                         ;
            $ad_id = $this->adModel->save($ad);
            
            // set form input name="id"
            $this->validation->ad_id = $ad_id;
            
            // set user message
            $data['message'] = '<div class="success">add new ad success</div>';
        }
                $data['ads'] = $this->adModel->get_by_id($ad_id)->row();

        
        // load viewinclude 'person.php';
        
            $this->load->view('banner_view', $data);
            $this->load->view('left_column_view', $data);
            $this->load->view('right_column_view', $data);
            $this->load->view('adEdit', $data);
            $this->load->view('footer_view', $data);    }

This is really driving me nuts by now Sad
#13

[eluser]Cristian Gilè[/eluser]
Quote:However, not putting $ad_id as a parameter in my function is not the issue I think.

Yes, I didn't say to pass $ad_id as parameter to the index function but you have to define $ad_id before to pass it to your model.

Are you sure that your addAd() function works without errors if the validation is TRUE ? It seems that the logic of the validation is inverted. You have to change your condition as:

Code:
if ($this->validation->run() != TRUE){
            $data['message'] = 'It is not going to the database';
        }else{
...
...

Cristian Gilè
#14

[eluser]sb05[/eluser]
Hi Cristian,

if I do != TRUE instead of == TRUE

the error still stays that the ad_id variable is undefined.
and I get the message that it's not going to the database.

My addAd function() works with no errors if the validation is set to TRUE and it also displays my ad.

I found something else out.
In the functions where I pasted the line
Code:
$data['ads'] = $this->adModel->get_by_id($ad_id)->row();
There were functions where it displayed it correctly from the get-go and others gave me the error of undefined variable: ad_id, right?

So in those functions with that error. If I add a line before saying
Code:
$ad_id=10;

Then my ad display worksSmile There is a big BUT though. It's a half solution. Because if I delete that ad in my crud and add a new one, the auto-increment in my database will give the new ad an id of 11. So if I want to display that, then I have to tweak into my code again in my view file, to display that add correctly.

So I think now instead of
Code:
$ad_id=10;

I should do something like
Code:
$ad_id = 'some SQL query where it understands that if I delete ad_id = 10 in my CRUD
and put ad_id =11 in its place, it wont give any errors and will
display the image that corresponds to ad_id = 11';

Ok, that last one was roughly said, but I hope you guys understand what I mean. So should I pursue that direction more?
So now it comes down to defining $ad_id with a proper SQL query?
#15

[eluser]Cristian Gilè[/eluser]
Quote:Hi Cristian,

if I do != TRUE instead of == TRUE

the error still stays that the ad_id variable is undefined.
and I get the message that it’s not going to the database.

My addAd function() works with no errors if the validation is set to TRUE and it also displays my ad.

You haven't set any validation rules so, according to your code, your validation test is always FALSE and the else branch

Code:
}else{
            // save data
            $ad = array('name' => $this->input->post('name'),
                            'image_url' => $full_file_path,
                            'link' => $this->input->post('link'),
                            'client_name' => $this->input->post('client_name'))

                         ;
            $ad_id = $this->adModel->save($ad);
            
            // set form input name="id"
            $this->validation->ad_id = $ad_id;
            
            // set user message
            $data['message'] = '<div class="success">add new ad success</div>';
        }
                $data['ads'] = $this->adModel->get_by_id($ad_id)->row();

will be executed. Here's why your addAd() function works.

Quote:There were functions where it displayed it correctly from the get-go and others gave me the error of undefined variable: ad_id, right?

So in those functions with that error. If I add a line before saying
$ad_id=10;

Then my ad display worksSmile

Yes, because you set $ad_id before to call the $this->adModel->get_by_id($ad_id)->row(); function, so $ad_id is not undefined.

Quote:Then my ad display worksSmile There is a big BUT though. It’s a half solution. Because if I delete that ad in my crud and add a new one, the auto-increment in my database will give the new ad an id of 11. So if I want to display that, then I have to tweak into my code again in my view file, to display that add correctly.

So I think now instead of
$ad_id=10;


I should do something like
$ad_id = 'some SQL query where it understands that if I delete ad_id = 10 in my CRUD
and put ad_id =11 in its place, it wont give any errors and will
display the image that corresponds to ad_id = 11';

Ok, that last one was roughly said, but I hope you guys understand what I mean. So should I pursue that direction more?
So now it comes down to defining $ad_id with a proper SQL query?

Your banner is associated to a client name. Right? So, probably in your db should be three tables one for banner, one for clients and one for the banner-clients association.
Code:
BANNER table:
id
name
image
link

CLIENT table:
id
name

BANNER_CLIENT table (association):
id
banner_id
client_id

When you delete a banner with id = x and from BANNER table you have to delete the corresponding row in the BANNER_CLIENT table where banner_id = x.

When you insert a banner in the BANNER table for a client x, save the id for that insert (for example y) and insert a row in the BANNER_CLIENT table with banner_id = y and client_id = x.

Now, you will pass the $id_client to the model function that query the BANNER_CLIENT table and with a join on the BANNER table you could retrieve the banner data for the client with id = $id_client.

I hope I was clear


Cristian Gilè
#16

[eluser]sb05[/eluser]
Ok, I think I see what you mean. So with adding this client_id I bypass the auto-increment of the ad_id, because the client_id stays the same, so then it doesn't matter the ad_id changes, right?
#17

[eluser]Cristian Gilè[/eluser]
That's right.


Cristian Gilè
#18

[eluser]sb05[/eluser]
Ok thanks! I will try it out.




Theme © iAndrew 2016 - Forum software by © MyBB