Message: Undefined property: stdClass::$Date using codeigniter - codeigniter

my code runs well but im getting this error Message: Undefined property: stdClass::$Date and i dont know where this error came from, as far as ive seen my codes are correct. The problem is in my foreach code
here's my controller below
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start();
class News_and_events extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->library('form_validation');
$this->load->model('admin_model', 'am');
}
public function index(){
if($this->session->userdata('logged_in')){
$this->data['title'] = 'News and Events | Spring Rain Global Consultancy Inc Admin Panel';
$this->data['logout'] = 'Logout';
$session_data = $this->session->userdata('logged_in');
$this->data['id'] = $session_data['id'];
$this->data['username'] = $session_data['username'];
$this->data['allData'] = $this->am->getAllData();
$this->load->view('pages/admin_header', $this->data);
$this->load->view('content/news_and_events', $this->data);
$this->load->view('pages/admin_footer');
}else{
redirect('login', 'refresh');
}
}
}
my model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Admin_model extends CI_Model{
public function saveData($array){
$this->db->insert('news_and_updates', $array);
}
public function getAllData(){
return $this->db->select(
'news_and_updates.Event',
'news_and_updates.Description',
'news_and_updates.Date'
)
->from('news_and_updates')
->order_by("Date", "desc")
->get()->result_object();
}
}
?>
and my views
<script type="text/javascript">
$(document).ready(function(){
$("#add_another").click(function(){
alert('test');
});
});
</script>
<div class="container" >
<br />
<br />
<br />
<ul id="nav">
<li><h4>Home</h4></li>
<li><h4>News and Events</h4></li>
<li><h4>Activities</h4></li>
</ul>
<div class="starter-template">
<h1>News And Events</h1>
<?php if($this->session->flashdata('add_another')):?>
<div id="add_another" style="float:left;">
<input type="button" value="Add Another" class="btn btn-primary" />
</div>
<?php else: ?>
<form action="<?php echo base_url().'news-and-events/add'?>" method="post">
<?php echo validation_errors('<div class="error">', '</div>');?>
<table class="table-striped">
<tr>
<td>Date: </td>
<td><input type="text" id="datepicker" name="date" value="<?php echo set_value('date');?>" /></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td >Event: </td>
<td ><input type="text" name="event" value="<?php echo set_value('event');?>" /></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td width="20%">Description: </td>
<td><textarea cols="30" rows="5" name="description" ><?php echo set_value('description');?></textarea></td>
</tr>
<tr>
<td width="20%"> </td>
<td><input type="submit" value="Add" class="btn btn-success" /></td>
</tr>
</table>
</form>
<?php endif; ?>
<br />
<br />
<table class="table" >
<tr>
<th>Date</th>
<th width="51%">Event</th>
<th>Description</th>
<th>Options</th>
</tr>
<?php foreach($allData as $x => $allDatas): ?>
<tr>
<td><?php echo $allDatas->Date; ?></td>
<td><?php echo $allDatas->Event; ?></td>
<td></td>
<td></td>
</tr>
<?php endforeach; ?>
</table>
</div>
</div><!-- /.container -->
<script>
var date = new Date();
var currentMonth = date.getMonth();
var currentDate = date.getDate();
var currentYear = date.getFullYear();
$('#datepicker').datepicker({
minDate: new Date(currentYear, currentMonth, currentDate),
dateFormat: "yy-mm-dd"
});
</script>
in my foreach code the error is this one $allDatas->Date; it says Message: Undefined property: stdClass::$Date
and that's the field name in my table Date
can someone help me figured this out??
any help is much appreciated! thanks! can't find the error

In your model or controller - after the getAllData method is executed:
echo the result of $this->db->last_query()
Manually execute the returned query in your database, then fix your query

As the comments started, The issue is that Date isn't generated from the query, which means something is wrong in that level, not the output itself.
Make sure the query is correct, dump it before you load the view directly from the controller to see what it gets, if it doesn't get a Date, your issue is elsewhere, probably in the query itself.

Related

Form Validation on Array name fields - Codeigniter

