Jump to content

Undefined Method


Go to solution Solved by Moorcam,

Recommended Posts

Hi guys,

I am using this same code on other sections of the application with no issues but on this particular one I am getting "Undefined Method "Model_fleet::show()"

Here is Fleet.php line 16 is the error:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Fleet extends CI_Controller 
{
	function __construct() 
	{
        parent::__construct();
        $this->load->model('admin/Model_common');
        $this->load->model('admin/Model_fleet');
    }

	public function index()
	{
		$data['setting'] = $this->Model_common->get_setting_data();
		$data['fleet'] = $this->Model_fleet->show();

		$this->load->view('admin/view_header',$data);
		$this->load->view('admin/view_fleet',$data);
		$this->load->view('admin/view_footer');
	}

	public function add()
	{
		$data['setting'] = $this->Model_common->get_setting_data();

		$error = '';
		$success = '';

		if(isset($_POST['form1'])) {

			$valid = 1;

			$this->form_validation->set_rules('v_number', 'Fleet Number', 'trim|required');
			$this->form_validation->set_rules('designation', 'Designated Driver', 'trim|required');

			if($this->form_validation->run() == FALSE) {
				$valid = 0;
                $error .= validation_errors();
            }

            $path = $_FILES['photo']['name'];
		    $path_tmp = $_FILES['photo']['tmp_name'];

		    if($path!='') {
		        $ext = pathinfo( $path, PATHINFO_EXTENSION );
		        $file_name = basename( $path, '.' . $ext );
		        $ext_check = $this->Model_common->extension_check_photo($ext);
		        if($ext_check == FALSE) {
		            $valid = 0;
		            $error .= 'You must have to upload jpg, jpeg, gif or png file for featured photo<br>';
		        }
		    } else {
		    	$valid = 0;
		        $error .= 'You must have to select a photo for featured photo<br>';
		    }

		    if($valid == 1) 
		    {
				$next_id = $this->Model_fleet->get_auto_increment_id();
				foreach ($next_id as $row) {
		            $ai_id = $row['Auto_increment'];
		        }

		        $final_name = 'fleet-'.$ai_id.'.'.$ext;
				$source_image = $path_tmp;
				$destination = './public/uploads/'.$final_name;
				$new_width = 600;
			  	$new_height = 600;
			 	$quality = 100;
				$this->Model_common->image_handler($source_image,$destination,$new_width,$new_height,$quality);

		        $final_name_thumb = 'fleet-'.$ai_id.'-thumb'.'.'.$ext;
				$source_image = $path_tmp;
				$destination = './public/uploads/'.$final_name_thumb;
				$new_width = 260;
			  	$new_height = 260;
			 	$quality = 100;
				$this->Model_common->image_handler($source_image,$destination,$new_width,$new_height,$quality);

		        $form_data = array(
						'v_number'             => $_POST['v_number'],
						'designation' => $_POST['designation'],
						'v_reg'      => $_POST['v_reg'],
						'v_pax'           => $_POST['v_pax'],
						'photo'            => $final_name
	            );
	            $this->Model_fleet->add($form_data);

		        $success = 'Fleet is added successfully!';
				$this->session->set_flashdata('success',$success);
		    	redirect(base_url().'admin/fleet');
		    } 
		    else
		    {
		    	$this->session->set_flashdata('error',$error);
		    	redirect(base_url().'admin/fleet/add');
		    }
            
        } else {
            $this->load->view('admin/view_header',$data);
			$this->load->view('admin/view_fleet_add',$data);
			$this->load->view('admin/view_footer');
        }
		
	}


	public function edit($id)
	{
		
    	$tot = $this->Model_fleet->fleet_check($id);
    	if(!$tot) {
    		redirect(base_url().'admin/fleet');
        	exit;
    	}
       	
       	$data['setting'] = $this->Model_common->get_setting_data();
		$error = '';
		$success = '';


		if(isset($_POST['form1'])) 
		{

			$valid = 1;

			$this->form_validation->set_rules('v_number', 'Fleet Number', 'trim|required');
			$this->form_validation->set_rules('designation', 'Designation', 'trim|required');

			if($this->form_validation->run() == FALSE) {
				$valid = 0;
                $error .= validation_errors();
            }

            $path = $_FILES['photo']['name'];
		    $path_tmp = $_FILES['photo']['tmp_name'];

		    if($path!='') {
		        $ext = pathinfo( $path, PATHINFO_EXTENSION );
		        $file_name = basename( $path, '.' . $ext );
		        $ext_check = $this->Model_common->extension_check_photo($ext);
		        if($ext_check == FALSE) {
		            $valid = 0;
		            $error .= 'You must have to upload jpg, jpeg, gif or png file for featured photo<br>';
		        }
		    }

		    if($valid == 1) 
		    {
		    	$data['fleet'] = $this->Model_fleet->get_fleet($id);

		    	if($path == '') {
		    		$form_data = array(
						'v_number'             => $_POST['v_number'],
						'designation' => $_POST['designation'],
						'v_reg'      => $_POST['v_reg'],
						'v_pax'           => $_POST['v_pax']
					);

		            $this->Model_fleet->update($id,$form_data);
		    	}
		    	if($path != '') {
		    		unlink('./public/uploads/'.$_POST['current_photo']);

					$final_name = 'fleet-'.$id.'.'.$ext;

		        	$temp_arr = explode('.',$_POST['current_photo']);
					$temp_final = $temp_arr[0].'-thumb'.'.'.$temp_arr[1];
					unlink('./public/uploads/'.$temp_final);

					$final_name = 'fleet-'.$id.'.'.$ext;
					$source_image = $path_tmp;
					$destination = './public/uploads/'.$final_name;
					$new_width = 600;
				  	$new_height = 600;
				 	$quality = 100;
					$this->Model_common->image_handler($source_image,$destination,$new_width,$new_height,$quality);

		        	$final_name_thumb = 'fleet-'.$id.'-thumb'.'.'.$ext;
					$source_image = $path_tmp;
					$destination = './public/uploads/'.$final_name_thumb;
					$new_width = 260;
				  	$new_height = 260;
				 	$quality = 100;
					$this->Model_common->image_handler($source_image,$destination,$new_width,$new_height,$quality);

		    		$form_data = array(
						'v_number'             => $_POST['v_number'],
						'designation' => $_POST['designation'],
						'v_reg'      => $_POST['v_reg'],
						'v_pax'           => $_POST['v_pax'],
						'photo'            => $final_name
		            );
		            $this->Model_fleet->update($id,$form_data);
		    	}

		    	if($path != '') {

		    		unlink('./public/uploads/'.$_POST['current_photo']);

					$final_name = 'fleet-'.$id.'.'.$ext;

		        	$temp_arr = explode('.',$_POST['current_photo']);
					$temp_final = $temp_arr[0].'-thumb'.'.'.$temp_arr[1];
					unlink('./public/uploads/'.$temp_final);

					$final_name = 'fleet-'.$id.'.'.$ext;
					$source_image = $path_tmp;
					$destination = './public/uploads/'.$final_name;
					$new_width = 600;
				  	$new_height = 600;
				 	$quality = 100;
					$this->Model_common->image_handler($source_image,$destination,$new_width,$new_height,$quality);

		        	$final_name_thumb = 'fleet-'.$id.'-thumb'.'.'.$ext;
					$source_image = $path_tmp;
					$destination = './public/uploads/'.$final_name_thumb;
					$new_width = 260;
				  	$new_height = 260;
				 	$quality = 100;
					$this->Model_common->image_handler($source_image,$destination,$new_width,$new_height,$quality);


		    		$form_data = array(
						'v_number'             => $_POST['v_number'],
						'designation' => $_POST['designation'],
						'v_reg'      => $_POST['v_reg'],
						'v_pax'           => $_POST['v_pax'],
						'photo'            => $final_name
		            );
		            $this->Model_fleet->update($id,$form_data);
		    	}

				$success = 'Fleet is updated successfully';
				$this->session->set_flashdata('success',$success);
				redirect(base_url().'admin/fleet');
		    }
		    else
		    {
		    	$this->session->set_flashdata('error',$error);
		    	redirect(base_url().'admin/fleet/edit/'.$id);
		    }           
		} else {
			$data['fleet'] = $this->Model_fleet->get_fleet($id);
            $this->load->view('admin/view_header',$data);
			$this->load->view('admin/view_fleet_edit',$data);
			$this->load->view('admin/view_footer');
		}

	}


	public function delete($id) 
	{
		// If there is no Fleet in this id, then redirect
    	$tot = $this->Model_fleet->fleet_check($id);
    	if(!$tot) {
    		redirect(base_url().'admin/fleet');
        	exit;
    	}

        $data['fleet'] = $this->Model_fleet->get_fleet($id);
        if($data['fleet']) {
            unlink('./public/uploads/'.$data['fleet']['photo']);
            $temp_arr = explode('.',$data['fleet']['photo']);
			$temp_final = $temp_arr[0].'-thumb'.'.'.$temp_arr[1];
			unlink('./public/uploads/'.$temp_final);

        }

        $this->Model_fleet->delete($id);
        $success = 'Fleet is deleted successfully';
        $this->session->set_flashdata('success',$success);
    	redirect(base_url().'admin/fleet');
    }

 
}

