Pass variable from View to Controller

3

I'm doing a forum with PHP and CodeIgniter , and each category should display its subcategories , however, all categories show all sub-categories, how can I make each sub-category appear below its category?

I do not have to do this with a WHERE, but I do not know how to retrieve the category id to do this.

Category Model:

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

Subcategory Model:

<?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>
    
asked by anonymous 08.06.2014 / 18:49

3 answers

1

Opa,
I do not know if you have already, but building a menu with category / subcategory is standard on any language using recursive function

I'm having a bit of time to explain in the smallest detail, but seeing everything you've done, I think 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 select only once , bringing items sorted by codpai . This prevents you from having to make a select every time you have to bring the 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 looks for the children of the parent x . Throw 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>';
   }
}

Then in view you will see your menu you just call the function, starting with the parent 0: (All your main menus should have parent = 0 .

pegaFilhos(0,$itens_de_menu);

Thanks

    
25.06.2014 / 03:12
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);
    }

In other words, it will prepare 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>
    
08.06.2014 / 23:01
0

In the controller you can retrieve 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 in your controller as you like.

    
09.06.2014 / 00:53