简体   繁体   中英

How to assign values from associative array to check boxes using foreach in smarty?

I'm assigning an array named $users_criteria to smarty template. The array is as follows:

$users_criteria = array(
          1 => array(
                 'label' => 'Registered users',
                 'condition' => 'date_range'
               ),
          2 => array(
                 'label' => 'Logged-in users',
                 'condition' => 'date_range'
               ),
          3 => array(
                 'label' => 'Not logged-in Users',
                 'condition' => 'date_range'
               ),
          4 => array(
                 'label' => 'Never logged-in Users'
               ),
          5 => array(
                 'label' => 'Package purchase',
                 'condition' => 'package_type'
               ),
          6 => array(
                 'label' => 'Try demo packages',
                 'condition' => 'demo_packages'
               )
        ); 
$smarty->assign('users_criteria', $users_criteria);

        $file_to_show = 'contact-list-import.tpl';

Now there are six checkboxes in a template and I want to assign the label values from the above array to the value attribute of each checkbox. That is each checkbox should have a single value from the above array. In short my requirement is: The key having value 1 from the above array should be mapped to the checkbox having name registered_users, key having value 2 from the above array should be mapped to the checkbox having name logged_in_users,...Similar in this manner to all the six check boxes. I tried to achieve this but the repetitive combinations of check boxes I got. My code is as follows:

{foreach from=$users_criteria key=myId item=criteria}
                  <tr height="30">
                    <td align="left" width="20%">
                    <input type="checkbox" class="user_checkbox" name="user_checkbox" id="registered_users" value="{$criteria.label}"/>Registered users &nbsp;&nbsp;  
                    </td>
                    <td valign="middle" align="left" width="10%"><b>From Date </b> : </td>
                    <td align="left" >&nbsp;<input type="text" style="width:100px;" class="inputfield" name="registered_users_from_date" id="registered_users_from_date" maxlength="10" width="20%"></td>
                    <td valign="middle" align="left" width="10%"><b>To Date </b> : </td>
                    <td>&nbsp;<input type="text" style="width:100px;" class="inputfield" name="registered_users_to_date" id="registered_users_to_date" maxlength="10" width="20%"></td> 
                  </tr>
                  <tr><td colspan="5">&nbsp;</td></tr>
                  <tr height="30">
                    <td align="left" width="20%">
                    <input type="checkbox" class="user_checkbox" name="user_checkbox" id="logged_in_users" value="{$criteria.label}"/>Logged-in users &nbsp;&nbsp;  
                    </td>
                    <td valign="middle" align="left" width="10%"><b>From Date </b> : </td>
                    <td align="left" >&nbsp;<input type="text" style="width:100px;" class="inputfield" name="registered_users_from_date" id="logged_in_users_from_date" maxlength="10"></td>
                    <td valign="middle" align="left" width="10%"><b>To Date </b> : </td>
                    <td>&nbsp;<input type="text" style="width:100px;" class="inputfield" name="registered_users_to_date" id="logged_in_users_to_date" maxlength="10"></td>
                  </tr>
                  <tr><td colspan="5">&nbsp;</td></tr>
                  <tr height="30">
                    <td align="left" width="20%">
                    <input type="checkbox" class="user_checkbox" name="user_checkbox" id="not_logged_in_users" value="{$criteria.label}"/>Not logged-in Users &nbsp;&nbsp;  
                    </td>
                    <td valign="middle" align="left" width="10%"><b>From Date </b> : </td>
                    <td align="left" >&nbsp;<input type="text" style="width:100px;" class="inputfield" name="not_logged_in_users_from_date" id="not_logged_in_users_from_date" maxlength="10"></td>
                    <td valign="middle" align="left" width="10%"><b>To Date </b> : </td>
                    <td>&nbsp;<input type="text" style="width:100px;" class="inputfield" name="not_logged_in_users_to_date" id="not_logged_in_users_to_date" maxlength="10"></td>
                  </tr>
                  <tr><td colspan="5">&nbsp;</td></tr>
                  <tr height="30">
                    <td width="300" colspan="5">
                    <input type="checkbox" class="user_checkbox" name="user_checkbox" id="never_logged_in_users" value="{$criteria.label}"/>Never logged-in Users  
                    </td>
                  </tr>
                  <tr><td colspan="5">&nbsp;</td></tr>
                  <tr height="30">
                    <td width="300" colspan="1">
                    <input type="checkbox" class="user_checkbox" name="user_checkbox" id="package_purchase" value="{$criteria.label}"/>Package purchase  
                    </td>
                    <td align="left"  colspan="4">
                      <select name="test_pack_type_id" id="test_pack_type_id">
                      <option value="">All</option> 
                      {if $all_type}
                       {foreach from=$all_type item="type"}
                      <option value="{$type.package_type_id}">{$type.package_type_name}</option>
                       {/foreach}
                      {/if}
                 </select>
                  </td>
                  </tr>
                  <tr><td colspan="5">&nbsp;</td></tr>
                  <tr height="30">
                    <td width="300" colspan="5">
                    <input type="checkbox" class="user_checkbox" name="user_checkbox" id="try_demo_packages" value="try_demo_packages"/>Try demo packages  
                    </td>
                  </tr>
                  <tr><td colspan="5">&nbsp;</td></tr>
                  {/foreach}

Can you correct the code in order to assign proper values to the checkboxes using foreach in smarty? Thanks in advance.

You can do it using {section} tag. I am giving you an example.

 {section name=i loop=$users_criteria}
    <tr height="30">
        <td align="left" width="20%">
        <input type="checkbox" class="user_checkbox" name="user_checkbox" id="registered_users" value="{$users_criteria[i].label}"/>Registered users &nbsp;&nbsp;  
        </td>
        <td valign="middle" align="left" width="10%"><b>From Date </b> : </td>
        <td align="left" >&nbsp;<input type="text" style="width:100px;" class="inputfield" name="registered_users_from_date" id="registered_users_from_date" maxlength="10" width="20%"></td>
        <td valign="middle" align="left" width="10%"><b>To Date </b> : </td>
        <td>&nbsp;<input type="text" style="width:100px;" class="inputfield" name="registered_users_to_date" id="registered_users_to_date" maxlength="10" width="20%"></td> 
    </tr>
    <tr><td colspan="5">&nbsp;</td></tr>
 {/section}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM