Display an array data from two tables in codeigniter - php

I've two tables on my database, monitor [pk = idMonitor] and monitor_data [pk = idMonitor_data].
Please click you can see the tables fields here. As you can see i put the array data in table monitor_data.
I want to Update the condition for every idinventory where monitor_data.idMonitor = $id.
But first i want to display the current data of 'monitordate','idinventory', and 'condition' from database to my view.
My controller
public function edit($id=0) {
$dataa = $this->monitor_m->get_record(array('monitor_data.idMonitor'=>$id),true);
$this->data->monitordate = $dataa->monitordate;
$this->data->condition = $dataa->condition; <-line 20
$this->data->detail = $this->monitor_m->get_record(array('monitor_data.idMonitor'=>$id),true);
$this->template->set_title('SMIB | Monitoring')
->render('monitor_edit',$this->data);
}
My View (monitor_edit)
<?php echo form_open(site_url("monitor/ubah"),'data-ajax="false"'); ?>
<?php foreach ($detail as $items): ?>
<h4><?php echo '[ '.$items['idinventory'].' ] '?> </h4>
<?php echo form_label ('Condition : ');
echo form_dropdown('condition', array('good'=>'Good','broke'=>'Broken','lost'=>'Lost'),#$items['condition']);
?>
<?php endforeach; ?>
<?php echo form_close(); ?>
My Model
class Monitor_m extends MY_Model {
public function __construct(){
parent::__construct();
parent::set_table('monitor','idMonitor');
}
public function get_record($id = 0,$get_user = FALSE) {
$this->db->where($id);
if ($get_user){
$this->db->join('monitor_data','monitor_data.idMonitor = monitor.idMonitor');
$this->db->join('inventory','inventory.idinventory = monitor_data.idinventory');
$this->db->join('user','user.id_user = monitor.id_user');
}
$data = parent::get_array();
return $this->improve_data($data);
}
Here is my problem : its work fine for monitordate code in my controller, BUT i keep getting an error for condition code
Maybe because i use 'monitor_data.idMonitor' as my parameter $id not idinventory. how can i use 2 parameters for example like where idMonitor=$id and idinventory=$idiventory.
Do i explain it right ?
Severity: Notice Message: Trying to get property of non-object
Filename: controllers/monitor.php Line Number: 20
Please Please help me, i dont know what is wrong with my controller :( i've searching the solution but none of those work. :(

It's weird if you can get monitordate but can't get condition.
Can you edit your controller to be like this?
public function get_record($id = 0,$get_user = FALSE) {
$this->db->where($id);
if ($get_user){
$this->db->join('monitor_data','monitor_data.idMonitor = monitor.idMonitor');
$this->db->join('inventory','inventory.idinventory = monitor_data.idinventory');
$this->db->join('user','user.id_user = monitor.id_user');
}
// $data = parent::get_array();
$data = $this->db->result_array();
print_r($data);
echo $this->db->last_query();
exit;
return $this->improve_data($data);
}

Related

How to send multidimensional array as data using array key in codeigniter?

I just want to send some data as array from view to controller using a array key. But can not solve this following problem whenever it comes to send multidimensional array.
Here is my controller
public function all_products(){
$data['ls'] = $this->product_details('product1');
$data['ws'] = $this->product_details('product2');
$data['wr'] = $this->product_details('product3');
$this->load->view('view_page', $data);
}
public function product_details($key){
$data['g'] = // Some rows from database
$data['p'] = // Some rows from database
$data['o'] = // Some rows from database
return $data;
}
After that, if I print the data key, the output showing in view
<?=print_r("ls")?>
// ls1 [Output]
Thanks in advance.
You only need this in your function:
public function product_details($key)
{
$this->db->select()->from('mydb')->where('id', $key);
return $this->db->get()->row();
}
And retrived it in your Controller:
public function all_products()
{
$data['product1'] = $this->Product_model->product_details($key1);
$data['product2'] = $this->Product_model->product_details($key2);
$data['product3'] = $this->Product_model->product_details($key1);
$this->load->view->('product', $data);
}
In your view:
<p>Name of Product 1: <?php echo $product1->product_name; ?></p>
<p>Name of Product 2: <?php echo $product2->product_name; ?></p>
<p>Name of Product 3: <?php echo $product3->product_name; ?></p>

My search code is not outputting any result on codeigniter

Please help!!
I am trying to search database to produce results for my search query but it does not output any result even when the searched term exist in the database
Here is my controller
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Search extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model("profile_model");
$this->load->model("model_home");
}
public function index(){
$data = array();
$search_term = $this->input->post('search');
if($query = $this->model_home->car_search())
{
$data['car'] = $query;
}
$this->load->view('search_results', $data);
}
}
Here is my model
public function car_search($search_term='default')
{
$this->db->select('*');
$this->db->like('car_make',$search_term);
$this->db->or_like('car_model',$search_term);
$this->db->or_like('car_year',$search_term);
$this->db->or_like('registration_number',$search_term);
$this->db->or_like('engine_number',$search_term);
$this->db->or_like('chasis_number',$search_term);
$query = $this->db->get('cars');
return $query->result_array();
}
My search form
<?php
echo form_open('search');
echo form_input(array('name'=>'search'));
echo form_submit('search_submit','submit');
?>
My search result page
<?php
if(isset($car)) :
foreach($car as $row) {
echo $row['car_make'];
} //<-- moved this line
else :
echo '<h1> Not working </h1>';
endif; ?>
Its always outputting "Not Working"
This code is a bit of a mess, if you tidy it up you will see the error
Your code without all the <?php and ?>
<?php
$count = 1;
if(isset($car)) :
foreach($car as $row) {
$count++;
}
echo $row['car_make']
else :
echo '<h1> Not working </h1>';
endif;
?>
So you should see the } in the wrong place ending the foreach loop, quite easily now, so change it to
<?php
$count = 1;
if(isset($car)) :
foreach($car as $row) {
$count++;
echo $row['car_make'];
} //<-- moved this line
else :
echo '<h1> Not working </h1>';
endif;
?>
As you are getting the Not Working message I have to assume there is a problem with your querying of the database.
Probably as you are not passing the search term into the car_search method call. So try passing the paramter you get from the POSTed data.
public function index(){
$data = array();
if($query = $this->model_home->car_search($this->input->post('search'))) {
$data['car'] = $query;
}
$this->load->view('search_results', $data);
}
You should also look at the default value used in the car_search method as the string 'default' will almost definitely cause the query to return no results.