I have a dynamic form to add rows. The form lets the users to add transactions and submit it. I want to validate only the transaction numbers before inserting them into DB. I have to check
If the transaction field is empty or not
If the transaction already exists in the transactions table (if yes : display message transaction already exists. Please remove row. If no: proceed to DB insertion)
View part:
<div class="row">
<div class="col-md-12 ">
<?php echo validation_errors(); ?>
<?php
echo form_error("Transaction_number[]");
?>
<?php echo form_open('payment_refund/refund_form/'); ?>
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<table id='tbl' class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th>Transaction_number</th>
<th>Order_number</th>
<th>Amount_paid</th>
<th>Transaction_date</th>
<th>Remove</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type='text' name='Transaction_number[]' value="<?php echo set_value('Transaction_number[]'); ?>" /></td>
<td><input type='text' name='Order_number[]' value="<?php echo set_value('Order_number[]'); ?>" /></td>
<td><input type='number' name='Amount_paid[]' value="<?php echo set_value('Amount_paid[]'); ?>" /></td>
<td><input type='date' name='Transaction_date[]' value="<?php echo set_value('Transaction_date[]'); ?>" /></td>
<td><input type='button' value='Remove' class='rmv'></td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan='3'><input type='button' value='+Add Row' id='add_row'></td>
</tr>
</tfoot>
</table>
<script>
$(document).ready(function(){
$("#add_row").click(function(){
var row="<tr> <td><input type='text' name='Transaction_number[]'></td> <td><input type='text' name='Order_number[]'></td> <td><input type='number' name='Amount_paid[]'></td> <td><input type='date' name='Transaction_date[]'></td> <td><input type='button' value='Remove' class='rmv'></td> </tr>";
$("#tbl tbody").append(row);
});
$("body").on("click",".rmv",function(){
$(this).closest("tr").remove();
});
});
</script>
<!-- Insert a check to see if any transaction details are entered before submit -->
<input type="submit" name="submit" id="submit" value="submit" class="btn btn-primary" >
<?php echo form_close(); ?>
</div>
Controller Part:
class Refund_form extends MY_Controller
{
public function __construct()
{
parent::__construct(array('stu'));
$this->load->model('payment_refund/refund_form_model','',TRUE);
}
public function index()
{
$this->load->helper('form');
$this->load->helper(array('form', 'url'));
$this->load->helper('array');
$this->load->library('form_validation');
$Transaction_number = $this->input->post('Transaction_number');
for($i = 0; $i < count($Transaction_number); $i++)
{
$tran = $Transaction_number[$i];
$this->load->library('form_validation');
$this->form_validation->set_rules($tran, 'Transaction', 'required');
}
if ($this->form_validation->run() == TRUE)
{
echo "All went well <br/>"; // insertion into DB for all table data fetched from view is done here
}
else
{
echo "All is NOT well <br/>";
}
public function add()
{
$this->drawHeader("Refund form - Add");
$this->load->view('payment_refund/refund_form_view');
$this->drawFooter();
}
Other methods that i tried to validate the form
`$formrules = array(
array(
'field' => 'Transaction_number[]',
'label' => 'the transsaction field',
'rules' => 'required'
),
);
$this->form_validation->set_rules($formrules);
if ($this->form_validation->run() == true)
{return true; } //DB insertion
else
{ return false; } `
DB insertion is done as below
for($i=0;$i<count($Transaction_number);$i++){
$data[] = [
'tracking_id' => $tracking_id,
'trans_id' => $Transaction_number[$i],
'order_id' => $Order_number[$i],
'amount' => $Amount_paid[$i],
'date_of_trans' => $Transaction_date[$i],
];
}
//inser_batch
$response=$this->db->insert_batch('refund_transaction_details',$data);
if($response==true){
echo "Records Saved Successfully";
}
else{
echo "Insert error !";
}

Codeigniter: having issue with fetching data as sum of columns

i am new to Codeigniter, i have two tables named as Users and Amount, there are multiple amounts in Amount Table across one user have same user_id which is foreign key. i want to get data and sum up the amounts value if user_id is same.
My Modal is:
public function client_list(){
$this->db->select_sum('*', sum('amount'));
$this->db->from('users');
$this->db->join('amount', 'amount.user_id = users.ID');
$query=$this->db->get();
$result=$query->result();
return $result;
}
Controller is:
public function index(){
$data['records'] = $this->Admin_model->client_list();
$this->load->view('admin/Client_list', $data);
}
View part is:
<?php
foreach($records as $record):
?>
<tr class="odd gradeX">
<td>
<label class="mt-checkbox mt-checkbox-single mt-checkbox-outline">
<input type="checkbox" class="checkboxes" value="1" />
<span></span>
</label>
</td>
<td> <?php echo $record->firstname; ?> </td>
<td> <?php echo $record->lastname; ?> </td>
<td> <?php echo $record->phone;?> </td>
<td> <?php echo $record->email;?> </td>
<td> <?php echo $record->amount;?> </td>
<td> <?php echo $record->dateofbirth;?> </td>
</tr>
<?php endforeach; ?>
Kind Help would be highly appreciated.

Edit Pages - Invalid argument supplied for foreach()

I wonder why I still having this error message appearing.
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/editpages.php
Line Number: 48
Backtrace:
File: C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\masterlinkci2\application\admin\views\editpages.php
Line: 48
Function: _error_handler
controllers/Cpages.php
public function editpagesupdate() {
$data['pagessuccess'] = '';
$pages_id = $this->uri->segment(3);
$data['pages'] = $this->Mpages->update_pages($pages_id);
$this->load->view('editpages', $data);
}
models/Mpages.php
public function update_pages($pages_id)
{
$data = array(
'pages_name' => $this->input->post('pages_name'),
'create_date' => $this->input->post('create_date'),
'pages_order' => $this->input->post('pages_order'),
'pages_content' => $this->input->post('pages_content')
);
$this->db->where('pages_id', $pages_id);
$query = $this->db->update('pages', $data);
//return $query->result_array();
return $query;
}
views/editpages.php
<div class="widget-box">
<div class="widget-title"><h5>Pages</h5></div>
<div class="widget-content">
<?php echo form_open('cpages/editpagesupdate'); ?>
<table border="0" style="width: 100%; height: 90px;">
<tr>
<td>Pages Name</td>
<td><input type="text" name="pages_name" value="<?php echo $pages_item['pages_name']; ?>"></td>
</tr>
<tr>
<td>Create Date</td>
<td><input type="text" name="create_date" value="<?php echo $pages_item['create_date']; ?>"></td>
</tr>
<tr>
<td>Order</td>
<td><input type="text" name="pages_order" value="<?php echo $pages_item['pages_order']; ?>"></td>
</tr>
<tr>
<td>View Content</td>
<td><textarea rows="3" cols="20" name="pages_content"><?php echo $pages_item['pages_content']; ?></textarea></td>
</tr>
<tr>
<td></td>
<td><input type="submit" class="edit" name="submit" value="SUBMIT"></td>
</tr>
</table>
</div>
</div>
<?php endforeach; ?>
controllers/Cpages.php
public function editpagesupdate() {
$data['pagessuccess'] = '';
$pages_id = $this->uri->segment(3);
$update = $this->Mpages->update_pages($pages_id);
$data = array();
/* get updated pages here */
if($update){
$data['pages'] = $this->Mpages->get_pages();
}
$this->load->view('editpages', $data);
}
models/Mpages.php
public function get_pages()
{
$query = $this->db->get('pages');
return $query->result_array();
}
views/editpages.php
<div class="widget-box">
<div class="widget-title"><h5>Pages</h5></div>
<div class="widget-content">
<?php echo form_open('cpages/editpagesupdate'); ?>
<table border="0" style="width: 100%; height: 90px;">
<?php foreach($pages as $pages_item) : ?>
<tr>
<td>Pages Name</td>
<td><input type="text" name="pages_name" value="<?php echo $pages_item['pages_name']; ?>"></td>
</tr>
<tr>
<td>Create Date</td>
<td><input type="text" name="create_date" value="<?php echo $pages_item['create_date']; ?>"></td>
</tr>
<tr>
<td>Order</td>
<td><input type="text" name="pages_order" value="<?php echo $pages_item['pages_order']; ?>"></td>
</tr>
<tr>
<td>View Content</td>
<td><textarea rows="3" cols="20" name="pages_content"><?php echo $pages_item['pages_content']; ?></textarea></td>
</tr>
<?php endforeach; ?>
<tr>
<td></td>
<td><input type="submit" class="edit" name="submit" value="SUBMIT"></td>
</tr>
</table>
</div>
</div>

my modal in image responsive show only first row image from database

I tried to make responsive image for all image thumbnail from my database.
the thumbnail is showing, but when I clicked it, all of it display the first image only. why this is happening?
here is a few line code from view file
<div data-toggle="modal" data-target="#myModal">
<table id="dynamic-table" method="post" class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>No</th>
<th>Nama Lengkap</th>
<th>Tanggal Mulai</th>
<th>Tanggal Akhir</th>
<th>Alasan</th>
<th>File</th>
<th>Nopeg</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php foreach ($hasil->result() as $row) { ?>
<tr>
<td>
<?php echo $row->no ?></td>
<td>
<?php echo $row->nama ?></td>
<td>
<?php echo $row->tglm ?></td>
<td>
<?php echo $row->tgla ?></td>
<td>
<?php echo $row->alasan ?></td>
<td>
<img src="<?php echo base_url().'uploads/'.$row->file ?>" height="75">
<div id="myModal" class="modal fade" role="dialog" data-backdrop="static" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
<img src="<?php echo base_url().'uploads/'.$row->file ?>" class="img-responsive">
</div>
</div>
</div>
</div>
</td>
<td>
<?php echo $row->nopeg; ?></td>
<td>
<button type="button" class="btn btn-pink btn-sm">Terima</button>
<button type="button" class="btn btn-info btn-sm">Tolak</button>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<script type="text/javascript">
$(document).ready(function() {
$('#myModal').on('show.bs.modal', function(e) {
var image = $(e.relatedTarget).attr('src');
$(".img-responsive").attr("src", image);
});
});
</script>
</div>
here is a code from model
<? php
class Model_data_cuti extends CI_Model {
public
function getdata() {
$hasil = $this - > db - > query("SELECT * FROM form");
return $hasil;
}
public
function create($data_form) {
$this - > db - > insert('form', $data_form);
}
public
function find($no) {
$hasil = $this - > db - > where('no', $no) - > limit(1) - > get('form');
if ($hasil - > num_rows() > 0) {
return $hasil - > row();
} else {
return array();
}
}
} ?>
and this is from controller
<? php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Data_cuti extends CI_Controller {
function __construct() {
parent::__construct();
$this - > load - > model('model_data_cuti');
}
public
function index() {
$data['hasil'] = $this - > model_data_cuti - > getdata();
$this - > load - > view('data_cuti_view', $data);
}
} ?>
this is the result page
enter image description here
look at the file column. if i clicke th image, it only display image from first row. when i click others image, it display the first image too.
I see there in your view code, you tried to get the image by
e.relatedTarget for modify your modal.
Mistake :
You do loop the modals, but declare the same id, it was write such as :
<div id="myModal" class="modal fade" role="dialog" data-backdrop="static" aria-labelledby="myModalLabel" aria-hidden="true">
If you inspect the element in your browser you will find that you have a lot of modals with the same id.
Approach Solution :
Just move out the modals from the loop condition, and try to modify it by onclick event listener inside of the true image of yours.
Code Provided ( Untested ) :
<td> <img onclick="photoModal('<?php echo base_url().'uploads/'.$row->file ?>');" src="<?php echo base_url().'uploads/'.$row->file ?>" height="75">
<script>
function photoModal(imgPath){
$('#myModal < div < div < div <img').attr("src",imgPath);
$('#myModal').modal('show');
}
</script>
Explanation
So, while you clicked the image, it will trigger function photoModal() to send the src attribute to the Modal and proccess it to change the src image inside of Modals
I already trick it with some code. My purpose is to make the image larger, so the user can read it.
ps: I already removed the modals.
here is my code in view
<table id="dynamic-table" method="post" class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>No</th>
<th>Nama Lengkap</th>
<th>Tanggal Mulai</th>
<th>Tanggal Akhir</th>
<th>Alasan</th>
<th>File</th>
<th>Nopeg</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php foreach ($hasil->result() as $row) { ?>
<tr>
<td>
<?php echo $row->no ?></td>
<td>
<?php echo $row->nama ?></td>
<td>
<?php echo $row->tglm ?></td>
<td>
<?php echo $row->tgla ?></td>
<td>
<?php echo $row->alasan ?></td>
<td>
<a class="thumbnail" href="<?php echo base_url().'uploads/'.$row->file ?>">
<img src="<?php echo base_url().'uploads/'.$row->file ?>" width="50" height="50">
</a>
</td>
<td>
<?php echo $row->nopeg; ?></td>
<td>
<button type="button" class="btn btn-pink btn-sm">Terima</button>
<button type="button" class="btn btn-info btn-sm">Tolak</button>
</td>
</tr>
<?php } ?>
</tbody>
</table>
if you have another way to make the modal working right, just tell me. thanks. ^^,

ajax delete using codeigniter

Hi i have this delete thing, and im using it on ajax to delete the data. My problem is it wont delete on the database, when i refresh the browser the data remains, Can someone help me out figured this thing??
Here's my controller below
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start();
class News_and_events extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->library('form_validation');
$this->load->model('admin_model', 'am');
}
public function index(){
if($this->session->userdata('logged_in')){
$this->data['title'] = 'News and Events | Spring Rain Global Consultancy Inc Admin Panel';
$this->data['logout'] = 'Logout';
$session_data = $this->session->userdata('logged_in');
$this->data['id'] = $session_data['id'];
$this->data['username'] = $session_data['username'];
$this->data['allData'] = $this->am->getAllData();
$this->load->view('pages/admin_header', $this->data);
$this->load->view('content/news_and_events', $this->data);
$this->load->view('pages/admin_footer');
}else{
redirect('login', 'refresh');
}
}
public function add(){
if($this->session->userdata('logged_in')){
$this->form_validation->set_rules('date', 'Date', 'trim|required|xss_clean');
$this->form_validation->set_rules('event', 'Event', 'trim|required|xss_clean');
$this->form_validation->set_rules('description', 'Description', 'trim|required|xss_clean');
if($this->form_validation->run() == FALSE){
$this->data['title'] = 'News and Events | Spring Rain Global Consultancy Inc Admin Panel';
$this->data['logout'] = 'Logout';
$session_data = $this->session->userdata('logged_in');
$this->data['id'] = $session_data['id'];
$this->data['username'] = $session_data['username'];
$this->data['allData'] = $this->am->getAllData();
$this->load->view('pages/admin_header', $this->data);
$this->load->view('content/news_and_events', $this->data);
$this->load->view('pages/admin_footer');
}else{
$array = array(
'Date' => $this->input->post('date'),
'Event' => $this->input->post('event'),
'Description' => $this->input->post('description')
);
$this->am->saveData($array);
$this->session->set_flashdata('add_another',1);
redirect('news_and_events', 'refresh');
}
}else{
redirect('homepage', 'refresh');
}
}
public function delete(){
$id = $this->uri->segment(2);
$this->am->delete($id);
redirect(base_url().'news-and-events');
}
}
and my views
<script type="text/javascript">
$(document).ready(function(){
$("#add_another").click(function(){
});
});
function goDelete(id){
var agree = confirm("Are you sure you want to delete this?");
if(agree){
$("#news-and-event"+id).fadeOut('slow');
$.post('<?php echo base_url().'news-and-events/delete/'?>', {id:id}, function(){
});
}else{
return false;
}
}
</script>
<div class="container" >
<br />
<br />
<br />
<ul id="nav">
<li><h4>Home</h4></li>
<li><h4>News and Events</h4></li>
<li><h4>Activities</h4></li>
</ul>
<div class="starter-template">
<h1>News And Events</h1>
<?php if(!$this->session->flashdata('add_another')):?>
<form action="<?php echo base_url().'news-and-events/add'?>" method="post">
<?php echo validation_errors('<div class="error">', '</div>');?>
<table class="table-striped">
<tr>
<td>Date: </td>
<td><input type="text" id="datepicker" name="date" value="<?php echo set_value('date');?>" /></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td >Event: </td>
<td ><input type="text" name="event" value="<?php echo set_value('event');?>" /></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td width="20%">Description: </td>
<td><textarea cols="30" rows="5" name="description" ><?php echo set_value('description');?></textarea></td>
</tr>
<tr>
<td width="20%"> </td>
<td><input type="submit" value="Add" class="btn btn-success" /></td>
</tr>
</table>
</form>
<?php else: ?>
<div id="add_another" style="float:left;">
<input type="button" value="Add Another" class="btn btn-primary" />
</div>
<?php endif; ?>
<br />
<br />
<table class="table" >
<tr>
<th>Date</th>
<th width="47%" >Event</th>
<th width="32%">Description</th>
<th>Options</th>
</tr>
<?php foreach($allData as $x => $allDatas): ?>
<tr id="news-and-event<?php echo $allDatas->id; ?>">
<td width="10%"><?php echo $allDatas->Date; ?></td>
<td style="text-align:left;"><?php echo $allDatas->Event; ?></td>
<td style="text-align:left;"><?php echo $allDatas->Description; ?></td>
<td width="10%">
Edit |
<a href="javascript:;" onclick="return goDelete('<?php echo $allDatas->id;?>');" >Delete</a>
</td>
</tr>
<?php endforeach; ?>
</table>
</div>
</div><!-- /.container -->
<script>
var date = new Date();
var currentMonth = date.getMonth();
var currentDate = date.getDate();
var currentYear = date.getFullYear();
$('#datepicker').datepicker({
minDate: new Date(currentYear, currentMonth, currentDate),
dateFormat: "yy-mm-dd"
});
</script>
and my model to delete the database
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Admin_model extends CI_Model{
public function saveData($array){
$this->db->insert('news_and_updates', $array);
}
public function getAllData(){
return $this->db->order_by("Date", "desc")
->get('news_and_updates')
->result_object();
}
public function delete($id){
$this->db->where('id', $id)->delete('news_and_updates');
}
}
?>
im using the $this->uri->segment(2); any help? is much greatly appreciated
thanks
You are sending data via POST but you are trying to delete based on the uri segment.
Try this instead in your controller:
public function delete(){
$id = $this->input->post('id');
$this->am->delete($id);
redirect(base_url().'news-and-events');
}

Resources