Jump to content

Recommended Posts

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 ?>

Link to comment
https://forums.phpfreaks.com/topic/266219-undefined-variable/
Share on other sites

@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.

Link to comment
https://forums.phpfreaks.com/topic/266219-undefined-variable/#findComment-1364410
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/266219-undefined-variable/#findComment-1364561
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.