Pass View Variable to Controller

Asked

Viewed 6,202 times

3

I’m doing a forum with PHP and Codeigniter, and each category must display its respective sub-categories, however, all categories, show all sub-categories, how can I make each sub-category appear below its category?

Without which I should do it with a WHERE, but I don’t know how to recover the category id to do it.

Category model:

<?php
class Categorias extends CI_Model{
    public function findAll(){
        $query = $this->db->get("categorias");
        return $query->result();
    }
}

Model of sub-categories:

<?php
class Subcategorias extends CI_Model{
    public function findByIdCategoria($id){
        $this->db->where("id_categoria", $id);
        $query = $this->db->get("subcategorias");
        return $query->result();
    }
}

Controller:

<?php
class Main extends CI_Controller{
    public function __construct(){
        parent::__construct();
        $this->load->helper("url");
        $this->load->model("categorias"); 
        $this->load->model("subcategorias");
    }

    public function index(){
        $data['categorias'] = $this->categorias->findAll();
        $data['subcategorias'] = $this->subcategorias->findByIdCategoria(/*preciso pegar o id para passar para esse parametro */);
        $this->load->view("home", $data);
    }
}

View that displays all categories:

<html lang="pt-br">
<head>
<title>Fórum!</title>
<meta charset="UTF-8" />
<link rel="stylesheet" href="<?php echo base_url(); ?>estilo/reset.css" type="text/css" />
<link rel="stylesheet" href="<?php echo base_url(); ?>estilo/padrao.css" type="text/css" />
</head>
<body>
<div id="site">

        <div id="topo"></div>
        <div id="linha"></div>
        <div id="principal">
            <div id="sidebar">
                <div class="box_titulo_pequeno"><span class="titulo">Menu</span></div>
                <div class="box_conteudo_pequeno"><div class="dez_margin">Início</div></div>

                <div class="box_titulo_pequeno"><span class="titulo">Facebook</span></div>
                <div class="box_conteudo_pequeno"><div class="dez_margin">Curta nossa página!</div></div>

            </div>
            <div id="conteudo">
                <?php
                foreach($categorias as $row){
                ?>
                    <div class="box_titulo_grande"><span class="titulo"><?php echo $row->titulo; ?></span></div>
                    <div class="box_conteudo_grande">
                        <div class="dez_margin">
                            <?php
                            foreach($subcategorias as $row){
                            ?>
                            <table width="100%" border="1" style="margin-top:5px; background:#E5E5E5;">
                                <tr>
                                    <td rowspan="2" style="background:url(<?php echo base_url(); ?>estilo/imagens/<?php echo $row->imagem; ?>); width:86px; height:86px;"></td>
                                    <td class="tdtitulo"><?php echo anchor("subcategoria/" . $row->id, $row->titulo); ?></td>
                                    <td rowspan="2" class="tdtopicos">
                                        <center>
                                            <br />
                                            <br />
                                            3 tópicos
                                        </center>
                                    </td>
                                </tr>
                                <tr>
                                    <td class="tddescricao"><?php echo $row->descricao; ?></td>
                                </tr>
                            </table>
                            <?php
                            }
                            ?>
                        </div>
                    </div>
                <?php
                }
                ?>
            </div>
        </div>
    </div>
</body>
</html>

3 answers

1

Opa,
I don’t know if you’ve solved it yet, but building a menu with category/subcategory is standard in any language using recursive function

I’m a little out of time to explain in detail, but given everything you’ve done, I believe you’ll be able to understand the concept.

You have to have a table with the basic structure:

cod|nome|codpai  
1  |Bla1|0  
2  |Bla2|0  
3  |Bla3|0  
4  |Bla4|1  
5  |Bla5|1  
6  |Bla6|3  
7  |Bla7|2  
8  |Bla8|2  
9  |Blax|0  
10 |Blaz|3

