passing multiple checkbox values to controller - codeigniter

I am developing a movie ticket reservation system in codeigniter.The user can select his seat by checking the box on each available seat.When I try to store the checked valued in session data i get an error.Below is my code.
View select_seat
<!DOCTYPE html>
<html>
<head>
<title>Select Seats</title>
<link rel="stylesheet" type="text/css" href="http://localhost/cinema/assets/css/form2.css"/>
</head>
<body>
<form method="post" action="http://localhost/cinema/Movies/select_combo" name="selectshow" accept-charset="utf-8" class="username">
<?php
$prevRowId = null;
$seatColor = null;
$tableRow = false;
//echo $result;
echo "<table width='100%' border='0' cellpadding='0' cellspacing='2'>";
foreach($query as $key=>$result)
{
$rowId = $result['rowId'];
$status = $result['status'];
$columnId = $result['columnId'];
$typeID=$result['seat_type_id'];
if ($prevRowId != $rowId) {
if ($rowId != 'A') {
echo "</tr></table></td>";
echo "\n</tr>";
}
$prevRowId = $rowId;
echo "\n<tr><td align='center'><table border='0' cellpadding='5' cellspacing='2'> <tr>";
} else {
$tableRow = false;
}
if ($status == 0) {
if($typeID==1)
$seatColor = "grey";
else if($typeID==2)
$seatColor="yellow";
else
$seatColor="white";
}
else {
$seatColor = "red";
}
echo "\n<td bgcolor='$seatColor' align='center'>";
echo "$rowId$columnId";
if ($status == 0)
{
echo "<input type='checkbox' name='seats[]' value='$rowId$columnId'></checkbox>";
}
echo "</td>";
if (($rowId == 'A' && $columnId == 6)
|| ($rowId == 'B' && $columnId == 7)
|| ($rowId == 'C' && $columnId == 7)
|| ($rowId == 'D' && $columnId == 7)
) {
// This fragment is for adding a blank cell which represent the "center aisle"
echo "<td> </td>";
}
}
echo "</tr></table></td>";
echo "</tr>";
echo "</table>";
mysql_close();
?>
<input type="submit" class="button-link" style="float:right; margin-top: 20px; margin-right:0px; margin-bottom: 10px;" value="Submit" />
</form>
</body>
</html>
Model
<?php
class Selectseat extends CI_Model {
public function __construct()
{
$this->load->database();
}
function get() {
return $this->db->query("SELECT * from seats order by rowId, columnId asc")
->result_array();
}
}
?>
Controller
function select_combo(){
foreach($this->input->post('seats[]') as $seat)
$data['seats']=$seat;
$this->session->set_userdata($data);
$this->load->model('combo_model');
$data['combos'] = $this->combo_model->get_combo();
$this->load->view('header');
$this->load->view('select_combo',$data);
}
I'm getting this error
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: controllers/Movies.php

A quick finding:
You can't use as
$this->input->post('seats[]')
Instead
$seats = $this->input->post('seats');
Now seats having array of values.
You can change your code as below:
function select_combo(){
$data['seats']=$this->input->post("seats");
$this->session->set_userdata($data);
$this->load->model('combo_model');
$data['combos'] = $this->combo_model->get_combo();
$this->load->view('header');
$this->load->view('select_combo',$data);
}

Related

how to dynamically change owl carousel image from MySQL database with information

