I fetched data from my localhost database. However, a "No data available in table" shows up in my datatables. I am having a hard time figuring out what is the problem because I don't get any errors from it. I use the function fetch in my system_model.php to fetch data from the database. Is there any way to find why values from the database are not showing?
Here is my code for my controller:
class SFM_controller extends CI_Controller {
public function __construct() {
parent::__construct();
// Load form helper library
$this->load->helper('form');
$this->load->helper('url');
// // Load form validation library
$this->load->library('form_validation');
// // Load session library
$this->load->library('session');
// Load database
$this->load->model('system_model');
}
public function index()
{
$data = array(
//'logo' => base_url()."/assets/images/logo/fams-small.png",
//'full_name' => $this->session->user_full_name,
'fo_supp' => $this->system_model->fetch('fo_supp'),
);
$this->load->view('includes/SFM/SFM_Header');
$this->load->view('includes/SFM/SFM_NavBar');
$this->load->view('SFM_view', $data);
$this->load->view('includes/SFM/SFM_Footer');
}
function logout()
{
$this->load->view('includes/Login/Login_Header'); //$data);
$this->load->view('Login_view');
$this->load->view('includes/Login/Login_Footer');
}
}
Here is my code for my Model:
class system_model extends CI_Model
{
function fetch($table, $where = null, $group_by = null, $order_by = null, $limit = null)
{
if($where != null) {
$this->db->where($where);
}
if($group_by != null) {
$this->db->group_by($group_by);
}
if($order_by != null) {
foreach ($order_by as $key => $value) {
$this->db->order_by($key, $value);
}
}
if($limit != null) {
$this->db->limit($limit);
}
$query = $this->db->get($table);
return $query->num_rows() > 0 ? $query->result() : false;
}
Here is my code for my View:
<table id="datatable-buttons" class="table table-striped table-bordered">
<thead>
<tr>
<th>Supplier Code</th>
<th>Address</th>
<th>Country</th>
<th>Description</th>
<th>Telephone Number</th>
<th>Fax Number</th>
<th>Consolidating Agent</th>
<th>Contact Person</th>
<th>Actions</th>
<th>Discount 1</th>
<th>Discount 2</th>
<th>Discount 3</th>
<th>Discount 4</th>
<th>Discount 5</th>
<th>Last Transaction</th>
<th>Old Supplier</th>
</tr>
</thead>
<tbody>
<?php if(!empty($fo_supp)): ?>
<?php foreach($fo_supp as $supp): ?>
<tr>
<td> <?=$supp->supp_code?> </td>
<td> <?=$supp->address." ".$supp->address2?></td>
<td><?=$supp->country?></td>
<td><?=$supp->description?></td>
<td><?=$supp->tel_no?></td>
<td><?=$supp->fax_no?></td>
<td><?=$supp->contact?></td>
<td><?=$supp->cons_agent?></td>
<td>$320,800</td>
<td><?=$supp->disc1?></td>
<td><?=$supp->disc2?></td>
<td><?=$supp->disc3?></td>
<td><?=$supp->disc4?></td>
<td><?=$supp->disc5?></td>
<td><?=$supp->last_trans?></td>
<td><?=$supp->supp_code2?></td>
</tr>
<?php endforeach;?>
<?php endif; ?>
</tbody>
</table>
Var dump
Why is my var dump like this? and not showing values
Pass your $data array in the view file instead of header file in controller index function.
$this->load->view('SFM_view', $data);
SOLVED!
I was passing the data to the wrong controller in which my login submit is located in another controller!
Related
I have a Bank model which has 'bank_name', 'account_name' and 'balance' fields. Another model Transaction is where user first selects the bank and inputs opening_balance and transaction_amount and the closing_balance which is "opening balance +- transaction_amount" (-, + depending upon debit/credit) becomes "balance" in Bank Model. I want to show the balance in a particular bank in bank.index page by grabbing the closing_balance from transactions table. I am stuck here. So far I have:
Bank Model:
protected $fillable=['bank_name','account_name'];
public function transactions()
{
return $this->hasMany('App\Transaction');
}
Transaction Model:
protected $fillable = ['bank_id','opening_balance','transaction_amount','isdebit','closing_balance'];
public function bank()
{
return $this->belongsTo('App\Bank');
}
BankController:
public function index()
{ $banks = Bank::all();
//$bal_1 = Bank::find(1)->transactions()->latest()->first();
// I can show the balance in bank which has id 1 by this manually.
return view('bank.index', compact('banks','bal_1'));
}
Transaction Controller:
public function index()
{ $transactions = Transaction::all();
return view('transaction.index',compact('transactions'));
}
Bank.index page
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Bank Name</th>
<th scope="col">Account Name</th>
<th scope="col">Balance</th>
</tr>
</thead>
<tbody>
#foreach($banks as $i=>$bank)
<tr>
<th scope="row">{{++$i}}</th>
<td>{{$bank->bank_name}}</td>
<td>{{$bank->account_name}}</td>
<td>{{$bal_1->closing_balance}}</td>
</tr>
#endforeach
</tbody>
</table>
You can define another relation in Bank model which give you one model object instead of array like this :
public function transaction()
{
return $this->hasOne('App\Transaction');
}
Then You can use this relation in your bank.index view :
<td>{{$bank->transaction->closing_balance}}</td>
view page:
#if(count($applications)>0)
<table class= "table table-striped">
<tr>
<th>Email</th>
<th>Date Applied</th>
<th>Leave Type</th>
<th>Leave Status</th>
<th></th>
</tr>
#foreach($applications as $application )
<tr>
<td>{{$application ->user->email}}</td>
<td>{{$application ->dateApplied}}</td>
<td>{{$application ->leaveType}}</td>
<td>{{$application ->leaveStatus}}</td>
<td>
{!!Form::open(['action'=>['ApplicationAdminController#destroy', $application ->appId], 'method'=>'DELETE'])!!}
{{Form::hidden('_method', 'DELETE')}}
{{Form::submit('Delete', ['class' => 'btn btn-danger'])}}
{!!Form::close()!!}
</td>
</tr>
#endforeach
</table>
controller:
public function destroy($appId)
{
$application = Application::find( $appId);
$application ->delete();
$this->deleteLeave($appId);
return redirect('/applicationAdminHistory')->with ('success', 'Application Removed');
}
public function deleteLeave($appId){
$user_id = Auth::id();
$req = DB::table('leave_summaries')
->where('user_id',$user_id)
->increment($leaveType,$day);
}
What I want to do is when click delete button the application will deleted and will do increments at the leave_summaries table. The increment at the leave_summaries based on the value of day and leave type in the application that has been deleted. After i run this code i got undefined variable error as shown below:
In this Code Please Pass $user_id , $leaveType ,$day:
public function deleteLeave($appId){
$req = DB::table('leave_summaries')
->where('user_id',$user_id)
->increment($leaveType,$day);
}
You didn't defined these three variables : $user_id, $leaveType, $day
You need to pass these values as parameters into the deleteLeave() method or these values had to be defined before the method called.
public function deleteLeave($user_id, $leaveType, $day){
$req = DB::table('leave_summaries')
->where('user_id',$user_id)
->increment($leaveType, $day);
}
I have table displaying many rows, and I'm using pagination and sort function, also I'm using ajax to return number of rows and other ajax to return rows between two dates.
The problem is if I wanted to sort rows and in the same time show some rows between two dates this wont work with me. Because when using ajax there is no url.
public function index()
{
$checks = Checks::orderBy('id', 'asc')->get();
$checks= Checks::sortable()->paginate(10);
return view('home',compact('checks'));
}
public function showpage(Request $request)
{
if($request->ajax())
{
$checks= Checks::orderBy('id', 'asc')->paginate($request->inputpage);
return view('layouts.showcheks',compact('checks'));
}
}
public function getCheckReport(Request $request)
{
if($request->ajax()){
$New=$request->StartDate;
$Old=$request->EndDate;
$checks= Checks::whereBetween('postingdate',[$New,$Old])->sortable()->orderBy('postingdate', 'asc')->get();
return view('layouts.showcheks',compact('checks'));
}
}
showchecks.blade.php
#foreach($checks as $indexKey => $check)
<tr >
<td>{{$check->details}}</td>
<td>{{date('m/d/Y', strtotime($check->postingdate))}}</td>
<td>{{$check->description}}</td>
</tr>
#endforeach
homepage:
<table class="table" id="postTable">
<thead>
<tr>
<th>#sortablelink('details','Details')</th>
<th>#sortablelink('postingdate','Date')</th>
<th>#sortablelink('description','Description')</th>
</tr>
{{ csrf_field() }}
</thead>
<tbody>
#foreach($checks as $indexKey => $check)
<tr >
<td>{{$check->details}}</td>
<td>{{date('m/d/Y', strtotime($check->postingdate))}}</td>
<td >{{$check->description}}</td>
</tr>
#endforeach
</tbody>
</table>
{{$checks->appends(Request::input())->links()}}
use a datatable https://datatables.net/ with ajax that is the best way also u can sorting a rows also..
I got 3 tables; my tables are "cursadas","usuarios" and "materias"
"cursadas" includes:(id, user_id[is the foreign key to the column "id" of the table "usuarios"], subject_id[is the foreign key to the column "id" of the table "materias"], grade, date)
"usuarios" includes:(id,username,name,lastname,password,type,status,date)
"materias" includes:(id, career_id, name, description, hours)
This is my final table "cursadas"(with data from tables "materias" and "usuarios")
TAKE A LOOK, i need something like this:
I got this error:
I think there is an error with my query, i do not know what should i do to make this work :S
Here is my code:
My view file ("usuario"):
<input id="busqueda_tabla" type="text">
<table class="table table-hover" align="center" border="1" cellspacing="0" cellpadding="0" width="700" id="tabla_busqueda">
<thead>
<th>id</th>
<th>User</th>
<th>Subject</th>
<th>Grade</th>
<th>Date</th>
</thead>
<tbody>
<?php
if (count($records) > 0 && $records != false) {
foreach($records as $record) {
echo "<tr>
<td>".$record['id']."</td>
<td>".$record['user']."</td>
<td>".$record['name']."</td>
<td>".$record['grade']."</td>
<td>".$record['date']."</td>
</tr>";
}
}
?>
</tbody>
</body>
</html>
My controller file ("login"):
<?php
Class Login extends CI_Controller{
public function index(){
$this->load->view('login_form');
}
public function do_login()
{
// load the form_validation library
$this->load->library('form_validation');
$this->form_validation->set_rules('usuario', 'Username', 'trim|required|min_length[3]|alpha_numeric');
$this->form_validation->set_rules('contrasena', 'Password', 'trim|required|min_length[6]');
// if there is errors
if ($this->form_validation->run() == FALSE) {
// this will load your form with the errors
$this->load->view('login_form');
} else {
// if no errors we will hit the database
$user=$this->input->post('usuario', true);
$pass=$this->input->post('contrasena', true);
$cek = $this->m_login->proceso_login($user,$pass);
$hasil=count($cek);
if($hasil > 0)
{
$pelogin =$this->db->get_where('usuarios',array('username' => $user, 'password' => $pass))->row();
// here $pelogin has the id of the user
// create session like this
$this->session->set_userdata(array('id' => $pelogin->id));
if($pelogin ->type == 0)
{
// here goes the admin data
redirect('login/admin');
}
else{
//call here usuario method which has user data who logged in like
redirect('login/usuario');
// OR
// Call some method which has user data in $records and
}
}
redirect('login/index');
}
}
public function admin (){
$data['records']=$this->m_login->getDetails();
$this->load->view('admin',$data);
}
public function usuario(){
$data['records']=$this->m_login->getDetails();
$this->load->view('usuario',$data);
}
And the model file("m_login")- with the query!
<?php
class m_login extends CI_Model{
public function proceso_login($user,$pass){
$this->db->where('username', $user);
$this->db->where('password', $pass);
return $this->db->get('usuarios')->row();
}
public function getDetails()
{
$st=$this->db->SELECT('cursadas.*, usuarios.name as usuarios, materias.name as materias_name')->from('cursadas')
->join('usuarios','usuarios.id=cursadas.user_id')
->join('materias','materias.id=cursadas.subject_id')
->WHERE('cursadas.user_id=',$this->session->userdata['id'])
->get()->result_array();
return $st[0];
}
}
?>
Change the query :
$st = $this->db->SELECT('cursadas.date as date, cursadas.grade as grade, usuarios.username as user, materias.name as subject')->from('cursadas')
->join('usuarios','usuarios.id=cursadas.user_id')
->join('materias','materias.id=cursadas.subject_id')
->WHERE('cursadas.user_id=',$this->session->userdata('id'))
->get()->result_array();
return $st;
And pls check the database field type use datetime for date int for id and so as required
In the view :
<tbody>
<?php
if (count($records) > 0 && $records != false) {
$id = 1;
foreach($records as $record) {
echo "<tr>
<td>".$id."</td>
<td>".$record['user']."</td>
<td>".$record['subject']."</td>
<td>".$record['grade']."</td>
<td>".$record['date']."</td>
</tr>";
$id++;
}
}
?>
I can't figure out why my data is not being passed to View
I get the message Undefined variable: project
Can someone help identify where i am going wrong?
when i return $project->name straight from the controller it displays the response, however fails to pass to view.
Here is my controller function:
public function getApi(){
$client = new Client();
$response = $client->get('https://play.geokey.org.uk/api/projects/77');
$body = $response->getBody()->getContents();
$project = json_decode($body);
return view ('response', compact($project));
}
And here is my View:
<h1>Welcome</h1>
<table class="table table-bordered">
<tr>
<th>Project Name</th>
<th>Date of Creation</th>
<th>Contribution Total</th>
</tr>
<tr>
<td>{{$project->name}}</td>
</tr>
</table>
Instead of:
return view ('response', compact($project));
Type
return view ('response', compact('project'));
return view ('response', compact('project')); instead of compact($project)