0
votes

I need to edit a record that fetches data from two tables (i.e. county and sub-county). I need the county dropbox field to fetch and display the record to be edited.

model: Sub_County_model.php

<?php
require_once APPPATH . '/libraries/Base_model.php';
class Sub_County_model extends Base_model
{
protected $table_name = 'sub_counties';
protected $column_id = 'sub_county_id';
protected $column_name = 'sub_county_title';
public function __construct()
{

    parent::__construct();
}

/**
 * This function is used to update the sub_county information
 * @param array $sub_countyInfo : This is sub_counties updated information
 * @param number $sub_countyId : This is sub_county id
 */
public function editSub_County($sub_countyInfo, $sub_countyId)
{
    $this->db->where('sub_county_id', $sub_countyId);
    $this->db->update('sub_counties', $sub_countyInfo);

    return true;
}

/**
 * This function is used to delete the sub_county information
 * @param number $sub_countyId : This is sub_county id
 * @return boolean $result : TRUE / FALSE
 */
public function deleteSub_County($sub_countyId, $sub_countyInfo)
{
    $this->db->where('sub_county_id', $sub_countyId);
    $this->db->update('sub_counties', $sub_countyInfo);

    return $this->db->affected_rows();
}

// Query to extract all sub_county data and populate on sub_countyList.php files
public function select()
{
    $this->db->select('sub_counties.sub_county_id, counties.county_title, sub_counties.sub_county_title, sub_counties.sub_county_description');
    $this->db->from('sub_counties,counties');
    $this->db->where('sub_counties.county_id = counties.county_id');
    $this->db->where('sub_counties.is_deleted', 0);
    $this->db->order_by('sub_counties.sub_county_id desc');
    $query = $this->db->get();
    return $query->result_array();
}

/**
 * This function is used to get the sub_county listing count
 * @param string $searchText : This is optional search text
 * @return number $count : This is row count
 */

public function sub_countyListingCount($searchText = '')
{
    $this->db->select('*');
    $this->db->from('sub_counties');
    //$this->db->where('sub_counties.is_deleted', 0);

    if (!empty($searchText)) {
        $likeCriteria = "(sub_counties.sub_county_title  LIKE '%" . $searchText . "%' OR  sub_counties.sub_county_description  LIKE '%" . $searchText . "%')";
        $this->db->where($likeCriteria);
    }
    $this->db->where('sub_counties.is_deleted', 0);
    $query = $this->db->get();

    return $query->num_rows();
}

/**
 * This function is used to get the sub_county listing count
 * @param string $searchText : This is optional search text
 * @param number $page : This is pagination offset
 * @param number $segment : This is pagination limit
 * @return array $result : This is result
 */
public function sub_countyListing($searchText = '', $page = 1, $segment = 10)
{
    $this->db->select('*');
    $this->db->from('sub_counties');
    //$this->db->where('sub_counties.is_deleted', 0);

    if (!empty($searchText)) {
        $likeCriteria = "(sub_counties.sub_county_title  LIKE '%" . $searchText . "%' OR  sub_counties.sub_county_description  LIKE '%" . $searchText . "%')";
        $this->db->where($likeCriteria);
    }
    $this->db->where('sub_counties.is_deleted', 0);
    $this->db->order_by('sub_counties.sub_county_id', 'DESC');
    $this->db->limit($page, $segment);
    $query = $this->db->get();

    $result = $query->result();
    return $result;
}

public function getSub_County()
{
    $query = $this->db->query('SELECT sub_county_id, sub_county_title FROM sub_counties WHERE is_deleted = 0');
    return $query->result();
}

}

view: sub_countyEdit.php

<!-- ============================================================== -->
<!--Pageloader start here-->
<!-- ============================================================== -->
<!-- ============================================================== -->

<div class='content-wrapper'>
<section class='content-header'>
    <h1>
        <i class='fa fa-th'></i> <?php echo $singular; ?>
    </h1>
