cerberus478 Posted July 25, 2012 Share Posted July 25, 2012 Hi I have an error that say A PHP Error was encountered Severity: Notice Message: Undefined variable: bras Filename: bras/index.php Line Number: 1 I don't know where the problem is, because I have used this code before and it works. This is my bras controller <?php class Bras extends CI_Controller { public function __construct() { parent::__construct(); $this->load->model('bras_model'); } public function index() { $data['bras'] = $this->bras_model->get_bras(); $data['title'] = 'bras archive'; $this->load->view('templates/header', $data); $this->load->view('bras/index', $data); $this->load->view('templates/footer'); } public function view($slug) { $data['bras_item'] = $this->bras_model->get_bras($slug); if (empty($data['bras_item'])) { show_404(); } $data['title'] = $data['bras_item']['title']; $this->load->view('templates/header', $data); $this->load->view('bras/view', $data); $this->load->view('templates/footer'); } public function create() { $this->load->helper('form'); $this->load->library('form_validation'); $data['title'] = 'Create a bra item'; $this->form_validation->set_rules('title', 'Title', 'required'); $this->form_validation->set_rules('content', 'content', 'required'); if ($this->form_validation->run() === FALSE) { $this->load->view('templates/header', $data); $this->load->view('bras/create'); $this->load->view('templates/footer'); } else { $this->bras_model->set_bras(); $this->load->view('admin/bras/success'); } } } This is my bras model <?php class Bras_model extends CI_Model { public function __construct() { $this->load->database(); } public function get_bras($slug = FALSE) { if ($slug === FALSE) { $query = $this->db->get('bras'); return $query->result_array(); } $query = $this->db->get_where('bras', array('slug' => $slug)); return $query->row_array(); } public function set_bras() { $this->load->helper('url'); $slug = url_title($this->input->post('title'), 'dash', TRUE); $data = array( 'title' => $this->input->post('title'), 'slug' => $slug, 'content' => $this->input->post('content') ); return $this->db->insert('bras', $data); } } and this is my index <?php foreach ($bras as $bras_item): ?> <h2><?php echo $bras_item['title'] ?></h2> <div id="main"> <?php echo $bras_item['image']; ?> <?php echo $bras_item['content'] ?> </div> <p><a href="bras/<?php echo $bras_item['slug'] ?>">View bra</a></p> <?php endforeach ?> Quote Link to comment https://forums.phpfreaks.com/topic/266219-undefined-variable/ Share on other sites More sharing options...
Jessica Posted July 25, 2012 Share Posted July 25, 2012 Is that your entire index? It is very clear, it says on line 1, the variable $bras is undefined. Which it is. Where do you expect the information to come from? Quote Link to comment https://forums.phpfreaks.com/topic/266219-undefined-variable/#findComment-1364238 Share on other sites More sharing options...
NomadicJosh Posted July 25, 2012 Share Posted July 25, 2012 @cerberus478, If CI works like other MVC frameworks, then $bras should refer to a method in your Bras_Model. I am not sure if get_bras is what you need because it doesn't seem to be retrieving all the fields you are after. However, if get_bras is the correct method, then you might want to start off like this in your view: foreach ($this->get_bras as $key => $bras_item): Also, again I am not sure how CI works, but you might be missing the parent::__construct(); in the __construct method of the Bras_Model. Quote Link to comment https://forums.phpfreaks.com/topic/266219-undefined-variable/#findComment-1364410 Share on other sites More sharing options...
cerberus478 Posted July 26, 2012 Author Share Posted July 26, 2012 @parkerj I did what you suggested and I get this error: A PHP Error was encountered Severity: Notice Message: Undefined property: CI_Loader::$get_bras Filename: bras/index.php Line Number: 1 Quote Link to comment https://forums.phpfreaks.com/topic/266219-undefined-variable/#findComment-1364520 Share on other sites More sharing options...
Mahngiel Posted July 26, 2012 Share Posted July 26, 2012 Foremost, you should never expect your data to work as intended - always have a backup plan. You have zero error checking in your program. Aside from that, your model method get_bras() is of concern to me. You're defaulting the param to a boolean but expecting a string. Quote Link to comment https://forums.phpfreaks.com/topic/266219-undefined-variable/#findComment-1364561 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.