Codeigniter: Sum contents of the records in a field

I am fetching some information from mysql database and I am displaying it using the following script(I am using Codeigniter).
Models
public function sum($field, $table, $userfield, $username)
{
return $this->db->query("SELECT SUM($field) FROM $table WHERE $userfield = '$username'");
}
Controller
public function save(){
$data = array(
'sum_simpan' => $this->user_m->sum('besar_simpanan', 'simpanan', 'id', $this->session->userdata('username'))->result()
); }
View
<div class="info-a">
<p>Total Simpanan Anda</p>
<h3><?php
foreach ($sum_simpan as $row) {
echo $row->besar_simpanan;
}
?>
</h3>
</div>
I want to do is sum the contents of a field, and display it in the view.
However I get an error
A PHP Error was encountered
Severity: Notice
Message: Undefined property: stdClass::$besar_simpanan
Filename: anggota/simpanan.php
Line Number: 14
Would you please kindly show me how to do it.
Thanks in Advance :)
Change your model like this
public function sum($field, $table, $userfield, $username)
{
return $this->db->query("SELECT SUM($field) as total FROM $table WHERE $userfield = '$username'");
}
also the view
<h3>
<?php
foreach ($sum_simpan as $row) {
echo $row->total;
}
?>
</h3>

message undefined variable: news on view page