Here is Model_fleet.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Model_fleet extends CI_Model 
{

    function get_auto_increment_id()
    {
        $sql = "SHOW TABLE STATUS LIKE 'tbl_fleet'";
        $query = $this->db->query($sql);
        return $query->result_array();
    }
    
    function get_fleet_number_by_id($id) {
        $sql = 'SELECT * FROM tbl_fleet WHERE v_id=?';
        $query = $this->db->query($sql,array($id));
        return $query->first_row('array');
    }

    function show() {
        $sql = "SELECT * FROM tbl_fleet";
        $query = $this->db->query($sql);
        return $query->result_array();
    }

    function add($data) {
        $this->db->insert('tbl_fleet',$data);
        return $this->db->insert_id();
    }

    function update($id,$data) {
        $this->db->where('v_id',$id);
        $this->db->update('tbl_fleet',$data);
    }

    function delete($id)
    {
        $this->db->where('v_id',$id);
        $this->db->delete('tbl_fleet');
    }

    function getData($id)
    {
        $sql = 'SELECT * FROM tbl_fleet WHERE v_id=?';
        $query = $this->db->query($sql,array($id));
        return $query->first_row('array');
    }

    function fleet_check($id)
    {
        $sql = 'SELECT * FROM tbl_fleet WHERE v_id=?';
        $query = $this->db->query($sql,array($id));
        return $query->first_row('array');
    }

    function get_fleet($id)
    {
        $sql = 'SELECT * FROM tbl_fleet WHERE v_id=?';
        $query = $this->db->query($sql,array($id));
        return $query->first_row('array');
    }
    
}

