1

I have created a dropdown in plain php that really works and I want it to apply also in my codeigniter project but I don't know how. I tried but the code does not work. This is the plain php to be converted to codeigniter: The main page:

<select id="operationName" name="operationName">
<option value="">-SELECT-</option>
<?php while($rs = $query->fetch())
{
extract($rs);
?>
<option value="<?php echo $OperationName;?>"><?php echo $OperationName;?>   </option>
<?php
}
?>
 </select>
<input type="text" id="deldays" name="deldays" >
<div id="deldays"></div>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script>
$(document).ready(function(){
$('#operationName').change(function(){
    $.get('trial.php',{q:$(this).val()},function(data){
        $('#deldays').val(data);
    });
});
});

and here is the php function that is being called:

if(isset($_GET['q']))
{
$days = $_GET['q'];
try{
$qry = $conn->prepare("SELECT * FROM operation WHERE OperationName = :opt");
$qry->bindParam(':opt',$days);
$qry->execute();
}
catch(PDOException $e)
{
echo $e->getMessage();
exit();
}
while($rs = $qry->fetch())
{
extract($rs);
echo $DeliveryDays;
}
}
4
  • 1
    Teach yourself how to use CodeIgniter. Commented Feb 29, 2016 at 2:15
  • I already know the basic of codeigniter and my rpoblem is that I cannot somehow implement this code to it. I am having problem with returning the value from controller to ajax..... Commented Feb 29, 2016 at 2:17
  • What specifically is the problem? Commented Feb 29, 2016 at 3:33
  • I am able to grasp the answer of @keronkonk but can you give me more readable code for model because I am a bit confused in what he'd done..... Commented Feb 29, 2016 at 3:37

2 Answers 2

2

First, you must know the concept of MVC, which codeigniter use

M is model (folder application/model) = the database operation

V is view (folder application/view) = the view, html, css, script etc.

C is controller (folder application/controller) = the page handler

this following page, which have the query, better if inserted in model

save it in folder application/model, with name m_operation.php

<?php
class Operation extends CI_Model {
    //table name
    private $operation = 'operation';
    function Operation() {
        parent::__construct();
        $this->load->helper('file');
    }
    function allOperation( $days ) {
        $this->db->where('OperationName', $days);
        return $this->db->get($operation)->result();                
    }
}
?>

this following page is view, save it in folder application/view, with name v_operation.php

<select id="operationName" name="operationName">
<option value="">-SELECT-</option>
<?php 
foreach ($operation as $row) {
    echo "<option value = '". $row->OperationName ."'>".
            $row->OperationName .
        "</option>";
}
?>
</select>


<input type="text" id="deldays" name="deldays" >
<div id="deldays"></div>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script>
$(document).ready(function(){
$('#operationName').change(function(){
    $.get('trial.php',{q:$(this).val()},function(data){
        $('#deldays').val(data);
    });
});
});

this following page is controller, save it in folder application/controller, with name operation.php

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

class Operation extends CI_Controller {
    function __construct() {
        parent::__construct();
        $this->load->helper('url');// Load Helper URL CI
        $this->load->model('m_operation');// load the model
    }

    public function index() {
        if(isset($this->input->get('q'))) {
            $days = $this->input->get('q');
            $data['operation'] = $this->m_operation->allOperation($days);       
            $this->load->view('v_operation', $data);
        }
    }
}

you can access the webpage with the name of controller, like this

yoursitename.com/index.php/operation

Sign up to request clarification or add additional context in comments.

3 Comments

I'm a bit confused in the model that you posted... it's different from how i write my query....
How can I apply this code to mine stackoverflow.com/questions/32607483/… ?.
I think you forgot something to include, how can I display the data for DeliveryDays in textfield.....
0

I found a wok around in my problem please check the code for future reference:

controller:

$operation = explode(',',$this->input->post('operationName'));
        foreach($operation as $val)
        {
            $opName = trim($operation[0]);
            $days = trim($operation[1]);
        }

model:

function get_operation()
{
    $this->db->select('*');
    $this->db->from('operation');
    $query = $this->db->get();
    $result = $query->result_array();

    return $result;
}

view:

<select class="form-control" id="operationName" name="operationName" >
       <option value="">-SELECT-</option>
        <?php
         foreach($operation as $val)
         {
          ?>
          <option value="<?php echo $val['OperationName'].','.$val['DeliveryDays'];?>"><?php echo $val['OperationName'];?></option>
          <?php
         }
      ?>
      </select>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.