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();
}
Related
I am getting the following error.
Message: Trying to get property of non-object
Table Structure:
name ='fb' value='https://www.facebook.com/mypage'
Model:
function get_fb_url(){
$query=$this->db->select('value');
$query=$this->db->from('preferences');
$query=$this->db->where('name','fb');
return $this->db->get()->row();
}
Controller:
$data['fb_url']=$this->Home_model->get_fb_url()->value;
View:
<li><img src="css/imgs/fb.png" /></li>
When i run my page it give me error
Message: Trying to get property of non-object
only when if value in the table is link like above, if its simple text then text properly show on page.
How Can i get rid off this error?
Instead of echo use return type
Change
echo $this->db->get()->row();
to
return $this->db->get()->row();
And add $ before variable name in view
<a href="<?php echo $fb_url;?>"
UPDATED
Change your model code to
function get_fb_url() {
$this->db->select('value');
$this->db->from('preferences');
$this->db->where('name', 'fb');
return $this->db->get()->row();
}
you must try like this:
function get_fb_url() {
$this->db->select('value');
$this->db->from('preferences');
$this->db->where('name', 'fb');
$query = $this->db->get();
return $query->row();
}
and in Controller:
$result = $this->Home_model->get_fb_url();
//try print $result, it must be an stdObject
$data['fb_url']=$result->value;
it is because in $data['fb_url']=$this->Home_model->get_fb_url()->value; / differentiate the link to the other function, that's why u r getting this error.
get_fb_url is returning object array not object. so you have to return value from method.
function get_fb_url()
{
$query=$this->db->select('value');
$query=$this->db->from('preferences');
$query=$this->db->where('name','fb');
return $this->db->get()->row()->value;
}
and than
$data['fb_url']=$this->Home_model->get_fb_url();
In Your model do this
public function get_fb_url() {
$this->db->select('value');
$this->db->from('preferences');
$this->db->where('name','fb');
$query = $this->db->get();
$row = $query->row();
return $row;
}
In Controller :
$data = array();
$result = $this->Home_model->get_fb_url();
$data['fb_url'] = $result->value;
$this->load->view('your_view',$data);
In View :
echo $fb_url;
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];
}?>
I have a table called 'News' with three columns: 'id', 'title' and 'details'
I have a function ('get_entry') inside a codeigniter model class (called 'News_model')
function get_entry()
{
$this->load->database();
return $this->db->select('id,title,details')->from ('news');
$data['newsarray'] = $this->db->row_array();
return $data['newsarray'];
}
I am connecting to the db so that is not the problem.I want to return an iterable array from get_entry() by calling the function from a controller file with the followlwing code. I want to push it into another array (called '$data['theNews']') using the code below.
foreach ($this->News_model->get_entry() as $key => $value){
array_push($data['theNews'],$value->title);
}
I have been using the code on this (https://www.codeigniter.com/user_guide/general/models.html) as a template (in particular the function 'get_last_ten_entries()' but I think I am close with the code I posted above. I would appreciate any help.
About your code:
You have two 'return' in your get_entry function:
function get_entry()
{
$this->load->database();
// First
return $this->db->select('id,title,details')->from ('news');
$data['newsarray'] = $this->db->row_array();
// Second
return $data['newsarray'];
}
Change it to:
function get_entry()
{
$this->load->database();
$query = $this->db->select('id,title,details')->from('news');
$data['newsarray'] = $query->row_array();
return $data['newsarray'];
}
It should work now.
Some advices:
Don't use Codeigniter 2 anymore. Version 3 is alive.
If you plan to return whole table columns, i suggest you to use the following code for the query:
$query = $this->db->get('news', 1, 20);
Where 1, 20 is the limit.
Now you can get the result:
return $query->result();
A simple example:
function get_entry()
{
$this->load->database();
$query = $this->db->get('news', 1, 20);
return $query->result();
}
This method returns the query result as an array of objects that you can print like so in your controller:
$news_array = $this->News_model->get_entry();
foreach ($news_array as $news)
{
echo $news->id;
}
Look at CI 3 Query Builder query builder for more examples.
One more suggestion, just autoload the database library in application/config/autoload.php if you need it globally.
Changing the code to this in the function worked:
function get_entry()
{
$this->load->database();
$query = $this->db->get('news');
//return $query->result();
foreach ($query->result() as $row)
{
echo "</br>";
echo $row->id;
echo "</br>";
echo $row->title;
echo "</br>";
echo $row->details;
echo "</br>";
}
}
Calling the function like so prints it out:
$news_array = $this->News_model->get_entry();
My database definitely contains values and I am trying to read these values for a specific column (name) into an array like so:
public function listcli()
{
$this->db->distinct();
$this->db->select('name');
}
}
then referencing to this function like so:
public function clist() {
$this->load->model('list_model');
$fields = $this->list_model->listcli();
$fieldl = $fields;
$data= array();
$data['fieldl'] = $fieldl;
$this->load->view('clientlist', $data);
}
This basically completes the database query in the model, passes the information into the controller where it puts the array into another array with a key (key being the same name as the array) so that I can pass it into my view, then my view which looks like this :
<html>
<body>
<p> <?php print_r($fieldl); ?></p>
<ul>
<?php
$fieldl = array();
?>
<p> <?php print_r($fieldl); ?> </p>
<?php
foreach($fieldl as $l) {
?>
<li>
<?php echo $l;?>
</li>
<?php
}
Then lists it
but even though I KNOW I have data in "name" the print_r is showing that my array is empty? Help please and thank you!
Are you sure your model is returning any value? If not, the return some:
public function listcli()
{
$this->db->distinct();
$this->db->select('name');
return $this->db->get('table_name')->result_array(); // return value
}
Please try the following
public function listcli(){
$this->db->distinct();
$this->db->select('name');
$query = $this->db->get('table_name');
return ($query->num_rows > 0 ? $query->result() : array());
}
If it does not help ...
Please give me table structure with some dummy date, So that I can help you resolve this.
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.