(Codeigniter) Echo result in view automatic insert to database - codeigniter

I want echo from if/else conditions (NEW, ALERT, ACTIVE, EXPIRED) automatic input to tr_status column in database. What should I do?
column picture phpmyadmin
<td>
<?php
$today = Date("Y-m-d");
$months = Date("Y-m-d", strtotime('+180 days'));
if ($t->tr_no == NULL) {
echo '<span style="color: #20B2AA; font-weight: bold; ">NEW</span>';
}
elseif ($t->tr_exp <= $today) {
echo '<span style=" color: red; font-weight: bold; ">EXPIRED</span>';
}
elseif ($t->tr_exp <= $months) {
echo '<span style="font-weight: bold; color: ORANGE;">ALERT</span>';
}
else {
echo '<span style="font-weight: bold; color: #4DBE24;">ACTIVE</span>';
}
?>
</td>

you need to create a function inside the contoller before load the view file
example :
$today = Date("Y-m-d");
$months = Date("Y-m-d", strtotime('+180 days'));
if ($t->tr_no == NULL) {
$object = array('tr_status' => 'NEW');
}elseif ($t->tr_exp <= $today) {
$object = array('tr_status' => 'EXPIRED');
} else {
$object = array('tr_status' => 'ACTIVE');
}
$this->db->insert('Table', $object);

Related

daterange with timerange using codeigniter 3 and ajax

I have a daterange picker and it is fully working. However, I have a new target which is daterange with timepicker. But the thing is I don't know how to implement it like what I did on my daterange without timepicker. Is there any way where I can implement it? I have provided my codes below of my fully working daterange picker. I have provided also my screenshot of my current daterange and my target daterange with timepicker. Thank you in advance.
HTML:
<div class="img-fluid">
<div class="input-group">
From:
<input type="date" class="mr-4" id="from" name="from">
To:
<input type="date" id="to" name="to" style="margin-right:50px;">
</div>
</div>
<div class="tab-pane fade show active" id="custom-tabs-two-1" role="tabpanel" aria-labelledby="custom-tabs-two-1-tab">
<table id="testing" class="table-striped table table-head-fixed" style="min-width:1000px; width:100%;">
<thead class="" style="background-color: #404040; color: white;">
<tr>
<th scope="col"><i class="fas fa-list-ol mr-2"></i>Transaction ID</th>
<th scope="col"><i class="fas fa-list-ol mr-2"></i>Reference No.</th>
<th scope="col"><i class="far fa-calendar-alt mr-2"></i>Date Requested</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
Script & Ajax:
var save_method; //for save method string
var table;
var base_url = '<?php echo base_url();?>';
$(document).ready(function() {
//datatables
var table = $('#testing').DataTable({
dom: 'lBfrtip',
buttons: [
'print', 'csv', 'copy', 'excel', 'pdfHtml5'
],
"processing": false, //Feature control the processing indicator.
"serverSide": true, //Feature control DataTables' server-side processing mode.
"order": [], //Initial no order.
// Load data for the table's content from an Ajax source
"ajax": {
"url": "<?php echo site_url('controller/ajax_list')?>",
"type": "POST",
"data": function ( data ) {
data.from = $('#from').val();
data.to = $('#to').val();
},
},
//Set column definition initialization properties.
"columnDefs": [
{
"targets": [ 0 ], //first column
"orderable": false, //set not orderable
},
{
"targets": [ -1 ], //last column
"orderable": false, //set not orderable
},
],
});
setInterval( function () {
testing.ajax.reload(null,false);
}, 1000);
Controller:
public function ajax_list()
{
$list = $this->repo->admin();
$data = array();
$no = $_POST['start'];
foreach ($list as $person) {
$no++;
$row = array();
$row[] = $person->transID;
$row[] = $person->refNumber;
$row[] = $person->dateRequested;
$data[] = $row;
}
$output = array(
"draw" => $_POST['draw'],
"recordsTotal" => $this->repo->showing_pending(),
"recordsFiltered" => $this->repo->count_filtered(),
"data" => $data,
);
//output to json format
echo json_encode($output);
}
}
Model:
var $table = 'ca';
var $column_order = array(null,'transID','dateRequested','refNumber');
var $order = array('transID' => 'desc');
var $column_search = array('transID','dateRequested','refNumber');
//set column field database for datatable orderable //set column field database for datatable searchable just firstname , lastname , address are searchable var $order = array('id' => 'desc'); // default order
private function _get_datatables_query()
{
if($this->input->post('from')){
$this->db->where('dateCre >=', $this->input->post('from'));
$this->db->where('dateCre <=', $this->input->post('to'));
}
$this->db->from('ca');
$i = 0;
foreach ($this->column_search as $item)
{
if($_POST['search']['value'])
{
if($i===0) // first loop
{
$this->db->group_start();
$this->db->like($item, $_POST['search']['value']);
}
else
{
$this->db->or_like($item, $_POST['search']['value']);
}
if(count($this->column_search) - 1 == $i)
$this->db->group_end();
}
$i++;
}
if(isset($_POST['order']))
{
$this->db->order_by($this->column_order[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
}
else if(isset($this->order))
{
$order = $this->order;
$this->db->order_by(key($order), $order[key($order)]);
}
}
function admin()
{
$this->_get_datatables_query();
if($_POST['length'] != -1)
$this->db->limit($_POST['length'], $_POST['start']);
$query = $this->db->get();
return $query->result();
}
public function showing_pending()
{
$this->db->from($this->table);
return $this->db->count_all_results();
}
function count_filtered()
{
$this->_get_datatables_query();
$query = $this->db->get();
return $query->num_rows();
}
}
What is your exact error? I think that you need to sanitize your post values before the query.
Change This:
$this->db->where('dateCre >=', $this->input->post('from'));
To this (please select the correct format in your case)
$from_date = DateTime::createFromFormat('d/m/Y h:m:s', $this->input->post('from'));
$this->db->where('dateCre >=', $from_date->format('Y-m-d h:m:s'));
Do it also with the "post('to')" value.

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'/>"]
});

