Notice Message: Undefined Index (Codeigniter)

Asked

Viewed 60 times

2

I have a form where two multiple selects is loaded, here the code:

<div class="form-group">
                        <label for="categorias[]">Selecione a(s) categoria(s) referente(s) a foto:</label>
                        <select name="categorias[]" class="form-control" multiple="multiple">
                            <?php foreach ($categorias as $categoria) : ?>
                                <option value="<?= $categoria['idCategoria'] ?>"><?= $categoria['dscCategoria'] ?></option>
                            <?php endforeach ?>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="subcategorias[]">Selecione a(s) subcategoria(s) referente(s) a foto:</label>
                        <select name="subcategorias[]" class="form-control" multiple="multiple">
                            <?php foreach ($subcategorias as $subcategoria) : ?>
                                <option value="<?= $subcategoria['idSubcategoria'] ?>"><?= $subcategoria['dscSubcategoria'] ?></option>
                            <?php endforeach ?>
                        </select>
                    </div>

The controller code that handles this view is as follows:

$this->load->model('admin/tbdcategoria');
    $lista['categorias'] = $this->tbdcategoria->listarCategorias();
    $dados['categorias'] = array('categorias' => $lista['categorias']);

    $this->load->model('admin/tbdsubcategoria');
    $lista['subcategorias'] = $this->tbdsubcategoria->listarSubcategorias();
    $dados['subcategorias'] = array('subcategorias' => $lista['subcategorias']);

    $this->load->view('layout/admin/sidebar');
    $this->load->view('admin/upload_fotos', $dados); 
    $this->load->view('layout/admin/footer');

The model code for data display is as follows:

function listarCategorias()
{
    return $this->db->get('tbdcategoria')->result_array();
}
function listarSubcategorias()
{
    return $this->db->get('tbdsubcategoria')->result_array();
}

The problem is this, in the view instead of loading the categories / subcategories registered in the database, is returning "Undefined Index"

  • It makes it easier for us, what exact line is generating this error ?

  • <option value="<?= $category['idCategoria'] ? >"><?= $category['dscCategoria'] ? ></option> <option value="<?= $subcategory['idSubcategory'] ? >"><?= $subcategory['dscSubcategory'] ? ></option> In these 2 index returns undefined

  • Have you checked if you are mounting this data ? tried to do a var_dump and Talz to be sure ?

  • This error is quite explanatory, you are trying to use an array key that does not exist

  • before using an array key, make a check if this key exists.

  • var_dump is returning "NULL"

  • But there is, because if I leave only one select searching for data from one model only, it loads, from the moment I call another select with another model, it passes to not loading either one

  • As I noticed, if I pass to the example $list['categories'] does not work, if I pass only $list, it works. But then I’ll never be able to upload data to my other select

Show 3 more comments

1 answer

0


I managed to solve, I will leave here what I did, in case someone goes through the same problem.

My view was like this:

<div class="form-group">
                        <label for="categorias[]">Selecione a(s) categoria(s) referente(s) a foto:</label>
                        <select name="categorias[]" class="form-control" multiple="multiple">
                            <?php foreach ($listarCategorias->result() as $row) : ?>
                                <option value="<?php echo $row->idCategoria; ?>"><?php echo $row->dscCategoria; ?></option>
                            <?php endforeach ?>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="subcategorias[]">Selecione a(s) subcategoria(s) referente(s) a foto:</label>
                        <select name="subcategorias[]" class="form-control" multiple="multiple">
                            <?php foreach ($listarSubcategorias->result() as $row) : ?>
                                <option value="<?php echo $row->idSubcategoria; ?>"><?php echo $row->dscSubcategoria; ?></option>
                            <?php endforeach ?>
                        </select>
                    </div>

My controller was like this:

$this->load->model('admin/tbdcategoria');
    $dados["listarCategorias"] = $this->tbdcategoria->listarCategorias();

    $this->load->model('admin/tbdsubcategoria');
    $dados["listarSubcategorias"] = $this->tbdsubcategoria->listarSubcategorias();

    $this->load->view('layout/admin/sidebar');
    $this->load->view('admin/upload_fotos', $dados); 
    $this->load->view('layout/admin/footer');

My model was like this:

function listarCategorias()
{
    $query = $this->db->get("tbdcategoria");

    return $query;
}
function listarSubcategorias()
{
    $query = $this->db->get("tbdsubcategoria");

    return $query;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.