codeigniter how can i display data in view from this model - codeigniter

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

Related

How do i retrieve images from folder which is linked to an item id?

The main issue is with the model, i cant figure out how to join the two tables so that i can display an item with its image which are stored in different locations.
model
$this->db->select('bs_items.id, bs_items.description, bs_items.title,
bs_items.touch_count,bs_items.price');
$this->db->from('core_images');
$this->db->join('bs_items', 'core_images.img_parent_id =
bs_items.id');
$this->db->order_by('bs_items.touch_count', 'desc');
$this->db->from('bs_items');
Controller:
this->load->model('Popular_car');
$this->load->model('Recent_car');
$data['popular_cars']= $this->Popular_car->popular_car();
$data['recent_cars']=$this->Recent_car->get_listing();
$this->load->view('templates/header');
$this->load->view('pages/home', $data);
$this->load->view('pages/home_recent', $data);
$this->load->view('templates/footer');
Please update your question with some code you have done , or database structure. i'm giving you the answer based on what i understood using your given code snippet.
Model
function get_items()
{
$this->db->select('*');
$this->db->from('core_images');
$this->db->join('bs_items', 'core_images.img_parent_id =
bs_items.id');
$this->db->order_by('bs_items.touch_count', 'desc');
$query = $this->db->get();
return $query->result();
}
Controller
function get_allitems()
{
$this->load->model('model_name');
$data['items'] = $this->model_name->get_items();
$this->load->view('view_name', $data);
}
View
<?php foreach ($items as $row): ?>
<h1><?php echo $row->title;?></h1>
<p><?php echo $row->touch_count;?></p>
<p><?php echo $row->price;?></p>
<p><?php echo $row->description;?></p>
<img src="<?php echo base_url().'path_of_image/'.$row->image_from_db; ?>
<?php endforeach; ?>
$myString = $this->db->get_where('profile_table' , array('id' => $edit ) )->row()->file_name_galery;
// $json11 = '{ "1":"s1.jpg", "2":"s2.jpg", "3":"s4.jpg", "4":"s4.jpg", "5":"s5.jpg" }';
$array = json_decode($myString);
if (empty($array)) {
echo '<p class="tx-danger"> NO IMAGE..</p>';
}
foreach($array as $key => $photo) { <div class="column_galery text-center zoom_img_app">
<a onclick="selected_photo(' echo $photo; ')" data-toggle="modal" data-target="#edit_image_mod" >
<img class="" src="http://yourapp.com/uploads/profile_gallery/ echo $photo; " style="width:100%"> <span class="tx-danger">DELETE</span> </a>
</div> }

How to display all records from database in codeigniter?

I want to display all records from database to my view page. Having a table customers, how can I display all records of this table?
Here is my customer_model.php file..
function all_customss()
{
$query=$this->db->query("select * from customers");
return $query->result();
}
Here are my controller.php file...
function viewpage()
{
$result['data'] = $this->customer_model->all_customss();
$this->load->view('models_view', $result);
}
And here is the link which will display all records after click...
<div class="col-md-12 col-sm-12 text-center"><a class="btn btn-default hvr-bounce-to-right" href="<?php echo site_url('/viewpage/');?>" role="button"><?php echo $this->lang->line('see_more'); ?></a>
and here is my view file...page_view.php
tr><td><?php
$i=1;
foreach($data as $row)
{
echo "<tr>";
echo "<td>".$i."</td>";
echo "<td>".$row->firstname."</td>";
echo "<td>".$row->lastname."</td>";
echo "<td>".$row->email."</td>";
echo "</tr>";
$i++;
}
?></td></tr>
You must put the name of your contoller before the name of your function like bellow:
<?php echo site_url('name_of_controoler/viewpage/');?>
function all_customss()
{
$query=$this->db->query("select * from customers");
return $query;
}
tr><td><?php
$i=1;
foreach($data->result () as $row)
{
echo "<tr>";
echo "<td>".$i."</td>";
echo "<td>".$row->firstname."</td>";
echo "<td>".$row->lastname."</td>";
echo "<td>".$row->email."</td>";
echo "</tr>";
$i++;
}
I have modified your code. You should retrieve an array from a model. But it in your case you have already extracted the result in the model. And take a look in the view. I have modified your foreach parameter

Codeigniter get parameters from url?

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.

Pagination Not working in my Codeigniter

this is my controller file and its not working
controller name blog
model name blogmodel
view file name loggedin
function getvalues(){
$config['base_url'] = 'http://192.168.1.50/codeigni/index.php/blog/getvalues';
$config['total_rows'] = 200;
$config['per_page'] = 20;
$this->pagination->initialize($config);
echo $this->pagination->create_links();
$this->load->model('blogmodel');
$data['results']=$this->blogmodel->getall();
$this->load->view('Login/loggedin',$data);
}
}
this is my model file and model name 'blogmodel'
function getall(){
$query=$this->db->query("SELECT * FROM user_details");
return $query->result();
}
}
this is my view file i cannot access pagination
<p><?php echo $links; ?></p><?php
echo "<table border='1'>
<tr>
<th>id</th>
<th>username</th>
<th>password</th>
<th>email</th>
</tr>";
foreach($results as $row){
echo "<tr>";
echo "<td>" .$row->id;
echo "<td>". $row->username;
echo "<td>". $row->password;
echo "<td>". $row->email;
echo "</tr>";
}
echo "</table>";
?>
Instead of doing echo $this->pagination->create_links();
do as below
$data["links"] = $this->pagination->create_links();

Codeigniter Paginaton Next button is not working

I am trying to display information from database. I have set $config['per_page'] to 2, in my view file I can see the information I want but when I click on the next button it doesn't change anything. The database values remain same and the current page remains the first page too.
Would you please kindly help me figure out the problem?
Thanks in Advance :)
Controller:
function index($id){
$this->load->library('pagination');
$config['base_url'] = site_url().'Student_fee_status/index/'.$id;
$this->db->select('*');
$this->db->from('studentpayment1');
$this->db->where('studentid', $id);
$query = $this->db->get('');
$numrows=$query->num_rows();
$config['total_rows'] = $numrows;
$config['per_page'] = 2;
$config['uri_segment'] = '2';
$config['num_links'] = 20;
$config['full_tag_open'] = '<div class="pagination" align="center">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$this->load->model('Mod_student_fee_status');
$data['records']= $this->Mod_student_fee_status->fee_status($id,$config['per_page'],$config['uri_segment']);
$data['main_content']='view_student_fee_status';
$this->load->view('includes/template',$data);
}
My Model :
function fee_status($id,$perPage,$uri_segment) {
$this->db->select('*');
$this->db->from('studentpayment1');
$this->db->where('studentid', $id);
$getData = $this->db->get('', $perPage, $uri_segment);
if($getData->num_rows() > 0)
return $getData->result_array();
else
return null;
}
EDIT
When the page first loads the link looks like this- http://localhost/sundial/Student_fee_status/index/1006/
but when I click on the next page it looks like this- http://localhost/sundial/Student_fee_status/index/1006/2
My View File:
<h1>Payment Status</h1>
<?php if(count($records) > 0) { ?>
<table id="table1" class="gtable sortable">
<thead>
<tr>
<th>S.N</th>
<th>Invoice ID</th>
<th>Transaction Description</th>
<th>Received Date</th>
<th>Debit</th>
<th>Credit</th>
<th>Balance</th>
</tr>
</thead>
<?php $i = $this->uri->segment(2) + 0; foreach ($records as $row){ $i++; ?>
<tbody>
<?php
$mydate= $row['period'];
$month = date("F",strtotime($mydate));
$year = date("Y",strtotime($mydate));
?>
<tr>
<td><?php echo $i; ?>.</td>
<td><?php echo $row['invoiceid'];?></td>
<td><a href="<?php echo base_url(); ?>student_fee_status/fee_types/<?php echo $row['paymentid']; ?>" rel="1" class="newWindow" >Total Fee For <?php echo $month ;?>, <?php echo $year ;?> </a></td>
<td><?php echo $row['received_date'];?></td>
<td><?php echo $row['totalamount'];?></td>
<td><?php echo "0";?></td>
<td><?php echo $row['totalamount'];?></td>
</tr>
<tr>
<td><?php echo $i; ?>.</td>
<td><?php echo $row['invoiceid'];?></td>
<td>Payment Received </td>
<td><?php echo $row['received_date'];?></td>
<td><?php echo "0";?></td>
<td><?php echo $row['amountpaid'];?></td>
<td>
<?php
$balance=$row['totalamount']-$row['amountpaid'];
if($balance>0){
echo "<font color=\"red\">$balance</font>";
}
else {
echo $balance;
}
?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<?php } ?>
<div class="tablefooter clearfix">
<div class="pagination">
<?php echo $this->pagination->create_links(); ?>
</div>
</div>
You are telling the pagination library to use $config['uri_segment'] = '2'; - the second segment of your uri.
When this is your url: http://localhost/sundial/Student_fee_status/index/1006/ I am guessing this is your base_url: http://localhost/sundial/
In this case your segments are:
Student_fee_status - your controller
index - the controllers method you are calling
1006 - the argument you are calling the controllers method with
this should be the argument for pagination
Try this
$config['uri_segment'] = '4';
instead of
$config['uri_segment'] = '2';
edit:
$data['records']= $this->Mod_student_fee_status->fee_status($id,$config['per_page'],$config['uri_segment']);
I think this line contains another error.
You pass your model the information which uri_segment is used by the pagination library. That should be 4 now. However, your model uses this value to specify an offset in your query. This means you always put an offset of 4 into your query. But I think what you really want to do is, pass the model the VALUE of the 4th uri_segment.
I would try this instead:
$data['records']= $this->Mod_student_fee_status->fee_status($id,$config['per_page'],$this->uri->segment($config['uri_segment']));

Resources