background-image doesn't work in foreach loop

I have a foreach loop and slider, and into loop I have background image. Background image is working for some, but not all of them.
This is my code:
#if($products)
#foreach($products as $product)
<div class="swiper-slide text-center">
<a href="{{url('products/'.$product->id)}}">
<div class="border-2"
style="background-image: url({{src($product,'main_image')}})"></div>
<div class="mt-1">{{$product->title}}</div>
</a>
</div>
#endforeach
#endif
src is a function
function src($object,$slug,$thumb_size = null) {
$table = $object->getTable();
$width = ($thumb_size)?explode('/',$thumb_size)[0]:false;
$height = ($thumb_size)?explode('/',$thumb_size)[1]:false;
if($attachment = $object->attachments($slug)->first()) {
if($width && $height) {
$src = url("/uploads/".$table."/thumb_{$width}-{$height}_".$attachment->file);
} else {
$src = url("/uploads/".$table."/".$attachment->file);
}
} else {
if($width && $height) {
if(file_exists("uploads/defaults/thumb_{$width}-{$height}_default.png")) {
$src = url('')."/uploads/defaults/thumb_{$width}-{$height}_default.png";
} else {
Image::make("default.png")->fit($width,$height)->save("uploads/defaults/thumb_{$width}-{$height}_default.png");
$src = url('')."/uploads/defaults/thumb_{$width}-{$height}_default.png";
}
} else {
$src = url('')."/default.png";
}
}
return $src;
}

Table inside a datatable cell laravel 5.2

