How to get a certain php variable that is inside the view in the controller [closed]

1

Good morning I have a page in php that is inside the view folder in codeigniter, there I have a foreach that I am listing the alphabet and I wanted to get this variable ($ letter) inside the controller. Can someone help me?    This and my page within the view:

 <!-- Navigation -->
        <div class="titulo"><img alt="Logo" src="<?php echo base_url('midias/Logo.png')?>" /></div>
        <nav class="navbar navbar-inverse navbar-static-top" role="navigation">
                <!-- Brand and toggle get grouped for better mobile display -->
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                </div>
                <!-- Collect the nav links, forms, and other content for toggling -->
                <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <?php foreach(range('A', 'Z') as $letra) {?>
                    <ul class="nav navbar-nav">
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><?php print $letra;?><span class="caret"></span></a>
                           <form method="post"> <input type="hidden" name="inicial"  value="<?php echo $letra;?>" /></form>
                            <ul class="dropdown-menu">
                               <?php foreach ($listar as $lista):?>
                               <li><a href="#"><?php echo $lista['nome_categoria']?></a></li>
                               <?php endforeach;?> 
                            </ul>
                         </li>
                    </ul>
                    <?php }?>
                </div>
                <!-- /.navbar-collapse -->
            <!-- /.container -->
        </nav>

And this is my controller:

<?php
class Index_controller extends CI_Controller{
    public function index(){
        $this->load->helper('form');
        $alfabeto['l'] =  $_POST['alfabeto'];
        echo $alfabeto['l'];
        $this->load->model('categoria_model','model',true);
        $alfabeto['listar'] = $this->model->listar($alfabeto['l']);
        $this->load->view('menu',$alfabeto);
        $this->load->view('home');
    }

}

What I want to do, I created a select that has a like that is receiving a paramenter inside my model

public function listar($letra){
        $this->db->select('nome_categoria');
        $this->db->from('categoria');
        $this->db->like('nome_categoria',$letra,'after');
        return $this->db->get()->result_array();
    }

Then I get this model in cotroller like this:

public function index(){

        $this->load->helper('form');

        $this->load->model('categoria_model','model',true);

        $alfabetoNoController = range('A', 'Z'); // :)

        $alfabeto['alfabeto'] = $alfabetoNoController;
        $alfabeto['letra'] = null;
        $alfabeto['listar'] = $this->model->listar($alfabeto['letra']);//aqui estou pegando o model com o parametro
        $this->load->view('menu', $alfabeto);
        $this->load->view('home');
}

that alphabet ['letter'] has come from the foreach that acontence it in the view, which has a $ letter, that here:

<!-- Navigation -->
    <div class="titulo"><img alt="Logo" src="<?php echo base_url('midias/Logo.png')?>" /></div>
    <nav class="navbar navbar-inverse navbar-static-top" role="navigation">
            <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
            </div>
            <!-- Collect the nav links, forms, and other content for toggling -->
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <?php foreach($alfabeto as $letra)//eu tenho que pega essa letra aqui, mas nao sei como {?>
                <ul class="nav navbar-nav">
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><?php print $letra;?><span class="caret"></span></a>
                       <form method="post"> <input type="hidden" name="alfabeto"  value="<?php echo $letra;?>" /></form>
                        <ul class="dropdown-menu">
                           <?php foreach ($listar as $lista):?>
                           <li><a href="#"><?php echo $lista['nome_categoria']?></a></li>
                           <?php endforeach;?> 
                        </ul>
                     </li>
                </ul>
                <?php }?>
            </div>
            <!-- /.navbar-collapse -->
        <!-- /.container -->
    </nav>
    
asked by anonymous 01.11.2015 / 02:32

1 answer

0

[Edited]

Note that your html uses name=inicial :

 <input type="hidden" name="inicial"  value="<?php echo $letra;?>" />

But $_POST['alfabeto'] uses the key alfabeto , when the correct one should be initial.

Then View can remain the same as its original:

                <?php foreach(range('A', 'Z') as $letra) {?>
                <ul class="nav navbar-nav">
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><?php print $letra;?><span class="caret"></span></a>
                       <form method="post"> <input type="hidden" name="inicial"  value="<?php echo $letra;?>" /></form>
                        <ul class="dropdown-menu">
                           <?php foreach ($listar as $lista):?>
                           <li><a href="#"><?php echo $lista['nome_categoria']?></a></li>
                           <?php endforeach;?> 
                        </ul>
                     </li>
                </ul>
                <?php }?>

And the controller should look like this:

<?php
class Index_controller extends CI_Controller{
    public function index(){
        $this->load->helper('form');

        $alfabeto['l'] =  $_POST['inicial'];
        echo $alfabeto['l'];
        $this->load->model('categoria_model','model',true);
        $alfabeto['listar'] = $this->model->listar($alfabeto['l']);

        $this->load->view('menu',$alfabeto);
        $this->load->view('home');
    }
}

You can create the range loop inside the Controller and then send it to the View:

<?php
class Index_controller extends CI_Controller
{
    public function index()
    {
        $this->load->helper('form');

        $alfabeto['l'] =  $_POST['alfabeto'];

        echo $alfabeto['l'];

        $this->load->model('categoria_model','model',true);

        $alfabetoNoController = range('a', 'z'); // :)

        $alfabeto['listar'] = $this->model->listar($alfabeto['l']);
        $alfabeto['alfabeto'] = $alfabetoNoController;

        $this->load->view('menu', $alfabeto);

        //Se quiser fazer algo com ele no controller use o foreach
        foreach($alfabetoNoController as $letra) {
              //Algo aqui...
        }

        $this->load->view('home');

    }
}

And the View looks like this:

 <!-- Navigation -->
    <div class="titulo"><img alt="Logo" src="<?php echo base_url('midias/Logo.png')?>" /></div>
    <nav class="navbar navbar-inverse navbar-static-top" role="navigation">
            <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
            </div>
            <!-- Collect the nav links, forms, and other content for toggling -->
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <?php foreach($alfabeto as $letra) {?>
                <ul class="nav navbar-nav">
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><?php print $letra;?><span class="caret"></span></a>
                       <form method="post"> <input type="hidden" name="inicial"  value="<?php echo $letra;?>" /></form>
                        <ul class="dropdown-menu">
                           <?php foreach ($listar as $lista):?>
                           <li><a href="#"><?php echo $lista['nome_categoria']?></a></li>
                           <?php endforeach;?> 
                        </ul>
                     </li>
                </ul>
                <?php }?>
            </div>
            <!-- /.navbar-collapse -->
        <!-- /.container -->
    </nav>

    
01.11.2015 / 02:49