I'm new to PHP and CodeIgniter, and saw that there is many questions mentioning this and been trying them all but nothing seems to work. Everything is auto-loaded in configuration, the database is running and function for posting to database are working but writing to view page doesn't work at all. Except for displaying username, but for that I create a new variable on view page.
Controller
public function ShowNews()
{
$data = array();
$this->load->model('user');
$data['news'] = $this->user->getNews();
$this->load->vars($data);
}
Model
function getNews(){
$q = $this->db->get('News');
if($q->num_rows() > 0){
return $q->result();
}
return FALSE;
}
View
<?php foreach($news as $row) : ?>
<li><?php echo $row->Title; ?> </li>
<li><?php echo $row->Date; ?></li>
<?php endforeach; ?>
This is the error EDIT ves to news
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: news
Filename: admin/Pocetna.php
Line Number: 64
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: admin/Pocetna.php
Line Number: 64
Using WAMP, NetBeans with CodeIgniter. I saw it has to be something with passing data from controller to view but I can't figure it out and been trying few days already, but always having problems.
You're not passing $data to your view. Your controller should be like this:
public function show_news()
{
$this->load->model('user');
$data = [];
$data['news'] = $this->user->get_news();
$this->load->view('news', $data);
}
Your view should also be checking if $news is FALSE, because you'll have some issues with foreach if you loop over the value FALSE. Your model should also be returning result_array not result, foreach cam't loop over objects..
public function get_news()
{
$q = $this->db->get('News');
return($q->num_rows() > 0) ? $q->result_array() : FALSE;
}
Your view should look something like this:
<?php
if($news !== FALSE)
{
foreach($news as $row)
{
echo "<li>{$row['title']}</li>";
echo "<li>{$row['date']}</li>";
}
}
else
{
echo "No news to see here!";
}
?>
Your title also doesn't link up with the error in the post, so that's the solution to the one in the title.

CodeIgniter $data not passed from Controller to View

my controller is not passing $data to my view and I don't know why not. I'm reusing code from a previous project which worked fine and I certainly understand the idea of how $data passing is meant to work. But maybe I missed something when copying code over?
I put in the variable $data['hello'] in there just for testing purposes. As you can see from the output $hello isn't even getting through. The if fails and the else code is run correctly which means the view file itself is being loaded.
Controller:
function users() {
$data['title'] = 'users';
$data['users'] = $this->main_m->get_users();
$data['hello'] = 5;
$this->load->view('users', $data);
}
View:
<?php
echo $hello;
if ($users->num_rows != 0) {
foreach ($users->result() as $user) {
}
} else {
echo "No users.";
}
Output (abridged):
A PHP Error was encountered
Message: Undefined variable: hello
Line Number: 2
A PHP Error was encountered
Message: Undefined variable: users
Line Number: 3
A PHP Error was encountered
Message: Trying to get property of non-object
Line Number: 3
No users.
Edit: more info on request:
Model:
public function get_users($amount = 0, $offset = 0) {
$this->db->from('users');
$this->db->order_by('l_name', 'desc');
if ($amount != 0)
$this->db->limit($amount, $offset);
return $this->db->get();
}
I always do like this
change your model to
$query = $this->db->get();
return $query->result();
And in view
if (count($users)> 0) {
foreach ($users as $user) {
echo $user['name'];
}
} else {
echo "No users.";
}
Hope this helps
Regards
iijb
Just write $data = array(); before you are sending some data into $data array.
function users() {
$data = array();
$data['title'] = 'users';
$data['users'] = $this->main_m->get_users();
$data['hello'] = 5;
$this->load->view('users', $data);
}
I think you could solve your issue with some simple var_dump() checks.
Check what's coming out of your model by var_dump()ing $data['users'] - is this an object? What happens when you var_dump() $data['users']->result()?
Then, var_dump() $data in your view - does it have all the pieces?
Thing is, even showing us your model function doesn't prove that your getting a real data result. Check that. Your code looks okay at a glance so I don't think that is where the issue exists.
There is something very basic that is wrong. So get out of that controller and do a sanity check. First confirm that your welcome view is working. If it is go to the welcome controller and put this in the index method
$data['here'] = 'we are here' ;
$this->load->view('welcome_message', $data);
and then somewhere in the welcome.php view file
<?php echo $here ?>
You do not need to set this: $data = array();
However some people suggest it because that way even if you dont create any data variables you wont get an error if its in the view call $this->load->view('welcome_message', $data);
finally i would suggest looking at this
function users() {
$data['title'] = 'users';
$data['users'] = $this->main_m->get_users();
$data['hello'] = 5;
$this->load->view('users', $data);
}
lets see you have method called users, returning an object called users, and a view called users -- that could get confusing ! :-)

Categories