I am using laravel 5.2 framework. I have successfully implemented datatables. By application design requirement, how to implement tables within the datatable cell?
Its not really directly possible unless you define your own table and style it with css as follows
styes for my inner table defined on my view
<style type="text/css">
h3 {
color: steelblue;
}
.subtabletable {
display: table;
width: 100%;
}
.subtable .row {
display: table-row;
}
.subtable .header {
font-weight: bold;
}
.subtable .cell {
display: table-cell;
padding: 5px;
}
</style>
Definition of my table in Html
<table id="example" class="display responsive nowrap" cellspacing="0" width="100%">
My javascript
$(document).ready(function () {
var request = $.ajax({
url: "sort_respones",
type: "GET",
dataType: "html"
});
request.done(function(msg){
dynamicdatatable(msg);
});
});
function dynamicdatatable(mydata){
var myData=JSON.parse(mydata) ;
console.log(myData);
var myColumns = [
{
title : "Employee",
data : "question"
},
{
title : "Type",
data : "response_type"
},
{
title : "Responses",
data : null,
render : function (data, type, row, meta) {
var subtableHtml = "<div class='subtable'>";
subtableHtml += "<div class='row header'><span class='cell'>Response</span><span class='cell'>Occurances</span></div>";
for (var i = 0; i < data.projects.length; i++) {
subtableHtml += "<div class='row'>";
subtableHtml += "<span class='cell'>" + data.projects[i].project + "</span>";
subtableHtml += "<span class='cell'>" + data.projects[i].role + "</span>";
subtableHtml += "</div>";
};
subtableHtml += "</div>";
return subtableHtml;
}
}
];
$('#example').dataTable({
searching: false,
paging: false,
data : myData,
columns : myColumns,
dom: 'Bfrtip',
buttons: [
'copyHtml5',
'excelHtml5',
'csvHtml5',
'pdfHtml5'
]
});
}
My controller function
function sort_respones(Request $request){
$results = DB::select( DB::raw("SELECT
questions.id,
questions.question,
questions.client_id,
surveys_session_answer_transactions.created_at,
surveys_session_answer_transactions.answer_text,
surveys_session_answer_transactions.answer_id,
surveys_session_answer_transactions.question_text,
questions.response_type_id,
Count(surveys_session_answer_transactions.answer_id) AS Occurances,
checkresponses.response,
radioresponses.response,
rating_bar_responses.maximum_value,
response_types.response_type
FROM
questions
INNER JOIN surveys_session_answer_transactions ON surveys_session_answer_transactions.question_id = questions.id
LEFT OUTER JOIN checkresponses ON checkresponses.question_id = surveys_session_answer_transactions.question_id AND checkresponses.id = surveys_session_answer_transactions.answer_id
LEFT OUTER JOIN rating_bar_responses ON rating_bar_responses.question_id = surveys_session_answer_transactions.question_id
LEFT OUTER JOIN radioresponses ON radioresponses.question_id = surveys_session_answer_transactions.question_id AND radioresponses.id = surveys_session_answer_transactions.answer_id
INNER JOIN response_types ON surveys_session_answer_transactions.response_type_id = response_types.type_id
where questions.client_id=:somevariable
GROUP BY
surveys_session_answer_transactions.question_id,
surveys_session_answer_transactions.answer_id"), array(
'somevariable' => "2",
));
//echo '<pre>'.print_r($results,1) .'</pre>';
// echo '<pre>';
// var_dump($results);
// echo '</pre>';
$new_question=0;
$dataUsage=[];
$final_array=[];
$responses=[];
$old_response="";$old_response_type="";
foreach ($results as $key) {
# code...
if ($new_question!=$key->id) {
# code...
if (isset($dataUsage[0])) {
# code...
array_push($final_array, array('question' =>$old_response,'response_type'=>$old_response_type,'projects' => ($responses)));
$responses=[];
//array_push($final_array, $dataUsage);
$dataUsage=[];
$responses_string="<table>";
}
array_push($dataUsage, array('question' => $key->question,'response_type'=>$key->response_type ));
$old_response=$key->question;
$old_response_type=$key->response_type;
// $responses_string='<TR><TD>'.$key->answer_text.'</TD><TD>: '.$key->Occurances.'<br></TD></TR>';
array_push($responses,array('project' => $key->answer_text, 'role' => $key->Occurances ));
}else{
// $responses_string=$responses_string.'<TR><TD>'.$key->answer_text.'</TD><TD> :'.$key->Occurances.'<br></TD></TR>';
array_push($responses,array('project' => $key->answer_text, 'role' => $key->Occurances ));
}
$new_question=$key->id;
}
array_push($final_array, array('question' =>$old_response,'response_type'=>$old_response_type,'projects' => ($responses)));
// echo '<pre>';
// var_dump($final_array);
// echo '</pre>';
// echo json_encode( array('data' =>$final_array ));
// $myData = array( array('employee' =>'Bob Smith', 'projects'=> array(array('project'=>'Alpha', 'role'=>'leader' ), array('project'=>'Alpha1', 'role'=>'leader1' ),array('project'=>'Alpha', 'role'=>'leader' ))));
echo json_encode($final_array);
}
of course don't forget the necessary routes

passing multiple checkbox values to controller

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);
}

Resources