You make the select only once, bringing the items ordered by codpai . This prevents you from having to make a select every time you have to bring your children

$this->db->select('cod,nome,codpai');
$this->db->order_by('codpai','ASC');
$query = $this->db->get('categorias');
$itens_de_menu = $query->result();

Having this we need a function that searches for the father’s children x . Play her in a helper

 if(!function_exists('pegaFilhos'))
{
   function pegaFilhos( $id_do_pai , $itens )
   {
        echo '<ul>';
        foreach($itens as $filho)
        {
            if($filho->codpai == $id_do_pai)
            {
                echo '<li>'.$filho->nome.'</li>';
                pegaFilhos($filho->codpai,$itens);
            }
        }
        echo '</ul>';
   }
}

There in view that you will display your menu you just call the function, starting with the parent 0: ( All your main menus should have father=0 . Simple, making only a select and using a view. Then you just need to work on the display to suit your layout, in the example I used a menu leveling with lists.

pegaFilhos(0,$itens_de_menu);

Thanks

0

Solution:

Controller Main:

<?php
    class Main extends CI_Controller{
        public function __construct(){
            parent::__construct();
            $this->load->helper("url");
            $this->load->model("categorias"); 
            $this->load->model("subcategorias");
        }

    public function index(){
        $data = array();
        $i = -1;
        foreach($this->categorias->findAll() as $categoria){
            $data['cs'][++$i]['categoria']  = $categoria;
            $data['cs'][$i]['subcategoria'] = $this->subcategorias->findByIdCategoria($categoria->id);
        }
        $this->load->view("home", $data);
    }

That is, will be prepared a Array so that the two items are in the same position when mounting the view.

View:

<html lang="pt-br">
<head>
<title>Fórum!</title>
<meta charset="UTF-8" />
<link rel="stylesheet" href="<?php echo base_url(); ?>estilo/reset.css" type="text/css" />
<link rel="stylesheet" href="<?php echo base_url(); ?>estilo/padrao.css" type="text/css" />
</head>
<body>
    <div id="site">
        <div id="topo"></div>
        <div id="linha"></div>
        <div id="principal">
            <div id="sidebar">
                <div class="box_titulo_pequeno"><span class="titulo">Menu</span></div>
                <div class="box_conteudo_pequeno"><div class="dez_margin">Início</div></div>
                <div class="box_titulo_pequeno"><span class="titulo">Facebook</span></div>
                <div class="box_conteudo_pequeno"><div class="dez_margin">Curta nossa página!</div></div>
            </div>
            <div id="conteudo">
                <?php foreach($cs as $cat){ ?>
                <div class="box_titulo_grande"><span class="titulo"><?php echo $cat['categorias']->titulo; ?></span></div>
                <div class="box_conteudo_grande">
                    <div class="dez_margin">
                        <?php
                        foreach($cat['subcategorias'] as $row){
                        ?>
                        <table width="100%" border="1" style="margin-top:5px; background:#E5E5E5;">
                            <tr>
                                <td rowspan="2" style="background:url(<?php echo base_url(); ?>estilo/imagens/<?php echo $row->imagem; ?>); width:86px; height:86px;"></td>
                                <td class="tdtitulo"><?php echo anchor("subcategoria/" . $row->id, $row->titulo); ?></td>
                                <td rowspan="2" class="tdtopicos">
                                    <center>
                                        <br />
                                        <br />
                                        3 tópicos
                                    </center>
                                </td>
                            </tr>
                            <tr>
                                <td class="tddescricao"><?php echo $row->descricao; ?></td>
                            </tr>
                        </table>
                        <?php } ?>
                    </div>
                </div>
                <?php } ?>
            </div>
        </div>
    </div>
</body>
</html>

0

In the controller you can recover the values as follows:

POST

$this->input->post('Campo');

GET

$this->input->get('Campo');

With this you can manipulate the results received from the view and call the methods on your controller as you want.

Browser other questions tagged

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