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>
Related
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);
}
I am facing the following error:
Trying to get property of non-object and Undefined variable php
errors in my code
Controller:
function showDoctorInformation(){
$this->load->model('PatientModel');
$data['doctorinfo'] = $this->PatientModel->getDoctorInformation();
$this->parser->parse('patient_msgview', $data);
}
Model:
function getDoctorId() {
$this->db->from('person');
$this->db->select('doctorId');
$doctorId = $this->db->get()->result();
return $doctorId;
}
function getDoctorInformation() {
$doctorId = $this->getDoctorId();
$this->db->from('DoctorInfo');
$this->db->where('doctorId', $doctorId);
$this->db->select('name', 'surname', 'Bio', 'Address', 'img');
$doctorinfo = $this->db->get()->result();
return $doctorinfo;
}
View:
<?= $doctorinfo->name ?>
I have displayed information from the database before with this method and I can't see the error now.
result() return
This method returns the query result as an array of objects, or an
empty array on failure
So you need to fetch single data form your database using ->row()
function getDoctorId() {
$this->db->select('doctorId');
$this->db->from('person');
$this->db->select('doctorId');
$query = $this->db->get();
if ($query->num_rows == 1) {
$row=$query->row();// fetch single row
return $row->doctorId;// get doctor id
} else {
return FALSE;
}
}
And in viwe you have to get your data using foreach loop
For exm
foreach ($doctorinfo as $row)
{
echo $row['title'];
echo $row['name'];
echo $row['body'];
}
I can't seem to figure about what is wrong with this code. thank yu for your help.
I am getting error messages:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: views/members.php
Line Number: 24
My model:
public function show_user()
{
$this->db->select('');
$this->db->from('users');
$this->db->where('email',$this->input->post('email'));
$q=$this->db->get('');
if($q->num_rows() > 0 )
{
$data = array();
foreach($q->result() as $row)
{
$data=$row;
}
return $data;
}
}
My controller:
public function members()
{
if ($this->session->userdata('is_logged_in'))
{
$this->load->model('model_users');
$data['member'] = $this->model_users->show_user();
$this->load->view('members', $data);
}
else
{
redirect('main/restricted');
}
}
My view:
echo "<p> Congratulations you are logged in!!</p>";
foreach($member as $row)
{
echo $row->Name. "<br/>";
echo $row->email. "<br/>";
}
So this my code. I want to display user information from database but I am getting errors. please help me..
You only need to return $q-result();
if($q->num_rows() > 0 )
{
return $q-result();
}
I'm not sure what your database looks like but I suspect this is because either the Name or email properties don't exist on the $row object. Are you sure your field in your users table is called Name or is it name (lowercase)? If its lowercase your code should be.
foreach($member as $row)
{
echo $row->name. "<br/>"; //Changed this line
echo $row->email. "<br/>";
}
Also your foreach loop on your model is a bit pointless. You might as well remove it and just do
return $data->row;
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.
I'm trying to delete message rows from my database as part of a social networking site for a college project but am getting an error: Severity: Notice Message: Undefined index: id in my view. Any help much appreciated!!!!
This is my method in the Model:
function delMessage($id)
{
$this->db->where(array('id' => $id));
$this->db->delete('messages');
}
This is my method in my Controller:
function delMessage($id) {
$this->messages->delMessage($id);
redirect('message');
}
And this is the code in my view:
if(!empty($messages)){
foreach($messages as $message):
$delete = $message['id'];
var_dump($message); ?>
<li><?=$message['from']?> says...: "<?=$message['message']?>"(<?=anchor("message/delMessage/$delete", 'delete')?>)</li>
<?php endforeach?>
<?php }else{ ?>
<?php echo 'No Messages to View'; }?>
The way you have this written you are calling the view without passing any data to it. If you're going to loop through an array in your view, you have to pass the array from your controller.
Your controller needs to look more like the following:
function delMessage($username) {
$this->load->model('messages_model');
$data['messages'] = $this->messages->deleteUserMessage($username);
$this->load->view('message', $data);
}
In order for that to work, your model has to do more than just delete. It also has to query the database and return the array $messages.
I could try to help you more, but looking at your code I can't figure out what you're trying to do. You've assigned the variable $delete, but you never used it. You are dumping the array on each loop, and you're echoing 'from' each time. At this point, that's like Egyption heiroglyphics. Provide some more info please.
UPDATE BASED ON THE NEW INFORMATION PROVIDED:
function deleteUserMessage($username) {
$this->db->where('from', $username);
$this->db->or_where('to', $username);
$query = $this->db->get('messages');
if ($query->num_rows() > 0){
$messages = $query->result_array();
$this->db->where('from', $username);
$this->db->or_where('to', $username);
$this->db->delete('messages');
return $messages;
}
}