Return data in a DIV (Codeigniter)

Asked

Viewed 54 times

0

I have a view where her code is as follows::

            <section>
            <h1 class="text-center">Marcação de Fotos</h1>

            <?php
                echo form_open('admin/exibe_fotos/index');
            ?>

                <label for="categoria">Selecione a categoria referente a foto desejada:</label>
                <select name="categorias" class="form-control">
                    <?php foreach ($listarCategorias->result() as $row) : ?>
                    <option value="<?php echo $row->idCategoria; ?>"><?php echo $row->dscCategoria; ?></option>
                    <?php endforeach ?>
                </select>

                <input type="submit" class="btn btn-primary btn-lg float-right mt-3" value="Buscar">

            <?php 
                echo form_close(); 
            ?>

            <div class="container-galeria row d-flex justify-content-center">
                <?php foreach ($exibeFotos->result() as $row) : ?>
                    <div class="card mx-3 my-3" style="width: 18rem;">
                        <img class="card-img-top" src="<?php echo base_url(); ?>assets/upload/<?php echo $row->caminhoImagem; ?>" alt="<?php echo $row->tituloImagem; ?>">
                        <div class="card-body">
                            <h5 class="card-title"><?php echo $row->tituloImagem; ?></h5>
                            <p class="card-text"><?php echo $row->dscImagem; ?></p>
                            <a href="#" class="btn btn-primary">icone aqui</a>
                        </div>
                    </div>
                <?php endforeach ?>
            </div>
        </section>

Where the view controller is with the following code:

class Exibe_fotos extends CI_Controller {
public function __construct()
{ 
    parent::__construct(); 
    $this->load->helper(array('form', 'url')); 
}
public function index()
{
    $categoria = $this->input->post('categorias');
    $this->load->view('layout/admin/sidebar');
    $this->load->model('admin/tbdimagem');
    $fotos['exibeFotos'] = $this->tbdimagem->exibeFotos($categoria);
    $this->load->view('admin/marcacao_fotos', $fotos);
    $this->load->view('layout/admin/footer');
}

}

The problem is this, is being returned the error "Undefined variable: $exibeFotos". I do not understand, why? It is declared in the view, and also in the controller.

1 answer

0


Good afternoon friend, see that in your foreach inside your view you call ->result(), try to put ->result() in your controller or model query (if you are using).

Your controller:

class Exibe_fotos extends CI_Controller {
public function __construct()
{ 
    parent::__construct(); 
    $this->load->helper(array('form', 'url')); 
}
public function index()
{
  $categoria = $this->input->post('categorias');
  $this->load->view('layout/admin/sidebar');
  $this->load->model('admin/tbdimagem');
  $fotos['exibeFotos'] = $this->tbdimagem->exibeFotos($categoria)->result();
  $this->load->view('admin/marcacao_fotos', $fotos);
  $this->load->view('layout/admin/footer');
}

Your HTML:

         <section>
            <h1 class="text-center">Marcação de Fotos</h1>

            <?php
                echo form_open('admin/exibe_fotos/index');
            ?>

                <label for="categoria">Selecione a categoria referente a foto desejada:</label>
                <select name="categorias" class="form-control">
                    <?php foreach ($listarCategorias as $row) : ?>
                    <option value="<?= echo $row->idCategoria; ?>"><?= echo $row->dscCategoria; ?></option>
                    <?php endforeach ?>
                </select>

                <input type="submit" class="btn btn-primary btn-lg float-right mt-3" value="Buscar">

            <?php 
                echo form_close(); 
            ?>

            <div class="container-galeria row d-flex justify-content-center">
                <?php foreach ($exibeFotos as $row) : ?>
                    <div class="card mx-3 my-3" style="width: 18rem;">
                        <img class="card-img-top" src="<?php echo base_url(); ?>assets/upload/<?php echo $row->caminhoImagem; ?>" alt="<?php echo $row['tituloimagem']; ?>">
                        <div class="card-body">
                            <h5 class="card-title"><?= echo $row->tituloImagem; ?></h5>
                            <p class="card-text"><?= echo $row->dscImagem; ?></p>
                            <a href="#" class="btn btn-primary">icone aqui</a>
                        </div>
                    </div>
                <?php endforeach ?>
            </div>
        </section>

See if it works... I recommend not very popular your index(){} in the controller with many view directives... I would make a function for each call, better understanding and for future errors, quick solutions...

Browser other questions tagged

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