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;
}
Related
Right now I am writing a program in core PHP, but I am not able to write a perfect oops concept as I m rewriting the code many times.
for Eg.
public function all()
{
$init = new Database;
$sql = "SELECT * FROM categories";
// Result
$result = $init->conn->query($sql);
if ($result->num_rows > 0) {
?>
<table class="table table-hover">
<thead>
<tr>
<th>Action</th>
<th scope="col">id</th>
<th scope="col">Category name</th>
<th scope="col">Category description</th>
</tr>
</thead>
<tbody>
<?php
while ($row = $result->fetch_assoc()) {
?>
<tr>
<td>
<form action="update_categories.php?update_id=<?php echo $row['id'] ?>" method="post">
<button type="submit" class="btn btn-primary" name="update">Update</button>
</form>
<form action="delete_categories.php?id=<?php echo $row['id'] ?>" method="post">
<button type="submit" name="delete" class="btn btn-danger">Delete</button>
</form>
</td>
<td><?php echo $row["id"] ?></td>
<td><?php echo $row["category_name"] ?></td>
<td><?php echo $row["category_description"] ?></td>
</tr>
<?php
} ?>
</tbody>
<?php
} else {
?>
<table class="table table-hover">
<thead>
<tr>
<th scope="col">id</th>
<th scope="col">Category name</th>
<th scope="col">Category description</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">no result</th>
<td>no result</td>
<td>no result</td>
</tr>
</tbody>
<?php
}
}
So here you can see I have to fetch the data from the database but in this function, I have included Html too so if you want to add database data to another place I have to write the function for that again and fetch the values.
Is there any way I can only fetch the value and i can use the function to print it wherever I want.
I'm working with displaying an output when I count the value of the second query is greater than or equal to 2 , using the first query where second query schedule number is equal to the first query schedule number.
The line that I got an error:
if ($x>=2) {.'<button onclick="CreateRevision('.$row['id'].')" type="button" class="btn btn-primary"><span class="fa fa-copy"></span></button>'.} else{}.'.
I just got this message syntax error, unexpected'.'
My whole code is this:
<?php
//open connection to mysql db
include("db_connection.php");
$data = '
<table id="tb_jobsched" class="display nowrap table table-bordered" width="100%" cellspacing="0">
<thead>
<tr>
<th width="3%">#</th>
<th width="10%">Schedule Number</th>
<th width="20%">Item Name</th>
<th width="10%">Output</th>
<th width="10%">Date Created</th>
<th width="10%">Expected Date</th>
<th width="10%">Prepared By</th>
<th width="7%">Status</th>
<th width="7%"></th>
</tr>
</thead>
<tfoot>
<tr>
<th width="3%">#</th>
<th width="10%">Schedule Number</th>
<th width="20%">Item Name</th>
<th width="10%">Output</th>
<th width="10%">Date Created</th>
<th width="10%">Expected Date</th>
<th width="10%">Prepared By</th>
<th width="7%">Status</th>
<th width="7%"></th>
</tr>
</tfoot>';
$query = "SELECT * FROM prepressjobs WHERE pj_deleted = 0 ORDER BY pj_datecreated DESC";
if (!$result = mysqli_query($db,$query)) {
exit(mysqli_error());
}
// if query results contains rows then featch those rows
if(mysqli_num_rows($result) > 0)
{
$number = 1;
while($row = mysqli_fetch_assoc($result))
{
$data .= '<tr>
<td>'.$number.'</td>
<td>'.$row['pj_schednum'].'</td>
<td>'.$row['pj_itemname'].'</td>
<td>'.$row['pj_output'].'</td>
<td>'.$row['pj_datecreated'].'</td>
<td>'.$row['pj_expectedfinished'].'</td>
<td>'.$row['pj_preparedby'].'</td>
<td>'.$row['pj_status'].'</td>
<td>
<div class="btn-group" role="group" aria-label="Button group with nested dropdown">
<button onclick="GetJobDetails('.$row['id'].')" type="button" class="btn btn-primary"><span class="fa fa-gear"></span></button>
'.
$querya = "SELECT COUNT(rm_schednum) AS jobrevision FROM jobrevisions WHERE rm_schednum ='{$row['pj_schednum']}'";
$resulta= mysqli_query($db, $querya);
$rowa = mysqli_fetch_assoc($resulta);
$x=$rowa['jobrevision'];
if ($x>=2) {.'<button onclick="CreateRevision('.$row['id'].')" type="button" class="btn btn-primary"><span class="fa fa-copy"></span></button>'.} else{}.'
</div>
</td>
</tr>';
$number++;
}
}
else
{
// records now found
$data .= '<tr><td colspan="8">Records not found!</td></tr>';
}
$data .= '</table>';
echo $data;
?>
What is the following solution about this?
<?php
<?php
//open connection to mysql db
include("db_connection.php");
$data = '
<table id="tb_jobsched" class="display nowrap table table-bordered" width="100%" cellspacing="0">
<thead>
<tr>
<th width="3%">#</th>
<th width="10%">Schedule Number</th>
<th width="20%">Item Name</th>
<th width="10%">Output</th>
<th width="10%">Date Created</th>
<th width="10%">Expected Date</th>
<th width="10%">Prepared By</th>
<th width="7%">Status</th>
<th width="7%"></th>
</tr>
</thead>
<tfoot>
<tr>
<th width="3%">#</th>
<th width="10%">Schedule Number</th>
<th width="20%">Item Name</th>
<th width="10%">Output</th>
<th width="10%">Date Created</th>
<th width="10%">Expected Date</th>
<th width="10%">Prepared By</th>
<th width="7%">Status</th>
<th width="7%"></th>
</tr>
</tfoot>';
$query = "SELECT * FROM prepressjobs WHERE pj_deleted = 0 ORDER BY pj_datecreated DESC";
if (!$result = mysqli_query($db,$query)) {
exit(mysqli_error());
}
// if query results contains rows then featch those rows
if(mysqli_num_rows($result) > 0)
{
$number = 1;
while($row = mysqli_fetch_assoc($result))
{
$data .= "<tr>
<td>{$number}</td>
<td>{$row['pj_schednum']}</td>
<td>{$row['pj_itemname']}</td>
<td>{$row['pj_output']}</td>
<td>{$row['pj_datecreated']}</td>
<td>{$row['pj_expectedfinished']}</td>
<td>{$row['pj_preparedby']}</td>
<td>{$row['pj_status']}</td>
<td>
<div class='btn-group' role='group' aria-label='Button group with nested dropdown'>
<button onclick='GetJobDetails({$row['id']})' type='button' class='btn btn-primary'><span class='fa fa-gear'></span></button>
";
$querya = "SELECT COUNT(rm_schednum) AS jobrevision FROM jobrevisions WHERE rm_schednum ='{$row['pj_schednum']}'";
$resulta= mysqli_query($db, $querya);
$rowa = mysqli_fetch_assoc($resulta);
$x = $rowa['jobrevision'];
if ($x>=2) {
$data .= "<button onclick='CreateRevision({$row['id']})' type='button' class='btn btn-primary'><span class='fa fa-copy'></span></button>";
} else{}
$data .= "</div>
</td>
</tr>";
$number++;
}
}
else
{
// records now found
$data .= "<tr><td colspan='8'>Records not found!</td></tr>";
}
$data .= "</table>";
echo $data;
?>
Following is my form:
<form method="POST" action="../controller/assignsubteacher.php">
<table class="table table-bordered table-striped table-hover table-condensed" id="coursedetail" >
<thead>
<tr>
<th>Sub Id</th>
<th>Sub Name</th>
<th>Teacher Name</th>
</tr>
</thead>
<tbody id="table_ajax">
</tbody>
<tfoot>
<tr>
<th>Sub Id</th>
<th>Sub Name</th>
<th>Teacher Name</th>
</tr>
</tfoot>
</table>
<div class="col-md-2">
<button type="submit" class="btn btn-primary btn-block btn-flat">Submit</button>
</div>
</form>
and table in the form is populated by following response:
while($row=mysqli_fetch_array($result))
{
$list='<select id="teacher" name="teacher'.$COUNT.'" class="form-control">
<option value = "UNKNOWN" selected="select">-SELECT-</option>';
$get_teacher="select Regno,Name from Student_Registration inner join Login on Regno=Uname where Id=2;";
$teacher_list = mysqli_query($con,$get_teacher);
while($row_Teacher=mysqli_fetch_array($teacher_list))
{
$list.='<option value="'.$row_Teacher['Regno'].'">'.$row_Teacher['Name'].'</options>';
}
$list.='</select>';
$Subject_ID=$row["SubId"].'<input type="hidden" name="SubId'.$COUNT.'" value="'.$row["SubId"].'">';
//$Subject_Name=$row["Subject_Name"].'<input type="hidden" name="SubName'.$COUNT.'" value="'.$row["Subject_Name"].'">';
$Subject_Name=$row["Subject_Name"];
$tr.='<tr>
<td>'.$Subject_ID.'</td>
<td>'.$Subject_Name.'</td>
<td>'.$list.'</td>
</tr>';
$COUNT=$COUNT+1;
}
echo $tr;
I am not able to use the posted data to insert in to database. Is there any way i can send the data as Array and retrieve it.
following is the AJAX to populate the table body:
xhr.onreadystatechange = function()
{
if (this.readyState == 4 && this.status == 200)
{
console.log(xhr.responseText);
Table.innerHTML=xhr.responseText;
}
};
I was thinking of using foreach to insert data in the POST controller, but have no idea how to achieve that.
any help would be appreciated.
Use SQL insert query to do so.
while($row=mysqli_fetch_array($result))
{
$list='<select id="teacher" name="teacher'.$COUNT.'" class="form-control">
<option value = "UNKNOWN" selected="select">-SELECT-</option>';
$get_teacher="select Regno,Name from Student_Registration inner join Login on Regno=Uname where Id=2;";
$teacher_list = mysqli_query($con,$get_teacher);
while($row_Teacher=mysqli_fetch_array($teacher_list))
{
$list.='<option value="'.$row_Teacher['Regno'].'">'.$row_Teacher['Name'].'</options>';
}
$list.='</select>';
$Subject_ID=$row["SubId"].'<input type="hidden" name="SubId'.$COUNT.'" value="'.$row["SubId"].'">';
//$Subject_Name=$row["Subject_Name"].'<input type="hidden" name="SubName'.$COUNT.'" value="'.$row["Subject_Name"].'">';
$Subject_Name=$row["Subject_Name"];
$tr.='<tr>
<td>'.$Subject_ID.'</td>
<td>'.$Subject_Name.'</td>
<td>'.$list.'</td>
</tr>';
$COUNT=$COUNT+1;
$sql = "INSERT INTO `table` (column1, column2, column3) VALUES ($list, $subject_name, $tr)";
//Replace columns & table name
if(mysqli_query($con, $sql)) {
echo "Inserted";
}
}
echo $tr;
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;
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);
}
});
}