How to get a certain php variable inside the view in the controller

Asked

Viewed 1,027 times

1

Good morning I have a php page 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 inside 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 parameter 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 take this model in the 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 to come from the foreach that happens there in the view, which has a $letter, this one:

<!-- 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>

1 answer

0

[Edited]

Note that your html uses name=inicial:

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

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

Then the 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 loop range within 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 View would look 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>

  • but how I get the $letter inside the controller?

  • @danielsilva Erai you want range('a', 'z') as $letra or do you want to pick up a specific letter? Because $letra is used inside the loop and at the end it will always catch the z.

  • But how do you want to structure this in Controller, the range generates an array, and you can access one by one like this $alfabetoNoController[0], It depends on where you want to put it in the controller, because if it’s to fly it makes no difference, see I edited the answer. I think the problem is that maybe you don’t quite understand what array is. @danielsilva

  • how do I make this $alphabetNoController[0], be dynamic?

  • $alfabetoNoController[0] for you to understand how accessible the array is, have you seen the editing of my answer? This is what you want? $alfabeto['alfabeto'] = $alfabetoNoController;&#xA;&#xA; $this->load->view('menu', $alfabeto);&#xA;&#xA; //Se quiser fazer algo com ele no controller use o foreach&#xA; foreach($alfabetoNoController as $letra) {&#xA; //Algo aqui...&#xA; } --- An array is like a set of variables. @danielsilva

  • but how I pass $letter dynamically to the $this->model->list(inside);

  • @danielsilva that you did not mention in the question, see now changed the direction, you want to send the range(a, z) for a model that is a different thing. In case you sent with POST was not? Through $alfabeto['l'] = $_POST['alfabeto'];? You want to pass a letter selected by the user pro model is this?

  • and because I have a select having like receive as parameter the $letter. I tried to create an Hidden input, but I don’t know how I can get it in the controller too, I already tried $this->input->name();. But it didn’t work

  • So your problem isn’t getting the $letra and rather catch the $_POST am I correct?

  • yes, I wanted to pick up the $_post

  • @danielsilva I think I found the problem, edited the answer.

  • Now it does not have the Undefined index error: initial

  • @danielsilva I’ll be honest, I think the mistake already happened with $_POST['alfabeto'], because this is a POST, unless you have forced a value to $_POST['alfabeto'] = 'y'; somewhere in your code. I recommend using empty or isset to check if the shipment came by POST. Friend sincerely can not understand the purpose of your code and so it is difficult to answer. I recommend you edit the question trying to put your goal with the code.

Show 8 more comments

Browser other questions tagged

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