0

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><a href="<?php echo base_url().'homepage'?>" title="Home"><h4>Home</h4></a></li>
     <li><a href="<?php echo base_url().'news-and-events'?>" title="News and Events"><h4>News and Events</h4></a></li>
     <li><a href="" title="Activities"><h4>Activities</h4></a></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>&nbsp;</td>
            </tr>
            <tr>
              <td >Event: </td>
              <td ><input  type="text" name="event" value="<?php echo set_value('event');?>" /></td>
            </tr>
            <tr>
              <td>&nbsp;</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%">&nbsp;</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

7
  • dump $this->data['allData'] in your controller - what is the output? Commented Mar 7, 2014 at 14:58
  • i tried to print_r it no Date Commented Mar 7, 2014 at 15:22
  • echo the result of $this->db->last_query() to make sure your query is correct Commented Mar 7, 2014 at 15:26
  • where will i echo it? Commented Mar 7, 2014 at 15:27
  • in your model or controller - after the getAllData method is executed Commented Mar 7, 2014 at 15:37

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

hi its ok now thanks for the comments i change my query into this one return $this->db->get('news_and_updates') ->result_object();
how will i able to add the order by??
if you use the get method that you posed in @Patrick's comments add this before that: $this->db->order_by("Date", "desc");
0

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.

4 Comments

ahh i see ahm can you check my query?? is that correct?
Hi you are correct my model code query has something wrong that's why it has error
i change it to this query return $this->db->get('news_and_updates') ->result_object();
I see the issue is solved thanks to m79lkm, Don't forget to mark the question as answered :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.