Codeigniter get parameters from url? - codeigniter

I have one view
<div class="panel-body">
<div class="table-responsive">
<table id="mytable" class="table table-responsive table-striped table-bordered" cellspacing="0"
width-="100%">
<tbody>
<?php
foreach ($result as $row) {
if ($row->active == 1) {
foreach ($countries as $country) {
if ($country->id == $row->country) {
if ($this->session->userdata('language') == 3) {
$ccountry = $country->ro_name;
} else if ($this->session->userdata('language') == 2) {
$ccountry = $country->ru_name;
} else {
$ccountry = $country->name;
}
}
}
$time_sbs = substr($row->time, 0, 5);
echo '<tr ">
<td width="20%">' . $row->name . '</td>
<td width="10%">' . $row->date . ' ' . $time_sbs . '</td>
<td width="10%">' . $row->registration_date . '</td>
<td width="10%">' . $ccountry . '</td>
<td width="10%">' . $row->city . '</td>
<td width="10%">' . $row->address . ' </td>
<td width="20%">' . $row->description . '</td>
I sampled from the database for the date
function get_tournaments()
{
//data is retrive from this query
$query = $this->ci->db->query("SELECT * FROM tournaments WHERE date > CURDATE() ORDER BY date");
/*$query = $this->ci->db->query("SELECT * FROM tournaments");*/
return $query;
}
function get_tournaments_finished()
{
//data is retrive from this query
$query = $this->ci->db->query("SELECT * FROM tournaments WHERE date < CURDATE() ORDER BY date");
/*$query = $this->ci->db->query("SELECT * FROM tournaments");*/
return $query;
}
I need to have one table in the view, but with the help of the get parameter, I showed the tournament maps:
<?php echo base_url(); ?>tournaments?type=upcoming",
<?php echo base_url(); ?>tournaments?type=finished"
How to check for a get parameter type

In Codeigniter You can check input parameter by using
$input_params=$this->input->get();// this will give you all parameters
or
$single_param=$this->input->get('name_of_the_parameter');// this will give you individual parameter

use
$_GET['type'];
Or if you prefer, instead of using get, you could use the method parameter to know the type
//something like this
//your url will be like this
base_url('tournament/upcoming'); or
base_url('tournament/finished');
//then your controller method will look like this
public function tournament(){
$type = $this->uri->segment(3);
//then fetch the data based on the type
}
I hope this helps you, if you have any issue, let me know so we can sort it out.

Related

Real Live Search and Filter Laravel

I'm new on Laravel and try to implement real live search and filter on my project, but it doesn't work at all. I dont understand ajax ver much and just copy paste the code from other website. I tried to understand the code and I think its correct but it doesn't work, so please help. Thanks
Here is my controller
public function search(Request $request)
{
if($request->ajax())
{
$output = '';
$query = $request->get('query');
if($query != '')
{
$data = Service::table('service')
->where('keterangan', 'like', '%'.$query.'%')
->orWhere('biaya', 'like', '%'.$query.'%')
->get();
}
else
{
$data = Service::table('service')
->orderBy('kodeService', 'asc')
->get();
}
$total_row = $data->count();
if($total_row > 0)
{
foreach($data as $row)
{
$output .= '
<tr>
<td>'.$row->kodeService.'</td>
<td>'.$row->keterangan.'</td>
<td>'.$row->biayaService.'</td>
</tr>
';
}
}
else
{
$output = '
<tr>
<td align="center" colspan="5">No Data Found</td>
</tr>
';
}
$data = array(
'table_data' => $output
);
echo json_encode($data);
}
}
This is the script
$(document).ready(function(){
fetch_customer_data();
function fetch_customer_data(query = '')
{
$.ajax({
url:"{{ route('live_search.action') }}",
method:'GET',
data:{query:query},
dataType:'json',
success:function(data)
{
$('#table tbody').html(data.table_data);
}
});
}
$(document).on('keyup', '#search', function(){
var query = $(this).val();
fetch_customer_data(query)
});
});
Route :
Route::resource('service', 'ServiceController');
Route::get('service/search', 'Service#search')->name('live_search.action');
And index.blade
<table class="table table-striped table-hover table-bordered" id="table">
<thead>
<tr>
<th>Kode Service</th>
<th>Keterangan</th>
<th>Biaya</th>
<th>Aksi</th>
</tr>
</thead>
<tbody>
#foreach($service as $data)
<tr>
<td><?= $data->kodeService?></td>
<td><?= $data->keterangan ?></td>
<td><?= $data->biayaService?></td>
<td>
<a class="btn btn-sm btn-info" href="{{ route('service.edit', $data['kodeService']) }}"> <i class="oi oi-pencil"></i> Edit</a>
<button type="button" class="btn btn-sm btn-danger" data-toggle="modal" data-target="#myModal"><span class="oi oi-trash"></span> Hapus</button>
</td>
</tr>
#endforeach
</tbody>
</table>
Put your route like this :
Route::get('service/search', 'ServiceController#search')->name('live_search.action');
Route::resource('service', 'ServiceController');
After that open the Browser Console panel (Press F12 to open it) and check the Ajax request in Network tab.
Where you can get the specific error if any in the Response tab.
If you need an extra route to your resource route,you should place the new route before the resource route.
Route::get('service/search', 'ServiceController#search')->name('live_search.action');
Route::resource('service', 'ServiceController');

codeigniter how can i display data in view from this model

This is request_report function in my report_model how can code tabular data in my view passed by controller
function request_report($from, $to, $member_filter )
{
$this->db->select('requests.request_id,items.item_type,items.quantity,items.item_unit,items.item_name, requests.item_id, requests.quantity_requested, requests.quantity_delivered, requests.purpose');
$this->db->from('requests', 'items');
$this->db->join('items', 'items.item_id = requests.item_id');
$this->db->where('created_time >=', $from);
$this->db->where('created_time <=', $to);
if ($member_filter != 'null') $this->db->where('created_by', $member_filter);
$query = $this->db->get();
if ($query->num_rows() > 0 ) {
echo '<table id="requests" class="table table-bordered table-striped">
<thead>
<tr>
<th>#</th>
<th>Category</th>
<th>Items</th>
<th>Quantity Requested</th>
<th>Quantity Available</th>
<th>Quantity Delivered</th>
<th>Purpose</th>
</tr>
</thead>
<tbody>';
foreach ($query->result() as $row) {
echo '<tr>';
echo '<td>';
echo $row->request_id;
echo '</td>';
echo '<td>';
echo $this->get_item_type_name($row->item_type);
echo '</td>';
echo '<td>';
echo $row->item_name;
echo '</td>';
echo '<td>';
echo $row->quantity . " " . $this->get_unit_measurement($row->item_unit);
echo '</td>';
echo '<td>';
echo $row->quantity_requested . " " . $this->get_unit_measurement($row->item_unit);
echo '</td>';
echo '<td>';
echo $row->quantity_delivered . " " . $this->get_unit_measurement($row->item_unit);
echo '</td>';
echo '<td>';
echo $row->purpose;
echo '</td>';
echo '</tr>';
}
echo '</tbody>
</table>';
}
else{
return '<h4 style="text-align: center; padding: 37px;"> No Matching Requests Found </h4>';
}
}
and this is my view
<div class="col-sm-12">
<?php echo $this->report_model->request_report($from, $to, $member_filter)>
</div>
my question is how can pass data from model to view through controller, not just calling model directly in view, and i need the tabular data in model to be in view, am looking for any help thanks!!
my question is how can pass data from model to view through
controller, not just calling model directly in view, and i need the
tabular data in model to be in view, am looking for any help thanks!!
In your controller say method is index()
<?php
class Site extends CI_Controller{
function index()
{
// this you can also put in __construct
// if you want to share same model across other methods
$this->load->model('report_model');
// And call your model function like below
$data = $this->report_model->request_report('2015-12-10', '2016-03-10', 'some_user');
// load view
$this->load->view('bootstrap_view',array('model_output' => $data));
}
}
and in your view, say bootstrap_view.php
<div class="col-sm-12">
<?php echo $model_output;>
</div>
Also note you can make use of table class in codeigniter, where you can set template of your interest, for more details follow below link
https://www.codeigniter.com/user_guide/libraries/table.html

Show data from multiple checkbox in codeigniter

i hope all of you can help me to solve my application problem
my problem is when i would like to show data from my multiple checkbox , data success to show but just showing my first data that i have check.
example is i check data 1 , data 2 , data 3, but the only data 1 are showing on my page.
my controller :
function comparison()
{
if ($this->input->post('submit')) {
foreach ($id_product = $this->input->post('id_product') as $rm) {
$show_compare = $this->Compare->start_compare($rm);
}
$data['comparison'] = $show_compare;
$data['title'] = "Comparison";
$data['meta_keywords'] = ". . .";
$data['meta_descriptions'] = ". . .";
$this->load->view('theme/comparison',$data);
}
}
My Model :
function start_compare($id_product)
{
$this->db->select('product.id_subcategory,product.type,product.product_name,specificcategory.specificcategory_name,specification_biostar.*');
$this->db->join('specification_biostar', 'specification_biostar.id_product = product.id_product', 'left');
$this->db->join('specificcategory', 'specificcategory.id_specificcategory = product.id_subcategory', 'left');
$this->db->where('product.id_product', $id_product);
$sql = $this->db->get('product')->result_array();
return $sql;
}
my view (option multiple-checkbox) :
<div class="box-body">
<input type="checkbox" name="id_product[]" id="txt" onClick="EnableSubmit3(this)" value="<?php echo $row['id_product']; ?>"><label>Choose</label>
</div>
my view (result data) :
<table class="table">
<?php foreach ($comparison as $row){ ?>
<tr>
<td colspan="2"><?php echo $row['id_product'] ?></td>
</tr>
<?php } ?>
</table>
in your code take $show_compare = array(); before foreach loop and use array_push in foreach.
$show_compare = array();
foreach ($id_product = $this->input->post('id_product') as $rm) {
array_push($show_compare,$this->Compare->start_compare($rm));
}
$data['comparison'] = $show_compare;

sort by alphabetical field

I need alphabetical order of how the name from a list. What I need to do while I occupied the pager CakePHP but I could not be. I thought in order by the field (in this case the name) so DESC and ASC does not work me in any case.
In Controller :
$conditions = array('active !=' => -1, 'NOT' => array('role_id' => 2));
$users = $this->paginate('User', $conditions);
$this->set(compact('users','filter'));
In View :
<div class="row">
<table class="table table-striped table-hover table-condensed col-lg-12">
<thead>
<tr>
<th>#</th>
<th><?=$this->Paginator->sort('username', h(__('User')));?></th>
<th><?=$this->Paginator->sort('name');?></th>
<th><?=$this->Paginator->sort('email');?></th>
<th><?=$this->Paginator->sort('created', h(__('Registration date')));?></th>
<th><?=$this->Paginator->sort('active', h(__('Status')));?></th>
</tr>
</thead>
<tbody>
<?php
$offset = $this->Paginator->counter(array('format' => '{:start}'));
foreach($users as $user) {
echo '<tr onclick="window.location.href=\''.$this->Html->url(array('controller'=>'users','action'=>'view',
$user['User']['id'])).'\'">'.
'<td>'.($offset++).'</td>'.
'<td>'.h($user['User']['username']).'</td>'.
'<td>'.h($user['User']['name'].' '.$user['User']['father_surname'].' '.$user['User']['mother_surname']).'</td>'.
'<td>'.str_replace('#','[at]',h($user['User']['email'])).'</td>'.
'<td>'.$this->Time->format('d-m-Y H:i',strtotime($user['User']['created'])).'</td>'.
'<td>';
if ($user['User']['active'] == 1)
echo '<span class="label label-success">'.h(__('Active')).'</span>';
else
echo '<span class="label label-warning">'.h(__('Blocked')).'</span>';
echo '</td>'.
'</tr>';
}
?>
</tbody>
</table>
</div>
Add the following after your conditions
$this->User->order(
array('User.username ASC') //use whatever field you want to sort
);

pass two query result to view in codeigniter

i have two query $q1 and $q2.
from the query1 i get multiple records and each record having id and i want use this id for second query in where condition.
i am doing this in controller.
i have tried following code.
In the foreach i am trying to store id and pass to $q2 where condition.
//Query 1
$q1 = $this->db->select(array(
'spf.id as id' ,
'spf.name',
'spf.added_on'
))->from('sp_form spf')->where($where)->order_by('spf.id desc')->limit(10, $page * 10)->get();
$data['data'] = $q1->result_array();
foreach($data as $rec)
{
$id = $rec['id']; // here how i catch id for each row
//Query 2
$q2 = $this->db->select("count('id') as count ")->from('sp_topic spft')->where('spft.formid',$id)->get();
$data['count'] = $q1->row_object();
}
// pass combine result to view
$this->load->view('myview', $data,true);
Edit:
This is my view.
I have try Nishant answer and i get resultq1 using foreach but how can i get result of resultq2.
<table width="100%">
<tr>
<td class="th"> Details</td>
<td width="5%" class="th"> Answer</td>
<td width="15%" class="th">Started by</td>
<td width="15%" class="th">Created on</td>
</tr>
<?php foreach($resultq1 as $row):?>
<tr>
<td><?php echo $row['name'] ;?></td>
<td >---- </td> // here i want to use resultq2
<td><?php echo $row['added_by'] ;?></td>
<td ><?php echo $row['added_on'];?></td>
</tr>
<?php endforeach;?>
</table>
you can do it like This.
$resultq1= $q1->result_array();
$data['resultq1'] = $resultq1;
$resultq2 = array();$i=0;
foreach($resultq1 as $row){
$id = $row['id'];
$q2 = $this->db->select("count('id') as count ")->from('sp_topic spft')>where('spft.formid',$id)->get();
$resultq2[$i]['count'] = $q1->row_object();
$i++;
}
$data['resultq2'] = $resultq2;
$this->load->view('myview', $data,true);
OR
You can use array_merge
like
$data = array_merge($resultq1, $resultq2);
Then in the myview you will get both the results in variables $resultq1, and $resultq2.
You can pass any numbers of the variables from the controller to the view file by $data['variable_name'] and it can be retrieved in the view file like simple variable $variable_name.
Some Sample links which might help :-
Passing 2 types of array variables in codeigniter view files
$data['count'] = $q1->row_object();
change this like below:
foreach($data as $rec)
{
$id = $rec['id']; // here how i catch id for each row
$q2 = $this->db->select("count('id') as count ")->
from('sp_topic spft')->where('spft.formid',$id)->get();
$data[$id]['count'] = $q2->result_array();
}
$this->load->view('myview', $data,true);

Resources