Please help me. i can not use my dataTable properly. what I want to do is
select from table and use thewherefunction. but i cant do it properly.
here is my controller code
public function reporttable ()
{
$zz = array('empnumber' => $this->input->post('empnumber'));
//$this->db->order_by("surname", "asc");
$this->datatables->select('date_auto,particulars,earned_VL,aul_VL,balance_VL,aulx_VL,earned_SL,aul_SL,balance_SL,aulx_SL,date_leave,action_taken')
->from('tblreport')->where($zz);
echo $this->datatables->generate();
}
this is my supposed query:
select * from tblreport where empnumber = (the empnumber in my textbox.)
there, i get a value from a textbox to my view. but it didn't work. i know that is wrong. can you please help me with my problem? thank you.
<p align="center"> <?php echo $this->table->generate();?></p></div>
<?php foreach ($tblemployee as $row){?>
<input type="text" name="empnumber" readonly="readonly" value="<?php echo $row->empnumber;?>"/>
<input type="hidden" name="empnumber" value="<?php echo $row->empnumber;?>"/>
here is my view for guide. thank you.
as Simple you can use
In Controller
$data['tblemployee'] = $this->model_name->reporttable($id)//assign your data base value to variable
$this->load->view('your view name',$data )
in Model
public function reporttable($id)
{
$query = $this->db->query("select * from tblreport where empnumber = '$id'");
$result = $query->result_array();
return $result; // this will return your data as array
}
In view
<?php
foreach ($tblemployee as $row)
{
echo $row['id];
}?>
Try this :
To make it more simplier.
In model :
public function reporttable ($id){
$this->db->select('*');
$this->db->from('tblreport');
$this->db->where('empnumber', $id);
$query = $this->db->get();
return $query->return_array(); // use this if so want to return many query if not you can also use first_row('array')
}
In controller :
public function function_name (){
$data['variable_name'] = $this->model_name->reporttable($id); // change the model_name by your own model where your function reporttable is located and use the variable_name for your view,
$this->load->view('view_name' , $data); // load the view page you want to display.
}
In Controller
$data['tblemployee'] = $this->model_name->reporttable($id)//assign your data base value to variable
$this->load->view('your view name',$data )
in Model
public function reporttable($id)
{
$query = $this->db->query("select * from tblreport where empnumber = '$id'");
$result = $query->result_array();
return $result; // this will return your data as array
}
In view
<?php
foreach ($tblemployee as $row)
{
echo $row['id];
}?>
Related
I gotta fetch data from 2 tables.. My tables are "Study","Users" and "Subjects"
"Study" includes:(id, user_id[is the foreign key to the column "id" of the table "Users"], subject_id[is the foreign key to the column "id" of the table "Subjects"], grade, date)
"Users" includes:(id,username,name,lastname,password,type,status,date)
"Subjects" includes:(id, career_id, name, description, hours)
I wanna get something like this at the end:
I got this errors:
Here is my code:
My view file ("home"):
<html>
<head>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h2 align="center">TABLE:Study</h2>
<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>
<th>Action</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['subject']."</td>
<td>".$record['grade']."</td>
<td>".$record['date']."</td>
<td align='center'>
<button type='button' class='btn btn-primary'>EDITAR</button></a> |
<button type='button' class='btn btn-danger'>BORRAR</button></a>
</tr>";
}
}
?>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
Here is my Controller file ("Home"):
<?php
class Home extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->model("Crudmodel");
}
public function index(){
# get all data in Study table
$selectStudys = $this->Crudmodel->selectStudys();
foreach ($selectStudys as $key => $study)
{
# get UserNames
$user = $this->Crudmodel->getName($study['user_id']);
#get Subject Names
$subject = $this->Crudmodel->getSubName($study['subject_id']);
#append both NEW VALUES to same array
$data[$key]['user_id'] = $user[0]['username'];
$data[$key]['subject_id'] = $subject[0]['name'];
}
$data['records'] = $selectStudys;
$this->load->view('home', $data);
}
}
?>
And my Model file ("Crudmodel"):
<?php
class Crudmodel extends CI_Model{
public function __construct(){
parent::__construct();
$this->load->database();
}
function selectStudys()
{
$query= $this->db->query("SELECT * FROM Study");
$result = $query->result_array();
return $result;
}
function getName($name)
{
$query= $this->db->query("SELECT username FROM Users WHERE id = $name ");
$result = $query->result_array();
return $result;
}
function getSubName($subject)
{
$query= $this->db->query("SELECT name FROM Subjects WHERE id = $subject ");
$result = $query->result_array();
return $result;
}
}
?>
Hope you can help me :/
Iam changed your query to join query, Simply change your code to below
public function index(){
# get all data in Study table
$query = $this->db->query("SELECT sd.user_id as id,sd.grade as grade,sd.date as date,sd.subject_id as subject,ur.username as user FROM Study as sd,Users as ur,Subjects as sb WHERE ur.id=sd.user_id and sb.id=sd.subject_id");
$result = $query->result_array();
$data['records'] = $result;
$this->load->view('home', $data);
}
and now run the code
Undefined indexes and trying to get property non-object more or less means the same thing which is you are not getting proper data or the variables or indexes you are trying to get are not initialized or undefined and cause of this can be error in query or blank data return by query you are running.
i would like to request you to pull your query data like this
$check = $this->db->query("SELECT * FROM SOMETHING AND YOUR CONDITION AND STUFF HERE");
if($check->num_rows()>0){
$result = $check->result();
return $result;
}else{
return false; // or anything you want.
}
let say this query function is stored in model and you are calling your model like this
$someVariable = $this->model_name->function();
if($someVariable!==FALSE){
// handle
}else
{
// handle
}
in the end, not sure why, but i also counter problems with double quotes sometime, YES I KNOW.. variable inside double quotes work, I'm just saying sometime... at least it happens with me, so i would like to request last thing. try debugging your query like this, currently you have
"SELECT * FROM TABLE WHERE THIS = $THAT"
Change this to
"SELECT * FROM TABLE WHERE THIS = '".$THAT."'"
I hope it will work out for you!.
EDITED:
(Sorry that i failed to show example from your own code)
Your Model file
<?php
class Crudmodel extends CI_Model{
public function __construct(){
parent::__construct();
$this->load->database();
}
function selectStudys()
{
$query= $this->db->query("SELECT * FROM Study");
if($query->num_rows()>0){
$result = $query->result_array();
}else{
$result = "";
// or anything you can use as error handler
return $result;
}
}
function getName($name)
{
$query= $this->db->query("SELECT username FROM Users WHERE id = $name ");
if($query->num_rows()>0){
$result = $query->result_array();
}else{
$result = "";
// or anything you can use as error handler
return $result;
}
}
function getSubName($subject)
{
$query= $this->db->query("SELECT name FROM Subjects WHERE id = $subject ");
if($query->num_rows()>0){
$result = $query->result_array();
}else{
$result = "";
// or anything you can use as error handler
return $result;
}
}
function CombineResults($subject, $name){
// you can also use this
$query = $this->db->query("SELECT sub.name, user.username FROM Subjects sub, Users user WHERE sub.id=$subject AND user.id = $name");
if($query->num_rows()>0){
return $query->result();
}else{
return "";
// or anything you can use as error handler
}
}
}
?>
Your controller file
public function index(){
# get all data in Study table
$selectStudys = $this->Crudmodel->selectStudys();
// we have condition on this model method/function we can validate
// response comming from this method and add handler just like we did
// for queries. your main problem can be this
foreach ($selectStudys as $key => $study)
{
# get UserNames
$user = $this->Crudmodel->getName($study['user_id']);
#get Subject Names
$subject = $this->Crudmodel->getSubName($study['subject_id']);
#append both NEW VALUES to same array
if(!empty($user[0]['username'])){
$data[$key]['user_id'] = $user[0]['username'];
// your main problem can be this. may be it is not getting value from query this is why we have put validation on model function and error handler condition here
}else{
$data[$key]['user_id'] = ''; // or anything as your else condition you can use as error handler
}
if(!empty($subject[0]['name'])){
$data[$key]['subject_id'] = $subject[0]['name'];
// your main problem can be this. may be it is not getting value from query this is why we have put validation on model function and error handler condition here
}else{
$data[$key]["subject_id"] = "";
// or anything you can use as error handler
}
}
$data['records'] = $selectStudys;
$this->load->view('home', $data);
}
I am using codeigniter framework. I have a table named client. I need to get the first name from the table and send it to my view page. I have a function in model file to retrieve the client data.
This is my model- function:
public function get_cli($id)
{
$this->db->select('*');
$this->db->from('client');
$this->db->where('id', $id);
$res = $query->result();
$row = $res[0];
return $row->first_name;
}
Here I get my client first name.
This is my controller file. Here I have a function to send the data from the client table to my view page.
public function ticket($id)
{
$id=$this->uri->segment(4);
$data['id']=$this->uri->segment(4);
$data['client']=$this->billing_model->get_cli($id);
$data['employee']=$this->employee_model->emp_name();
$data['main_content'] = 'admin/billing/ticket_page';
$this->load->view('includes/template', $data);
}
My view page.
<label>Client Id:<?=$id?></label>
<input type="text" value="<?=$client['first_name'];?>"/>
I am getting a white screen. Why is this happenening? Can someone help me with the code?
Change your model function to this:
public function get_cli($id)
{
$this->db->select('*');
$this->db->from('client');
$this->db->where('id', $id);
$query = $this->db->get();
$result = $query->result_array();
return $result[0]['first_name']; //returning only the first name key value of first row(0th row) of the result;
}
Then On the view page:
<input type="text" value="<?=$client;?>"/>
Which will print only the first name;
Another approach:
Change your model function to this:
public function get_cli($id)
{
$this->db->select('*');
$this->db->from('client');
$this->db->where('id', $id);
$query = $this->db->get();
$result = $query->result_array();
return $result; //returning the whole array;
}
Then On the view page:
<input type="text" value="<?=$client[0]['first_name'];?>"/>
Which will print only the first name value from the 0th(first row) of the array;
You should change your view page to this
<input type="text" value="<?=$client;?>" />
This will work as you are returning the firstname in your model not whole array.
So, your $client variable will contain the firstname. You dont need to refrence it.
Simply echoing $client will work.
When you have only single row in result i suggest you to use row_object() instead of result()
public function get_cli($id)
{
$this->db->select('*');
$this->db->from('client');
$this->db->where('id', $id);
$res = $query->row_object();
//$row = $res[0]; no need to write this line
return $res->first_name;
}
for your problem on your view
You will get first_name in $client
<input type="text" value="<?=$client?>"/>
if you are not getting anything please check your result from database might be null.
USE
$row['first_name'] instead of $row->first_name
I am trying to get data from a database and display it using a model, controller, and view.
Here is my model
public function waitlist_view() {
$data = array();
$this->load->database();
$this->db->select('*');
$this->db->from('waitlist');
$query = $this->db->get();
return $query->row();
}
Here is my controller
public function waitlist() {
$data['title']="parents_viewlist";
//redirect if not logged in
if(($this->session->userdata('logged_in')!= 1) && ($this->session->userdata('type')!='parent')) {
redirect('login/index');
}
$this->load->model('parents_model');
$data['row'] = $this->parents_model->waitlist_view();
$this->load->view('templates/cpsheader', $data);
$this->load->view('templates/cpsmenu');
$this->load->view('parents/parents_viewlist', $data);
$this->load->view('templates/cpsfooter');
}
Here is my view
<div>
<?php echo $row->waitlist_id; ?>
<?php echo $row->ay_code; ?>
<?php echo $row->school_id; ?>
<?php echo $row->waitlist_status; ?>
</div>
It doesnt display anything on the page when I pull it up. Any help would be appreciated!
in your model use result() to get data :
public function waitlist_view() {
$this->load->database();
$query = $this->db->get('waitlist')->result();
return $query;
}
in your controller :
$this->load->model('parents_model');
$data['row'] = $this->parents_model->waitlist_view();
$this->load->view('templates/cpsheader', $data);
$this->load->view('templates/cpsmenu');
$this->load->view('parents/parents_viewlist', $data);
$this->load->view('templates/cpsfooter');
Now use loop on $row in your view to print data.
In your controller print the data returned from db as:
echo "<pre>";print_r($data);echo "</pre>";exit;
add this line just after $data['row'] = $this->parents_model->waitlist_view();
Let us know what result are you getting.
It was hard to come up with a title. I am using CodeIgniter with models/views/controller. I have the following tables in my MySQL database that are relevant:
In my model I have the following function:
function get_shoptable() {
$this->db->from('productshop')->where('productId', $this->productId);
$query = $this->db->get();
return $query->result();
}
In my controller I use the above function like
$data['bookshop'] = $this->Product_model->get_shoptable();
In my view I am foreaching $bookshop. My problem is, what is the best wayto show shopName, instead of showing shopId. Taking in regards that $bookshop should be as it is (except of shopid), because I am creating a HTML table with product data.
Try some like this:
function get_shoptable() {
$this->db->from('productshop')
->join('shop', 'productshop.shopId = shop.shopId')
->where('productshop.productId', $this->productId);
$query = $this->db->get();
return $query->result();
}
Model:
function get_products() {
$this->db->select('productshop.productUrl, productshop.price, productshop.deliveryTime, productshop.shippingCast, productshop.inventory, productshop.productId, productshop.shopId, shop.shopName');
$this->db->from('productshop');
$this->db->join('shop', 'productshop.shopId = shop.shopId');
$this->db->where('productshop.productId', $this->productId);
return $this->db->get()->result_array();
}
Controller:
function products() {
$data['products'] = $this->model_name->get_product();
$this->load->view('products', $data);
}
VIEW:
<?php foreach($products as $p): ?>
<h1><?php echo $p['productUrl']; ?></h1>
<h1><?php echo $p['shopName']; ?></h1>
<?php endforeach(); ?>
get an overlook to active class of codeigniter for details of functions
function get_shoptable()
{
$this->db->from('productshop')
$this->db->join('shop', 'productshop.shopId = shop.shopId')
$this->db->where('productshop.productId', $this->productId);
$query = $this->db->get();
return $query->result();
}
//model
function shop_dropdown()
{
$this->db->select('shop');
$this->db->from('shop');
//$this->db->where('category_online', 1);
$query = $this->db->get();
foreach($query->result_array() as $row)
{
$data[$row['id']]=$row['name'];
}
return $data;
}
controller//
function shop_dropdown()
{
$data = array();
$this->load->model('shop_model');
$shop['select_options'] = $this->shop_model->shop_dropdown();
$this->load->view('shop/product_view', $shop);
}
view//
<?php
echo form_dropdown('shop', $select_options);
?>
this is not not working.please help me creating a drop downlist from database.if you can write a new code.
thanks in advance
Modify like this
function shop_dropdown()
{
$data = array();
$this->load->model('shop_model');
$shop = $this->shop_model->shop_dropdown();
$this->load->view('shop/product_view', $shop);
}
and in your view
echo form_dropdown('shop', $shop->option);//option is an value taking form database
that's it.accept answer if it useful for you
I am not sure if you have autoload form helper, if you didn't, you can't use the form_dropdown function unless you load it in the controller. I don't see you load form helper anywhere.
http://codeigniter.com/user_guide/helpers/form_helper.html
you are selecting 'shop' column in your model.
I think your model should be like this
function shop_dropdown()
{
$this->db->select('id,name'); //column names you want to select, can be optional if you want to select all columns.
$this->db->from('shop'); //table name, required
//$this->db->where('category_online', 1);
$query = $this->db->get();
foreach($query->result_array() as $row)
{
$data[$row['id']]=$row['name']; //make sure 'id' and 'name' ,columns are present in table
}
return $data;
}
And I hope you have edited application/config/databse.php