</section>

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

            <div class="card">
                <form id="norm_form" method="post" action="<?php echo site_url($verb . '/create') ?>">
                    <input type="hidden" name="saveType" id="saveType" value="1"/>
                    <input type="hidden" name="id" id="id" value=""/>
                    <div class="card-header">
                        <h3 class="card-title"> <?php echo 'Sub County Details:'; ?></h3>
                    </div>
                    <?php if ($this->session->flashdata('msg')) {echo $this->session->flashdata('msg');}?>
                    
                    <div class="card-body ">
                        <div class="row p-1">
                            <div class="col-md-12">
                                <div class="form-group ">
                                    <label for="name" class=" control-label">County Title</label>
                                    <select class="form-control" name="county_id" id="county_id" required>
                                        <option value="">Select County Title</option>
                                        <?php foreach($counties as $row){ 
                                            echo '<option value="'.$row->county_id.'">'.$row->county_title.'</option>';
                                        }?>
                                    </select>
                                    <?php echo form_error('county_id'); ?>
                                </div>
                            </div>
                        </div>
                        
                        <div class="row p-1">
                            <div class="col-md-12">
                                <div class="form-group ">
                                    <label for="name" class=" control-label">Sub County Title</label>
                                    <input type="text" id="sub_county_title" name="sub_county_title" class="form-control"
                                    placeholder="Enter Sub County" aria-describedby="sub_county_title" value="<?php echo set_value('sub_county_title'); ?>"/>
                                    <?php echo form_error('sub_county_title'); ?>
                                </div>
                            </div>
                        </div>
                    
                        <div class="row p-1">
                            <div class="col-md-12">
                                <div class="form-group ">
                                    <label for="name" class=" control-label">Sub County Description</label>
                                    <textarea type="text" id="sub_county_description" name="sub_county_description"  placeholder="Enter Sub County Description" class="form-control" aria-describedby="sub_county_description"><?php echo set_value('sub_county_description'); ?></textarea>
                                    <?php echo form_error('sub_county_description'); ?>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="card-footer">
                        <button type="submit" id="btnSubmit"
                            class="btn btn-primary"><?php echo $this->lang->line('submit'); ?></button>
                      
                        <a href="<?php echo site_url($verb); ?>" id="btnCancel"
                            class="btn btn-light"><?php echo $this->lang->line('cancel'); ?></a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</section>