I was searching online all day today and could not find any solution for adding a dynamic image to owl carousel from MySQL database with information. If anyone can help?
I have done something like the following but I am getting only one slide, Like bootstrap, it is not showing all the images and table information from the database. If I loop it gives me all the tables on the same page.
<link rel="stylesheet" type="text/css" href="http://www.24limousine.com/wp-content/themes/24Limousine/assets/css/owl.carousel.min.css">
<?php
$connect = mysqli_connect("localhost", "root", "", "testing");
function make_query($connect)
{
$query = "SELECT * FROM banner ORDER BY banner_id ASC";
$result = mysqli_query($connect, $query);
return $result;
}
function make_slide_indicators($connect)
{
$output = '';
$count = 0;
$result = make_query($connect);
while($row = mysqli_fetch_array($result))
{
if($count == 0)
{
$output .= '
<li data-target="#dynamic_slide_show" data-slide-to="'.$count.'" class="active"></li>
';
}
else
{
$output .= '
<li data-target="#dynamic_slide_show" data-slide-to="'.$count.'"></li>
';
}
$count = $count + 1;
}
return $output;
}
return $output;
}
function make_slides($connect)
{
$output = '';
$count = 0;
$result = make_query($connect);
while($row = mysqli_fetch_array($result))
{
if($count == 0)
{
$output .= '<div class="item active">';
}
else
{
$output .= '<div class="item">';
}
$output .= '
<img src="banner/'.$row["banner_image"].'" alt="'.$row["banner_title"].'" />
<div class="carousel-caption">
<h3>'.$row["banner_title"].'</h3>
</div>
</div>
';
$count = $count + 2;
}
return $output;
}
?>
<div class="cover-wrapper">
<div id="client-logos" class="owl-carousel text-center">
<div class="item">
<div class="client-inners">
<?php echo make_slides($connect); ?>
</div>
</div>
</div>
</div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="http://www.24limousine.com/wp-content/themes/24Limousine/assets/js/owl.carousel.min.js"></script>
<script>
$(document).ready(function() {
$('#client-logos').owlCarousel({
// loop:true,
margin:15,
nav:true,
responsive:{
0:{
items:2
},
600:{
items:4
},
1000:{
items:6
}
},
navText: ["<img src='http://pixsector.com/cache/a8009c95/av8a49a4f81c3318dc69d.png'/>","<img src='http://pixsector.com/cache/81183b13/avcc910c4ee5888b858fe.png'/>"]
});

ajax is not able to call the function of codeigniter

This is Welcome controller method
public function send_otp()
{
echo 'in';
die;
$phone = $_POST['mobile'];
if ($phone != '') {
$mobile_detail = $this->welcome_model->check_if_already_mobile_no($phone);
if (!empty($mobile_detail)) {
if ($mobile_detail['is_verified'] == 'yes') {
$message = 'Already Verified.';
echo json_encode(array('status' => 'error', 'message' => $message));
exit;
} else {
$this->welcome_model->delete_mobile_no($phone);
}
}
$otp = self::generateRandomNo();
$this->welcome_model->insert_mobile_detail($phone, $otp);
$link = file_get_contents("http://49.50.67.32/smsapi/httpapi.jsp?username=aplusv&password=aplusv1&from=APLUSV&to=$phone&text=$otp&coding=0");
$status = '';
if ($link != '') {
$status = 'success';
$message = 'Successfully Otp send to your no.';
} else {
$status = 'error';
$message = 'Error in sending OTP.';
}
echo json_encode(array('status' => $status, 'message' => $message));
exit;
}
}
This is model
public function check_if_already_mobile_no($mobile_no = null)
{
$query = $this->db->get_where('mobile_sms', array('mobile_no' => $mobile_no));
return $query->row_array();
}
public function get_mobile_details($mobile_no = null, $otp = null)
{
$query = $this->db->get_where('mobile_sms', array('mobile_no' => $mobile_no, 'otp' => $otp));
return $query->row_array();
}
public function insert_mobile_detail($phone, $otp)
{
$this->mobile_no = $phone;
$this->otp = $otp;
$this->is_verified = 'no';
$this->created_at = date('Y-m-d H:i:s');
$this->db->insert('mobile_sms', $this);
}
This is view
<div class="container" style="margin-top: 25px;">
<div class="row">
<div class="col-md-12" id="response_msg"></div>
<div class="col-md-4" id="enter_mobile">
<form method="POST" action="#">
<div class="form-group">
<label for="phone">Phone </label>
<input type="text" class="form-control" id="mobile" name="phone" placeholder="Enter Mobile">
</div>
<button type="button" name="send_mobile" id="send_otp" class="btn btn-primary">Submit</button>
</form>
</div>
<script src="assets/js/jquery.js"></script>
<script type="text/javascript">
// var base_url = "<?php echo base_url();?>";
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script>
$(function () { // start of doc ready.
$("#send_otp").on('click', function (e) {
var mobile = $('#mobile').val();
alert(mobile);
$.ajax({
url: '<?php echo site_url('index.php/welcome/send_otp'); ?>',
data: {'mobile': mobile},
type: "post",
dataType: 'json',
success: function (data) {
if (data.status == 'success') {
$('#response_msg').html('<div class="alert alert-success" role="alert">' + data.message + '</div>');
$('#mobile_no').val(mobile);
$('#enter_mobile').hide();
$('#verify_otp_form').show();
} else {
$('#response_msg').html('<div class="alert alert-danger" role="alert">' + data.message + '</div>');
}
}
});
});
in ajax is not getting call ie $.ajax is not working here and my controller ie welcome with method send_otp is not called here.
why my function in controller is not getting called
how to solve the issue
what is the proper way to call the controller function using base_url
i have check the console also it is not showing any error
I noticed that you are using site_url() slightly incorrectly: don't write index.php there, just use site_url('welcome/send_otp')
Don't forget you have an unconditional die() at the top of your send_otp method - it will prevent any code below itself from executing

How to put Session data to textbox in Codeigniter

I'm the newbie in Codeigniter. I put the textbox values to session and go to next page and when i click the back button on 2nd page, then the 1st page data should be still in textbox. How can I do that? Please help me.
Controller code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index(){
$this->load->view('welcome_message');
}
public function page1(){
$this->session->set_userdata('page1',$this->input->get());
$this->config->load('config');
$this->config->set_item('sess_expiration', '60');
$this->load->view('pg2');
}
public function page2(){
$this->session->set_userdata('page2',$this->input->get());
$this->config->load('config');
$this->config->set_item('sess_expiration', '60');
$this->load->view('pg3');
}
public function page3(){
$this->config->load('config');
$this->config->set_item('sess_expiration', '60');
$sess_data=$this->session->userdata();
$ses_key = key($sess_data);
if (empty($this->session->userdata("page1")))
{
echo "Session has been Expired~!";
$this->session->unset_userdata('page1');
$this->session->unset_userdata('page2');
//redirect(site_url(),'refresh');
}
else
{
$q1 = ($sess_data['page1']);
$q2 = ($sess_data['page2']);
$result1 = '';
$result2 = '';
$result3 = '';
$result4 = '';
$result5 = '';
$result6 = '';
$result7 = '';
$result8 = '';
for ($i = 0; $i < count($q1); $i++)
{
$key=key($q1);
$val=$q1[$key];
if ($val<> ' ')
{
if ($key === 'input1')
{
$result1 = $val;
//echo $result1;
}
else if ($key === 'input2')
{
$result2 = $val;
//echo $result2;
}
else if ($key === 'input3')
{
$result3 = $val;
//echo $result3;
}
else
{
$result4 = $val;
//echo $result4;
}
echo $key ." = ". $val ." <br> ";
}
next($q1);
}
for ($i = 0; $i < count($q2); $i++)
{
$key=key($q2);
$val=$q2[$key];
if ($val<> ' ')
{
if ($key === 'input5')
{
$result5 = $val;
//echo $result5;
}
else if ($key === 'input6')
{
$result6 = $val;
//echo $result6;
}
else if ($key === 'input7')
{
$result7 = $val;
//echo $result7;
}
else
{
$result8 = $val;
//echo $result8;
}
echo " <br> <br> " . $key ." = ". $val ." <br> ";
}
next($q2);
}
//$this->testing_model->add_data('sess_table',['val'=>$result1,'val2'=>$result2]);
$this->testing_model->add_data('user_table',['id'=>"default", 'text2'=>$result1, 'text3'=>$result2, 'text4'=>$result3, 'text5'=>$result4]);
$this->testing_model->add_data('sess_table',['id'=>"default", 'val'=>$result5, 'val2'=>$result6, 'val3'=>$result7, 'val4'=>$result8]);
echo "Successfully Saved to Database!";
$this->session->unset_userdata('page1');
$this->session->unset_userdata('page2');
//$querydata=$this->testing_model->query('select * from sess_table');
//print "<pre/>";
// print_r($querydata);
// foreach ($querydata as $key => $value) {
// print_r(json_decode($value->val,true));
// print_r(json_decode($value->val2,true));
// // echo json_decode($value->val.' ---- '.$value->val2;
// }
}
}
}
View 1:
<!DOCTYPE html>
<html lang="en">
</head>
<body>
page 1
<form method="get" action="<?= base_url('welcome/page1') ?>">
input 1 <input name="input1" type="text"/> <br/>
input 2 <input name="input2" type="text"/> <br/>
input 3 <input name="input3" type="text"/> <br/>
input 4 <input name="input4" type="text"/> <br/>
<input type="submit" value="go"/>
</form>
</body>
</html>
View 2:
<!DOCTYPE html>
<html lang="en">
</head>
<body>
page 2
<form method="get" action="<?= base_url('welcome/page2') ?>">
input 1 <input name="input5" type="text" /> <br/>
input 2 <input name="input6" type="text" /> <br/>
input 3 <input name="input7" type="text" /> <br/>
input 4 <input name="input8" type="text" /> <br/>
<input type="submit" value="go"/>
<a type="button" href="<?= base_url('welcome/page2') ?>">Back</a>
</form>
</body>
View 3:
<html>
<head>
<title></title>
</head>
<body>
<form method="get" action="<?= base_url('welcome/page3') ?>">
<input type="submit" value="Save"/>
<a type="button" href="<?= base_url('welcome/page1') ?>">Back</a>
</form>
</body>
</html>
Load library session in construct
class Welcome extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('session');
}
Open your autoload.php in applications/config/autoload.php
$autoload['libraries'] = array('session');
The session data is the least of your problems.
First off, $this->load->view('pg2'); does not call your other functions in your controller, it loads a view named 'pg2'. In your question you didn't show the names of your views but if 'View 1' is a file named pg1.php in the /views folder, your controller function could look like this:
public function pg1() {
$this->session->set_userdata('page1',$this->input->get());
$this->load->view('pg1');
}
And then you have to learn how to pass data to display in views too... Try to read up on the docs, for example here's the page on how views work: https://www.codeigniter.com/user_guide/general/views.html

session destrioyed on add to cart in laravel

I am using laravel5. I need to add the product into cart. when i add a new product it is add into cart item as 1. but when refresh the page or move to next page my cart item count destroyed. Here i attach my coding. Please help me what is my mistake. i am new to laravel
Controller:
public function cart($request)
{
$quick=Mainpage::get_quick(5);
$logo = Mainpage::get_image();
$result_cart = Mainpage::get_add_to_cart_details();
$session_result = '';
// if(Session::get('cus_id'))
// {
// $navbar = View::make('layout.header');
// }
// else
// {
// $navbar = View::make('layout.header');
// }
return view('cart', ['logo' => $logo, 'quick' =>$quick,'session_result'=>$session_result,'result_cart'=>$result_cart]);
}
Model:
public static function get_add_to_cart_details()
{
$get_pro_dea = "";
if(isset($_SESSION['cart'])){
$max=count($_SESSION['cart']);
for($i=0;$i<$max;$i++){
$pid=$_SESSION['cart'][$i]['product_id'];
//$pname="Have to get";
$get_pro_dea[$pid] = DB::table('le_product')->where('product_id',$pid)->get();
}
}
else
{
$get_pro_dea[0] = array();
}
return $get_pro_dea;
}
View:(header.blade.php):
<div class="col-sm-4" style="margin-top:17px">
<div class="cart box_1 pull-right">
<a href="<?php echo url('cart'); ?>">
<?php if(isset($_SESSION['cart']))
{
$item_count_header1 = count($_SESSION['cart']);
}
else {
$item_count_header1 = 0;
}
$item_count_header = $item_count_header1;
if($item_count_header != 0)
{
?>
<img src='<?php echo url(); ?>/assets/images/shopping-cart.png' alt=''><span style='color:black;
'> ( <?php echo $item_count_header; ?> Items) </span>
<?php
}
else
{ ?>
<img src="<?php echo url(); ?>/assets/images/shopping-cart.png" alt=""><span style="color:black;"> (card empty)</span>
<?php }
?>
</strong>
</a>
</div>
</div>
</div>
<?php } ?>
</div>

Making Dependent Combobox in codeigniter project

i am suffering greatly but still unable to make a dependent dropdown box in Codeigniter .
Here is the schema :
groups(group_id,group)
forum(forum_id,subject)
group_forum(group_id,forum_id)
Here is my code :
model
function get_group(){
$query = $this->db->get('group');
return $query->result();
}
function get_subject_by_group($id)
{
$subjects=array();
$this->db->from('forum');
$this->db->join('group_forum','group_forum.forum_id=forum.forum_id','group_id='.$id);
$q=$this->db->get();
foreach($q->result() as $y)
{
$subjects[$y['forum_id']] = $y['subject'];
}
return $subjects;
}
}
Controller:
<?php
class C_control_form extends CI_Controller {
function add_all(){
#Validate entry form information
$this->load->model('Model_form');
$this->form_validation->set_rules('f_group', 'Group', 'trim|required');
$this->form_validation->set_rules('f_forum', 'Forum', 'trim|required');
$data['groups'] = $this->Model_form->get_group(); //gets the available groups for the dropdown
if ($this->form_validation->run() == FALSE)
{
$this->load->view('header_for_combo',$data);
$this->load->view('view_form_all', $data);
$this->load->view('footer_for_combo',$data);
}
else
{
#Add Member to Database
$this->Model_form->add_all();
$this->load->view('view_form_success');
}
}
function get_subjects($group)
{
//echo "hi";
$this->load->model('Model_form');
header('Content-Type: application/x-json; charset=utf-8');
echo (json_encode($this->Model_form->get_subject_by_group($group)));
}
}
?>
View
<html>
<head>
<script type="text/javascript" src="<?php echo base_url("js/jquery-1.7.2.min.js"); ?>" ></script>
<script type="text/javascript">
// $('#f_group, #f_forum').hide();
$(document).ready(function(){
$('#f_group').change(function(){
var group_id = $('#f_group').val();
if (group_id != ""){
var post_url = "index.php/c_control_form/get_subjects/"+group_id;
// var post_url = "<?php echo base_url();?>"+"c_control_form/get_subjects"+group_id;
$.ajax({
type: 'POST',
url: post_url,
dataType : 'json',
success: function(subjects) //we're calling the response json array 'cities'
{
$("#f_forum > option").remove();
// $('#f_forum').empty();
// $('#f_forum, #f_forum_label').show();
$.each(subjects,function(forum_id,subject)
{
var opt = $('<option/>'); // here we're creating a new select option for each group
opt.val(forum_id);
//alert(id);
opt.text(subject);
$('#f_forum').append(opt);
});
} //end success
}); //end AJAX
} else {
$('#f_forum').empty();
// $('#f_forum, #f_forum_label').hide();
}//end if
}); //end change
});
</script>
</head>
<body>
<?php echo form_open('c_control_form/add_all'); ?>
<p>
<label for="f_group">Group<span class="red">*</span></label>
<select id="f_group" name="f_group">
<option value=""></option>
<?php
foreach($groups as $group){
echo '<option value="' . $group->group_id . '">' . $group->group_name.'</option>';
}
?>
</select>
</p>
<p>
<label for="f_forum">Subject<span class="red">*</span></label>
<select id="f_forum" name="f_forum" id="f_forum_label">
<option value=""></option>
</select>
</p>
<?php echo form_close(); ?>
</body>
Please change
$subjects[$y['forum_id']] = $y['subject'];
to
$subjects[$y->forum_id] = $y->subject;
in the model file. Also spaces or newlines in model php file after and before the php tags.
EDIT: Added below
function(subjects){
var select = $('#f_forum').empty();
$.each(subjects.values, function(i,item) {
select.append( '<option value="'
+ item.id
+ '">'
+ item.name
+ '</option>' );
});

Resources