And finally view_fleet.php

<?php
if(!$this->session->userdata('id')) {
    redirect(base_url().'admin');
}
?>

<section class="content-header">
    <div class="content-header-left">
        <h1>View Fleets</h1>
    </div>
    <div class="content-header-right">
        <a href="<?php echo base_url(); ?>admin/fleet/add" class="btn btn-primary btn-sm">Add New Fleet</a>
    </div>
</section>

<section class="content">
    <div class="row">
        <div class="col-md-12">

            <?php
                if($this->session->flashdata('error')) {
                    ?>
                    <div class="callout callout-danger">
                        <p><?php echo $this->session->flashdata('error'); ?></p>
                    </div>
                    <?php
                }
                if($this->session->flashdata('success')) {
                    ?>
                    <div class="callout callout-success">
                        <p><?php echo $this->session->flashdata('success'); ?></p>
                    </div>
                    <?php
                }
            ?>


            <div class="box box-info">
                <div class="box-body table-responsive">

                    <table id="example1" class="table table-bordered table-striped">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>Photo</th>
                                <th>Designation</th>
                                <th>Fleet #</th>
                                <th>Registration</th>
                                <th>Max Pax
                                <th width="140">Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $i=0;
                            foreach ($fleet as $row) {
                                $i++;
                                ?>
                                <tr>
                                    <td><?php echo $i; ?></td>
                                    <td style="width:130px;"><img src="<?php echo base_url(); ?>public/uploads/<?php echo $row['photo']; ?>" alt="<?php echo $row['v_number']; ?>" style="width:120px;"></td>
                                    <td><?php echo $row['designation']; ?></td>
                                    <td><?php echo $row['v_number']; ?></td>
                                    <td><?php echo $row['v_reg']; ?></td>
                                    <td><?php echo $row['v_pax']; ?></td>
                                    <td>                                        
                                        <a href="<?php echo base_url(); ?>admin/fleet/edit/<?php echo $row['v_id']; ?>" class="btn btn-primary btn-xs">Edit</a>
                                        <a href="<?php echo base_url(); ?>admin/fleet/delete/<?php echo $row['v_id']; ?>" class="btn btn-danger btn-xs" onClick="return confirm('Are you sure?');">Delete</a>
                                    </td>
                                </tr>
                                <?php
                            }
                            ?>                          
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>


</section>

I can't work out why this is happening as all other sections, use the same code concept and layout with no issue.

Any guidance would be appreciated.

Cheers

Dan

Link to comment
https://forums.phpfreaks.com/topic/315368-undefined-method/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.