Not able to display data through ajax in codeigniter - php

i want to show the restaurant after selecting its area from dropdown list. but my code did not show the restaurant name and the menu button of that restaurant please tell me where i did mistake
Its my model code
function select_record($table, $where = NULL)
{
$this->db->select();
if ($where) $this->db->where($where);
$this->db->from($table);
$query = $this->db->get();
// echo $this->db->last_query();
return $query->result();
}
Controller code
public function get_rests()
{
$cit_id = $this->input->post('cit_id');
$area = $this->input->post('areaID');
$where = array(
'city_id' => $cit_id,
'city_area_id' => $area
);
$data = $this->bulk->select_record('restaurant', $where);
$html = '<div class="container" id="">
<table align="centre" class="table table-condensed table-striped table-hover no-margin"style="width:70%" id="">
<thread>
<tr style="width: 56%;">
<th> No. </th>
<th style=""> Restaurant Names </th>
</tr>
</thread>
<tbody>
<th> <span value="'. $data[0]->restaurant_id . '" class="res_id"></span></th>
<th style=""> </th>
<th style=""> <span value="'. $data[0]->restaurant_name . '" class="res_id"></span> </th>
<th style="width: 1%" > </i>See Menu </th>
</tr>
</tbody>
</table>
</div>';
echo json_encode(array(
'data' => $html
));
}
Script code
function get_rests() {
var city_id = $('#city_id').val();
var area_id = $("#area_id").val();
$.ajax({
type: "POST",
url: "<?=base_url();?>index.php/Bulk_Controller/get_rests",
data: {
cit_id: city_id,
areaID: area_id
},
dataType: 'json',
cache: false,
success: function (response) {
alert(response);
$('#restaurant').html(response.data);
}
});
}
Its view code
<div id="restaurant">
</div>
when i did alert(response.data);
<div class="container" id="">
<table align="centre" class="table table-condensed table-striped table-hover no-margin"style="width:70%" id="">
<thread>
<tr style="width: 56%;">
<th>
No.
</th>
<th style="">
Restaurant Names
</th>
</tr>
</thread>
<tbody>
<th>
<span value="1" class="res_id"></span></th>
<th style="">
</th>
<th style="">
<span value="salten paper" class="res_id"></span>
</th>
<th style="width: 1%" >
</i>See Menu
</th>
</tr>
</tbody>
</table>
</div>
Please tell me where i did mistake

You are not putting the response from ajax properly. $('#restaurant').html(response.data); should be:
$('#restaurant').html(response);
Also change
url: "<?=base_url();?>index.php/Bulk_Controller/get_rests",
to
url: "<?=base_url();?>index.php/bulk_controller/get_rests",

Try the below,
You should return it rather than echo, Also you have to set the request header as json
Model:
function select_record($table, $where = NULL)
{
$this->db->select('*');
$this->db->from($table);// this should come right after select statement
if ($where) $this->db->where($where);
$query = $this->db->get();
// echo $this->db->last_query();
return $query->result();
}
Controller:
public function get_rests()
{
$cit_id = $this->input->post('cit_id');
$area = $this->input->post('areaID');
$where = array(
'city_id' => $cit_id,
'city_area_id' => $area
);
$data = $this->bulk->select_record('restaurant', $where);
$html = '<div class="container" id="">
<table align="centre" class="table table-condensed table-striped table-hover no-margin"style="width:70%" id="">
<thread>
<tr style="width: 56%;">
<th> No. </th>
<th style=""> Restaurant Names </th>
</tr>
</thread>
<tbody>
<th> <span value="'. $data[0]->restaurant_id . '" class="res_id"></span></th>
<th style=""> </th>
<th style=""> <span value="'. $data[0]->restaurant_name . '" class="res_id"></span> </th>
<th style="width: 1%" > </i>See Menu </th>
</tr>
</tbody>
</table>
</div>';
return $this->output->set_content_type('application/json')->set_output(json_encode(array(
'data' => $html,
)));
}
Script:
function get_rests() {
var city_id = $('#city_id').val();
var area_id = $("#area_id").val();
$.ajax({
type: "POST",
url: "<?=base_url();?>index.php/Bulk_Controller/get_rests",
data: {
'cit_id': city_id,
'areaID': area_id
},
dataType: 'json',
cache: false,
success: function (response) {
alert(response);
console.log(response)
$('#restaurant').html(response.data);
}
});
}

