menu

Questions & Answers

Proper controller structure

Controller

public function show_sku() {
        $this->load->view('templates/header');
        $this->load->view('check_sku');
        $this->load->view('templates/footer');
    }

    public function retrieve_data() {
        $data['product'] = $this->sku->find_sku();
        $this->load->view('templates/header');
        $this->load->view('check_sku', $data);
        $this->load->view('templates/footer');
    }

Routes

$route['check_sku/show_sku'] = 'check_sku/show_sku';
$route['check_sku/retrieve_data'] = 'check_sku/retrieve_data';

View

<?php echo form_open('/check_sku/retrieve_data'); ?>

<div class="form-group">
    <?php echo form_label('SKU', 'sku', ''); ?>
    <?php echo form_input('sku', '', $options['sku']); ?>
    <?php echo form_submit('', 'Check', 'class="btn btn-primary"') ?>
</div>
<?php echo form_close(); ?>

Model

public function find_sku() {
        $this->load->library('get_api');
        $sku = $this->input->post('sku');
        $get_data = $this->get_api->get_data("GET", "http://89.201.137.7:8082/datasnap/rest/artikli/sifra/" . $sku, false);
        $response = json_decode($get_data, true);
        return $response['result'][0]['artikli'][0];
    }

What is the proper way to structure your controller so I don't load the views two times and I don't submit the form by just hitting the route, and so that I pass the data to the view when the form is submitted by clicking the submit.

Comments:
2023-01-11 09:00:14
how does your model's sky->find_sku function look like? Are you using the input field sku value for a query?
2023-01-11 09:00:14
I fetch some data using the input field value. I posted the model.
Answers(0) :