I am trying to implement global settings to each controller and views.
Having database table named config containing
config_name | config_value
title | My Website
email | my#example.com
In my model I have query:
function config()
{
$query = $this->db
->select('*')
->get('config');
return $query->result();
}
In controller I call model data this way
$data['cfg'] = $this->config_model->config();
Now I want to display certain bits of config in view by using
<title><?php echo $cfg->title; ?></title>
But I get an error Trying to get property of non-object
I tried result query options in model query:
result();
result_array();
But it won't work. What am I doing wrong?
try this in your model:
public function config() {
$q = $this->db->get('config')
return $q->result_array();
And this in your controller:
$this->load->model("config_model");
$res = $this->config_model->get();
if($res){
$data['result'] = $res;
$this->load->view('Your view here', $data);
} else {
echo "failed";
}
And then you can access like this in your view:
$result[0]['title'];
Assuming your config table only has one row.
Ok I have simplified things and solved it this way:
Changed database table layout to 1 row
title | email | country | city
And now it works. Previously i wanted db layout different, but i'll go with this now.
can be used like that
your model:
public function get() {
$query = $this->db->get('config');
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
$data[$row->item] = $row->val;
}
return $data;
}
return false;
}
use in the controller:
$this->load->model("config_model");
$config = $this->config_model->get();
echo $config['title'];
use in view:
$this->load->view('index',$config);
result:
<p><?=$title?></p>
Related
I am facing problem to join these tables and also know how to make controller code to view data in view file with text boxes.
public function get_order_return_info()
{
$this->db->select('tbl_order_details.*', false);
$this->db->select('tbl_order_details.order_details_id', false);
$this->db->select('tbl_order.order_id', false);
$this->db->select('tbl_product.product_id', false);
$this->db->select('tbl_inventory.product_quantity', false);
$this->db->from('tbl_order_details');
$this->db->join('tbl_order', 'tbl_order_details.order_id = tbl_order.order_id ', 'left');
$this->db->join('tbl_product', 'tbl_order_details.product_code = tbl_product.product_id ', 'left');
$this->db->join('tbl_inventory', 'tbl_product.product_id = tbl_inventory.product_id ', 'left');
$query_result = $this->db->get();
$result = $query_result->result();
return $result;
}
You can use MVC to solve this
//Controller
function get_data() {
$data['list'] = $this->your_model->get_data();
$this->load->view('your_view/location',$data);
}
//Model
function get_data() {
$sql = "your_query";
$list = $this->db->query($sql)->result_array();
return $list;
}
//View
foreach($list as $data) {
echo $data['your_selected_field']; // it could be table,text field,or just text
}
API Reference : https://www.codeigniter.com/user_guide/database/results.html#result-arrays
Hope this helps
First u can create OrderModel to work with order table. Then create function get_order_data.
//Order Model
public function get_orders() {
// your db query to get orders
return $result;
}
Then u can load model in controller using $this->load->model('OrderModel'); Best place is __construct to load model
And call your get_orders() function
// Your controller
// Create data array to store front data
$data = [];
$data['orders'] = $this->OrderModel->get_orders();
After getting $orders pass it to view using following codes
$this->load->view('path/to/view', $data);
Finally u can call your order data in view using $orders
I hope this will help u
You need to set alias to the table to function it properly like this.
$this->db->select('a.*,a.order_details_id, b.order_id ', false);
$this->db->from('tbl_order_details as a');
$this->db->join('tbl_order as b', 'a.order_id = b.order_id ', 'left');
If you want select 1 row only you need to use this function.
$this->db->get('tbl_order_details')->row_array();
or if you want all result row you need to use this.
$this->db->get('tbl_order_details')->result_array();
On your controller you should add this lines to pass your result on your view page.
public function index()
{
$data['orderDetails'] = $this->your_model->get_order_return_info();
$this->load->view('pages/yourview/index',$data);
}
And finally to set your order details to the text boxes. You need to echo it inside the text boxes like this.
<input type="Text" name="orderID" value="<?php echo $orderDetails['order_id'];?>" >
If you use result_array() you need to use foreach loop to echo it.
I want to display a value in view in CodeIgniter but I am getting several errors like trying to get the property on non-object. I think my code is correct but I am getting errors. Below is my code.
controller:
public function trainer($id)
{
$user_record = $this->db->query("select * from usr_data where usr_id=$id")->result();
$data['title'] = 'trainer Dashboard';
$data['user_record'] = null;
$data['active_courses'] = [];
$data['inprogress'] = [];
if(count($user_record)) {
$user_record = $user_record[0];
$data['title'] = ucwords($user_record->firstname).' Dashboard';
$data['user_record'] = $user_record;
$active_courses = $this->base_model->getTrainercourseAll($id);
$data['active_courses'] = $active_courses;
$inprogress = $this->base_model->getstaffinprogress($id);
$data['inprogress'] = $inprogress;
}
$this->load->view('trainer-dashboard', $data);
}
model:
public function getstaffinprogress($user_id) {
$result=$this->executeSelectQuery("select AVG(m.percentage) from object_data o, ut_lp_marks m where o.obj_id=m.obj_id and o.type='crs' and m.status=1 ");
return $result;
}
view:
<h3>Avg inprogress:<?php echo "<span style='color:#ff00ff;font-family:verdana;'>".$inprogress->percentage."</span>";?></h3>
I want to display the column percentage which is coming from database.above code is in the controller, model and view.i thought my controller code is wrong.
Anyone help me to get rid of this error. I want to display a value in view in CodeIgniter but I am getting several errors like trying to get the property on non-object. I think my code is correct but I am getting errors. Below is my code.
Try this in your view file,
if(isset($inprogress)){
echo $inprogress->percentage;
}
Then your code look like this,
<h3>Avg inprogress:<?php if(isset($inprogress)){ echo "<span style='color:#ff00ff;font-family:verdana;'>".$inprogress->percentage."</span>";}?></h3>
Then call the controller function. I think inprogress is not set at the first time.
If it doesn't work, try to var_dump($inprogress) in controller and check value and type.
And try this code in your model. Query also seems not correct
public function getstaffinprogress($user_id) {
$this->db->select_avg('ut_lp_marks.percentage');
$this->db->where('ut_lp_marks.obj_id', $user_id);
$this->db->where('object_data.type', 'crs');
$this->db->where('ut_lp_marks.status', 1);
$this->db->join('object_data', 'object_data.obj_id = ut_lp_marks.obj_id');
$query = $this->db->get('ut_lp_marks');
return $query->result_array();
}
I assume that your db is ut_lp_marks. Then var_dump array and check data is correct first. Then access array element.
public function getstaffinprogress($user_id) {
$result = array();
$query=$this->db->query("select AVG(m.percentage) from object_data o, ut_lp_marks m where o.obj_id=m.obj_id and o.type='crs' and m.status=1 ");
foreach($query->result() as $row){
$result = $row;
}
return $result;
}
Also check $inprogress->percentage exists before print in view.
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();
i'm in mid of creating my own cms . And now i want to show which one of the category has parent but i don't know how, so please help me.
my category table
idkategori | namakategori | parentid
1 Programming 0
2 PHP 1
Or i need relationship table for my categories?
My Controller so far.
function tampilmenu()
{
$sql = "select * from fc_kategori";
$data['kategori'] = $this->bymodel->tampildata($sql);
$sql1 = "select parentid from fc_kategori";
$data['parent'] = $this->bymodel->tampildata($sql1);
$id=array();
foreach ($data['parent'] as $paren)
{
$id[]=$paren->parentid;
}
foreach ($data['kategori'] as $cat)
if(in_array($cat->parentid,$id))
{
$have ='Yes';
}
else
{
$have ='No';
}
echo $cat->idkategori.$have;
}
}
my model
function tampildata ($sql)
{
$query = $this->db->query($sql);
return $query->result();
}
Please don't laugh on me.
Kindly follow:
1) Since you are using a MVC framework, never write queries inside the controller (queries should always be written in models).
2) Never use raw queries, since CI provides you what is called as Active Record.
3) Also never pass direct queries anywhere you'll possibly code in whichever language. Always pass data and make do that function to compute and query process.
4) Remember, in CI Models are only used for database functionalities, Views are only used for your HTML markups and Controllers acts as the mediator between models and views.
Your code:
Controller -
public function tampilmenu()
{
$categories = $this->bymodel->get_category_having_parent();
echo "<pre>"; print_r($categories);
// this will return object having all categories that are parents
}
Model -
public function get_category_having_parent()
{
$parent_ids = array();
$ps = $this->get("parentid");
foreach($ps as $p)
{
$parent_ids[] = $p->parentid;
}
$this->db->where_in("id", $parent_ids);
$query = $this->db->get("fc_kategori");
return $query->result();
}
public function get($column="*")
{
$this->db->select($column);
$query = $this->db->get("fc_kategori");
return $query->result();
}
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();
}