Related

How can I display values from a table that correspond to their matching id

So in this query below im joining two tables through order_id and displaying the all the values from the user_orders table.
As per the image below I am trying to display only the order_Id rows that match the order_manager table.
public function getUserOrder(){
$sql = "SELECT user_orders.order_id,
user_orders.title, user_orders.price,
user_orders.quantity
FROM order_manager
JOIN user_orders ON order_manager.order_id = user_orders.order_id;";
$stmt = $this->connect()->prepare($sql);
$stmt->execute();
while ($result = $stmt->fetchAll()){
return $result;
}
}
I have attempted to use an if statement that appears to do something however it gives me the values that don't match order id in reverse.
<div class="container mt-5">
<?php $artworks = new Artworks(); ?>
<div class="row">
<div class="col-lg-12">
<table class="table table-dark">
<thead>
<tr>
<th scope="col">Order ID</th>
<th scope="col">Full Name</th>
<th scope="col">Phone</th>
<th scope="col">Address</th>
<th scope="col">Orders</th>
</tr>
</thead>
<?php
$artworks->getOrder();
foreach ($artworks->getOrder() as $art) {
echo "<tbody>
<tr>
<td>$art[order_id]</td>
<td> $art[full_name]</td>
<td> $art[phone] </td>
<td>$art[address]</td>
<td>
<table class= 'tale text-center table-dark'>
<thead>
<tr>
<th scope='col'>Order ID</th>
<th scope='col'>title</th>
<th scope='col'>price</th>
<th scope='col'>Quantity</th>
</tr>
<thead>
<tbody>
<tr>";
$artworks->getUserOrder();
foreach ($artworks->getUserOrder() as $order) {
if ($order['order_id'] == $art['order_id']) {
echo "<td>$order[order_id]</td>";
}
echo "
<td>$order[title]</td>
<td>$order[price]</td>
<td>$order[quantity]</td>
</tr>";
}
echo "
</tbody>
</table>
</td>
</tr>
";
}
?>
</tbody>
</table>
</div>
</div>
</div>
Here is an image to help explain the desired output
Solved moving the td items into the if statement. Guess it was mostly correct.
$artworks->getUserOrder();
foreach($artworks->getUserOrder() as $order)
{
if ($order['order_id'] == $art['order_id']) {
echo "<td>$order[order_id]</td>";
echo "
<td>$order[title]</td>
<td>$order[price]</td>
<td>$order[quantity]</td>
</tr>
";
}
}

Capturing passed modal value and converting it into php query parameter