</div>
<script>
$(document).ready(function() {
    validateData("#norm_form", {

    });
});
$(document).ready(function(){
    $('#county_id').change(function(){
        var county_id = $('#county_id').val();
    }
}
</script>

controller: Sub_County.php

<?php if (!defined('BASEPATH')) {
exit('No direct script access allowed');
}

require_once APPPATH . '/libraries/BaseController.php';
class Sub_County extends BaseController
{

protected $singular = 'Sub County';
protected $plural = 'Sub Counties';
protected $delete = '';
protected $viewPrefix = 'sub_county/sub_county';
protected $redirect_page = 'sub_county';
protected $verb = 'sub_county';

public function __construct()
{
    parent::__construct();
    $this->isLoggedIn();
    $this->load->model('Sub_County_model');
    $this->load->model('County_model');
    $this->singular ='Sub County';
    $this->plural = 'Sub Counties';
    $this->delete ='Delete Sub County';

}

public function index()
{
    $data = array();
    
    $data['data'] = $this->Sub_County_model->select();
    $this->global['pageTitle'] = $this->config->item('web_app_name') .' - '.$this->singular;
    $this->global['singular'] = $this->singular;
    $this->global['plural'] = $this->plural;
    $this->global['verb'] = $this->verb;
    $this->global['delete'] = $this->delete;
    $this->loadViews($this->viewPrefix . 'List', $this->global, $data, null);
}

public function add()
{
    $data = array();
    //populate dropdownlist in userCreate view with data
    $data['counties'] = $this->County_model->getCounty();
    $this->global['pageTitle'] = $this->config->item('web_app_name') .' - '.$this->singular;
    $this->global['singular'] = $this->singular;
    $this->global['plural'] = $this->plural;
    $this->global['verb'] = $this->verb;
    $this->global['delete'] = $this->delete;
    $this->loadViews($this->viewPrefix . 'Create', $this->global, $data, null);
}

public function edit($id = '')
{
    $data = array();
    //populate dropdownlist in userCreate view with data
    $data['counties'] = $this->County_model->getCounty();
    //general form code for adding users
    $this->global['pageTitle'] = $this->config->item('web_app_name') .' - '.$this->singular;
    $this->global['singular'] = $this->singular;
    $this->global['plural'] = $this->plural;
    $this->global['verb'] = $this->verb;
    $this->global['delete'] = $this->delete;
    //$id = $this->uri->segment(3);
    $data['row'] = $this->Sub_County_model->single($id);
    $this->loadViews($this->viewPrefix . 'Edit', $this->global, $data, null);
}

public function create()
{
    $this->load->library('session');
    $userId=$this->session->userdata('user_id');
    
    $this->form_validation->set_rules('county_id', 'County Title', 'trim|required');
    $this->form_validation->set_rules('sub_county_title', 'Sub County title', 'trim|required');
    $this->form_validation->set_rules('sub_county_description', 'Sub County description', 'trim|required');

    if ($this->form_validation->run() == false) {
        $this->session->set_flashdata('msg', '<div class="alert alert-danger">' . validation_errors() . '!</div>');
        redirect($this->redirect_page . '/add');
    } else {
        $county_id = $this->security->xss_clean($this->input->post('county_id'));
        $sub_county_title = $this->security->xss_clean($this->input->post('sub_county_title'));
        $sub_county_description = $this->security->xss_clean($this->input->post('sub_county_description'));
        $dataArray = array(
            'county_id' => $county_id,
            'sub_county_title' => $sub_county_title,
            'sub_county_description' => $sub_county_description,
            'created_by' => $userId,
        );
        $inserted = $this->Sub_County_model->create($dataArray);
        if ($inserted) {
            $this->session->set_flashdata('msg', '<div class="alert alert-success">' . $this->lang->line('data_added_successfully') . '!</div>');

        } else {
            $this->session->set_flashdata('msg', '<div class="alert alert-danger">' . $this->lang->line('data_not_saved') . '!</div>');

        }
        redirect($this->redirect_page);
    }
}

public function update()
{
    $this->load->library('session');
    $userId=$this->session->userdata('user_id');
    
    
    $this->load->helper('date');
    $updated_at = date('Y-m-d H:i:s', now());
    
    $id = $this->input->post('id');
    
    $this->form_validation->set_rules('county_id', 'County Title', 'trim|required');
    $this->form_validation->set_rules('sub_county_title', 'Sub County title', 'trim|required');
    $this->form_validation->set_rules('sub_county_description', 'Sub County description', 'trim|required');
    
    if ($this->form_validation->run() == false) {
        $this->session->set_flashdata('msg', '<div class="alert alert-danger">' . validation_errors() . '!</div>');
        redirect($this->redirect_page . '/edit/' . $id);
    } else {
        $county_id = $this->security->xss_clean($this->input->post('county_id'));
        $sub_county_title = $this->security->xss_clean($this->input->post('sub_county_title'));
        $sub_county_description = $this->security->xss_clean($this->input->post('sub_county_description'));
        $dataArray = array(
            'county_id' => $county_id,
            'sub_county_title' => $sub_county_title,
            'sub_county_description' => $sub_county_description,
            'updated_at' => $updated_at,
            'updated_by' => $userId,
        );
        $updated = $this->Sub_County_model->update($dataArray, $id);
        if ($updated) {
            $this->session->set_flashdata('msg', '<div class="alert alert-success">' . $this->lang->line('data_updated_successfully') . '!</div>');

        } else {
            $this->session->set_flashdata('msg', '<div class="alert alert-danger">' . $this->lang->line('data_not_saved') . '!</div>');

        }
        redirect($this->redirect_page);
    }
}

public function delete()
{
    $this->load->library('session');
    $userId=$this->session->userdata('user_id');
    
    $deleted = 1;

    $this->load->helper('date');
    $updated_at = date('Y-m-d H:i:s', now());
    
    $id = $this->input->post('id');
    
    $dataArray = array(
        'is_deleted' => $deleted,
        'deleted_at' => $updated_at,
        'deleted_by' => $userId,
    );
    $updated = $this->Sub_County_model->update($dataArray, $id);
    if ($updated) {
        $this->session->set_flashdata('msg', '<div class="alert alert-success">' . $this->lang->line('data_deleted_successfully') . '!</div>');
        header('Content-Type:application/json');
        echo json_encode(array("code" => 1, "message" => $this->lang->line('data_deleted_successfully')));
    } else {
        $this->session->set_flashdata('msg', '<div class="alert alert-danger">' . $this->lang->line('data_not_deleted') . '!</div>');          
        header('Content-Type:application/json');
        echo json_encode(array("code" => 2, "message" => $this->lang->line('data_not_deleted')));
    }
    redirect($this->redirect_page);
}

public function single()
{
    header('Content-Type:application/json');
    $id = $this->input->post('id');
    echo json_encode($this->Sub_County_model->single($id));
}

public function dropdown()
{
    header('Content-Type:application/json');
    echo json_encode($this->Sub_County_model->dropdown());
}

}

County_model.php

 public function getCounty()
{
    $query = $this->db->query('SELECT county_id, county_title FROM counties WHERE is_deleted = 0');
    return $query->result();
}

I need the text field i.e. County Title in the sub_countyEdit.php view to display the data in a dropbox field for editing.

sub county edit form