Reputation: 331
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;
}
}
Upvotes: 1
Views: 332
Reputation: 331
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>
Upvotes: 0
Reputation: 359
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
Upvotes: 2