EDIT: May I ask for assistance regarding this code?
This is my modal. My input text receives a value of AAA-123-0001
This script is being included into the main page via include()
<input type="hidden" class="decid" id="id" name="id">
<?php include 'includes/services_payslip_list_payslip-selection.php'; ?>
This is my services_payslip_list_payslip-selection.php
<script>
$(document).ready(function() {
$("#id").change(function() {
var id = $(this).val();
if(id != "") {
$.ajax({
url:"services_payslip_list-payslip-table.php",
data:{id:id},
type:'POST',
success:function(response) {
var resp = $.trim(response);
$("#id").html(resp);
}
});
}
});
</script>
This is my services_payslip_list-payslip-table.php
<table id="example2" class="table table-bordered">
<thead>
<th>Schedule Date</th>
<th>Schedule Name</th>
<th>Recorded In</th>
<th>Recorded Out</th>
<th>Day Count</th>
<th>Day Value</th>
<th>N.D. Value</th>
<th>Leave Count</th>
<th>R.H. Count</th>
<th>R.H. Value</th>
</thead>
<tbody>
<?php
include 'includes/session.php';
if(isset($_POST['id'])) {
$id=$_POST['id']
}
$user = $user['fingerscanno'];
$sql = "SELECT fingerscanno, scheduledate, schedulename, recordin, recordout, noofdays, rate, nightdifferential, leaveday, regularholiday, specialholiday, referenceno
FROM payrollrecords WHERE fingerscanno='$user' and referenceno='$id'";
$query = sqlsrv_query($conn, $sql, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
while($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)){
echo "
<tr>
<td>".$row['scheduledate']."</td>
<td>".$row['schedulename']."</td>
<td>".$row['recordin']."</td>
<td>".$row['recordout']."</td>
<td>".$row['noofdays']."</td>
<td>".$row['rate']."</td>
<td>".$row['nightdifferential']."</td>
<td>".$row['leaveday']."</td>
<td>".$row['regularholiday']."</td>
<td>".$row['specialholiday']."</td>
</tr>
";
}
?>
</tbody>
</table>
What seems to be the problem here? services_payslip_list_payslip-selection.php won't load into the modal.

How to make search with condition using ajax and codeigniter

I want to make search within specific condition using codeigniter framework
when I press inside my view the condition doesn't happen
($this->db->where('del_status','1');)
I think my problem in query to data base.
my data base model
public function SearchData($tablename,$data)
{
//select * from users where username like'%ss%' AND del_status ='1'
$this->db->select("*");
$this->db->from($tablename);
$this->db->like('username', $data);
$this->db->or_like('fullname', $data);
$this->db->or_like('email', $data);
$this->db->where('del_status','1');
$sql = $this->db->get();
return $sql->result();
}
this is my controller
public function Search()
{
if($this->input->post("action") =="searchonusers" )
{
$data=$this->input->post("data");
$this->load->model("DatabaseModel");
$serachdata=$this->DatabaseModel->SearchData("users",$data);
if(is_array($serachdata)&&count($serachdata)>0)
{
echo '<div class="col-lg-12">
<div class="userdataa table-responsive">
<table class=" table table-responsive table-bordered table-hover ">
<tr>
<th>id</th>
<th>user name</th>
<th>full name</th>
<th> image</th>
<th>email</th>
<th>type</th>
<th>status</th>
<th>date</th>
<th>time</th>
<th>delete</th>
<th>edit</th>
<th>Activate</th>
</tr>';
foreach ($serachdata as $user):
echo'<tr id="'."user".$user->id.'">
<td>'.$user->id.'</td>
<td>'.$user->username.'</td>
<td>'.$user->fullname.'</td>
<td><img class="img-responsive" height="40%" width="40%" src="'.base_url()."upload/".$user->image.'"></td>
<td>'.$user->email.'</td>';
$usertype='';
if($user->type== '1'){
$usertype=" <label class='label label-warning'>User</label>";
}else{
$usertype="<label class='label label-danger '> Admin</label>";
}
echo '<td>'.$usertype.'</td>';
$acativate='';
$status='';
$regstatus=$user->status;
if($regstatus == '0'){
$acativate='<button class="btn btn-success btn-xs showsta" status="'.$user->status.'" id="'.$user->id.'">enable</button>';
$status='<label class="label label-danger">disactive</label>';
}else{
$acativate='<button class="btn btn-xs btn-danger showsta" status="'.$user->status.'" id="'.$user->id.'">disable</button>';
$status='<label class=" label label-success">active</label>';
}
echo' <td>'.$status.'</td>
<td>'.$user->date.'</td>
<td>'.$user->time.'</td>
<td><button class="deleteuserfirst btn btn-danger btn-xs" id="'.$user->id.'"><span class="glyphicon glyphicon-trash"></span>delete</button></td>
<td><button type="button" class="btn btn-primary btn-xs edituser" id="'.$user->id.'" data-toggle="modal" data-target="#usereditorwe"><span class="glyphicon glyphicon-edit">Edit</button>
<td> '.$acativate.'</td>
</tr>';
endforeach;
echo'</table>
</div>
</div>';
}else{
echo '<div class="col-lg-12">
<div class="userdataa table-responsive">
<table class=" table table-responsive table-bordered table-hover">
<tr>
<th>id</th>
<th>user name</th>
<th>full name</th>
<th> image</th>
<th>email</th>
<th>type</th>
<th>status</th>
<th>date</th>
<th>time</th>
<th>delete</th>
<th>edit</th>
<th>Activate</th>
</tr>
<tr>
<td colspan="12">No Data Found</td>
</tr>
</table>
</div>';
}
}else{
exit();
}
}
}
this is my ajax code
$(".serchdata").on("click",function () {
var action="searchonusers";
var data=$("#dataa").val();
if(data ==""){
alert("Please Insert Your Search Data ... ")
}else{
$.ajax({
url:"<?php echo base_url("Users/Search")?>",
method: "post",
data:{action:action,data:data},
success:function (data) {
$(".userdata").remove();
$(".box").remove();
$("#pagination_link").remove();
$(".serchresult").html(data)
Table();
}
})
}
});
You are checking is_array on an object type result() :
if(is_array($serachdata)&&count($serachdata)>0)
to check for an array, use result_array() instead :
return $sql->result_array();
Please replace your model with that
public function SearchData($tablename,$data)
{
//select * from users where username like'%ss%' AND del_status ='1'
$this->db->select("*");
$this->db->from($tablename);
$this->db->like('username', $data);
$this->db->or_like('fullname', $data);
$this->db->or_like('email', $data);
$this->db->where('del_status','1');
$sql = $this->db->get()->result_array();
return $sql;
}

Unable to make the html of the data in the controller and show on the dropdown select

i want to show the restaurant after selecting its area from dropdown list. but my code did not show the restaurant name and the menu button of that restaurant please tell me where i did mistake
This is my view code
<div id="restaurant">
</div>
This is controller code
public function get_rests()
{
$cit_id = $this->input->post('cit_id');
$area = $this->input->post('areaID');
$where = array('city_id'=>$cit_id,'city_area_id'=>$area);
$html = '
<div class="container" id="">
<table align="centre" class="table table-condensed table-striped table-hover no-margin"style="width:70%" id="">
<thread>
<tr style="width: 56%;">
<th>
No.
</th>
<th style="">
Restaurant Names
</th>
</tr>
</thread>
<tbody>
<?php $i=1;foreach($result as $row){
?>
<tr id="<?php echo $row->restaurant_id; ?>" class="res_id">
<th style="">
<?php echo $i++; ?>
</th>
<th style="">
<?php echo $row->restaurant_name; ?>
</th>
<th style="width: 1%" >
</i>See Menu
</th>
</tr>
<?php } ?>
</tbody>
</table>
</div>
';
echo $html;
This is my model code view
function select_record($table, $where = NULL){
$this->db->select();
if($where)
$this->db->where($where);
$this->db->from($table);
$query = $this->db->get();
// echo $this->db->last_query();
return $query->result();
}
Script code
function get_rests(){
var city_id = $('#city_id').val();
var area_id = $("#area_id").val();
$.ajax({
type: "POST",
url: "<?=base_url();?>index.php/Bulk_Controller/get_rests",
data: {cit_id: city_id,areaID: area_id},
dataType: "text",
cache:false,
success:
function(data){
// alert(data);
$('#restaurant').html(data);
}
});
}
You didn't called a model function to get data from your DB Table.
public function get_rests()
{
$cit_id = $this->input->post('cit_id');
$area = $this->input->post('areaID');
$where = array('city_id'=>$cit_id,'city_area_id'=>$area);
$result= $this->your_model->select_record('table_name',$where);
$html = '
<div class="container" id="">
<table align="centre" class="table table-condensed table-striped table-hover no-margin"style="width:70%" id="">
<thread>
<tr style="width: 56%;">
<th>
No.
</th>
<th style="">
Restaurant Names
</th>
</tr>
</thread>
<tbody>
<?php $i=1;foreach($result as $row){
?>
<tr id="<?php echo $row->restaurant_id; ?>" class="res_id">
<th style="">
<?php echo $i++; ?>
</th>
<th style="">
<?php echo $row->restaurant_name; ?>
</th>
<th style="width: 1%" >
</i>See Menu
</th>
</tr>
<?php } ?>
</tbody>
</table>
</div>
';
echo $html;

Getting multi rows in the table even if my database has no value using angular.js and PHP

i have one problem in my angular.js code.I have a table which will filled up by database value. when initially my database has no value and table should display blank but in my case now the database is empty still the table is splitting into 5 rows which has no value.I am explaining my code below.
subject.html:
<table class="table table-bordered table-striped table-hover" id="dataTable">
<colgroup>
<col class="col-md-1 col-sm-1">
<col class="col-md-2 col-sm-2">
<col class="col-md-2 col-sm-2">
<col class="col-md-2 col-sm-2">
<col class="col-md-2 col-sm-2">
<col class="col-md-2 col-sm-2">
<col class="col-md-1 col-sm-1">
</colgroup>
<thead>
<tr>
<th>Sl. No</th>
<th>
<select style="width:100%; border:none; font-weight:bold;">
<option>Course Name</option>
</select>
</th>
<th>Semester</th>
<th>Subject Name</th>
<th>Short Name</th>
<th>Edit</th>
</tr>
</thead>
<tbody id="detailsstockid">
<tr ng-repeat="sub in subjectData" >
<td>{{ $index+1 }}</td>
<td>{{sub.course_name}}</td>
<td>{{sub.semester}}</td>
<td>{{sub.subject_name}}</td>
<td >{{sub.short_name}}</td>
<td>
<a href=''>
<input type='button' class='btn btn-xs btn-green' value='Edit'>
</a>
</td>
</tr>
</tbody>
</table>
subjectController.js:
var subApp=angular.module('GofastoHome');
subApp.controller('subjectcontroller',function($scope,$http){
$http({
method: 'GET',
url: "php/subject/readSubjectData.php",
}).then(function successCallback(response) {
console.log('response',response);
$scope.subjectData=angular.fromJson(response);
},function errorCallback(response) {
alert("could not fetch data");
});
})
Here initially my database has no value.When user is refreshing(i.e-subject.html) the page the table present in the html content is splitting into 5 rows with no data but 5 serial number is coming which i don't need.check my php file below.
readSubjectData.php:
$connect = mysqli_connect("localhost", "root", "******", "go_fasto");
$result = mysqli_query($connect, "select * from db_subject order by subject_id desc ");
$data = array();
while ($row = mysqli_fetch_assoc($result)) {
$data[] = $row;
}
print json_encode($data);
My requirement is table will be splitted only when database has some value.When data database is blank it should not which i am getting in my current code.Please help me.
Try this:
Add script ng-show="showtable" to table tag which will become like this:
<table ng-show="showtable" class="table table-bordered table-striped table-hover" id="dataTable">
Then change your controller script like below:
var subApp=angular.module('GofastoHome');
subApp.controller('subjectcontroller',function($scope,$http){
$http({
method: 'GET',
url: "php/subject/readSubjectData.php",
}).then(function successCallback(response) {
console.log('response',response);
$scope.subjectData=angular.fromJson(response);
if($scope.subjectData.length > 0) //
$scope.showtable = true; // add these lines
else //
$scope.showtable = false; //
},function errorCallback(response) {
alert("could not fetch data");
});
})

Categories