pheromix
pheromix

Reputation: 19297

How to work with a phalcon variable inside PHP?

I created a variable inside a controller :

<?php

use Phalcon\Mvc\Controller;

class RestaurantreservationController extends Controller
{

    public function indexAction(){
        return $this->view->pick("reservation/listerReservation");
    }

    public function affecterReservTableAction($id) {
        $this->view->action_form = 'affecterReservTableExec';
        $this->view->titre = 'R&eacute;servation de table';
        $this->view->table_code = $id; // here is the variable I want to manipulate
        return $this->view->pick("reservation/reservTable");
    }

}
?>

Inside the view reservTable.phtml I want to work with the variable table_code :

<div class="input-control select">
        <select name="clt_id" id="clt_id">
            <option value="">--Choisir une table--</option>
            <?php
                $table_code = {{table_code}}; // it generates an error
                $tables = TableClient::lireParCritere([]);
                foreach ($tables as $table) {
                  $select = ($table_code == $table->table_code ? "selected" : "" );
                  ?>
                    <option <?php echo $select; ?> value="<?php echo $table->table_code; ?>"><?php echo $table->noms; ?></option>
                  <?php
                }
            ?>
        </select>
    </div>

How can I use it to set as the selected element of the select element ?

Upvotes: 1

Views: 478

Answers (3)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385144

You used it correctly three lines further down the code. At the point you're trying to use the variable, you're in PHP mode. That means you just write PHP code, i.e. $table_code. The form {{table_code}} is for interpolation inside a VOLT template, not for use inside PHP code.

I would actually recommend converting all of that PHP block into VOLT template code.

Upvotes: 0

Robert
Robert

Reputation: 20286

If you use Phalcon then it is good to use VOLT

When you create Volt type of template you can access variable by {{ table_code }}

If you wan't loop you can use something like

 {% for table in tables %}
   //do something
 {% endfor %}

Volt has also nice function to create selects

{{ select("table", table_code, 'using': ['table_code', 'table_noms']) }}

Upvotes: 3

James Fenwick
James Fenwick

Reputation: 2211

The problem is you are mixing phtml and Volt syntaxes when you try to assign {{table_code}} to $table_code.

The Volt variable {{ table_code }} is the same as $table_code.

Upvotes: 3

Related Questions