Jump to content

How send the variables between the php and tpl


down999

Recommended Posts

Hi,

I have another challenge. :)

 

Here is my .php file.

public function getContent()
    {
        foreach($DB_0 as $DB_0_data)
          $DB_1_array .= "<option values="$DB_0_data['id']">$DB_0_data['name']</option>";

        $this->smarty->assign('DB_2', $DB_2_array);
        $this->smarty->assign('DB_1', $DB_1_array);

        $this->_html .= $this->display(__FILE__, 'views/templates/admin/back_office.tpl');

        return $this->_html;
    }

I have an form in the back_office.tpl file.

<form action="index.php" method="post" class="form-horizontal">
     {foreach $DB_2 as $DB_2_name => $DB_2_data}
        <li class="filter_list_item" draggable="false">
           {$DB_2_name}
           <select>
              {$DB_1}
           </select>
        </li>
     {/foreach}
</form>

But I need add the condition for the option (if match than is selected). And I need run this condition in the tpl file.

Here is how I did it:

<form action="index.php" method="post" class="form-horizontal">
     {foreach $DB_2 as $DB_2_name => $DB_2_data}
        <li class="filter_list_item" draggable="false">
           <select>
              {foreach $DB_1 as $key => $DB_1_data}
                 {$selected = ""}
                 {if $DB_1_data['name'] == $DB_2_name}
                   {$selected = "selected"}
                 {/if}
                 <option {$selected} values="{$DB_1_data['id']}">{$DB_1_data['name']}</option>
              {/foreach}
           </select>
        </li>
     {/foreach}
</form>

But how you can see, there is really too much foreach. I need move the foreach for the $DB_1 outside the foreach of the $DB_2. But I don't know, how is possible to create the if condition.

 

Thank you!!!!!

Edited by down999 (see edit history)
Link to comment
Share on other sites

Maybe do the comparison in the php file, and store the results of the comparison to some array $compResult), then add:

 

$this->smarty->assign('comp_result', $compResult);

 
Then in the tpl file just go through the {$comp_result} array as needed
 
 
Just a thought,
pascal.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...