Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
This code keeps generating more than two select tags

here is the code:

{section name=k loop=$obj->mProduct.attributes}

  {* Generate a new select tag? *}
  {if $smarty.section.k.first ||
      $obj->mProduct.attributes[k].attribute_name !==
      $obj->mProduct.attributes[k.index_prev].attribute_name}
    {$obj->mProduct.attributes[k].attribute_name}:
  <select name="attr_{$obj->mProduct.attributes[k].attribute_name}">
  {/if}

    {* Generate a new option tag *}
    <option value="{$obj->mProduct.attributes[k].attribute_value}">
      {$obj->mProduct.attributes[k].attribute_value}
    </option>

  {* Close the select tag? *}
  {if $smarty.section.k.last ||
      $obj->mProduct.attributes[k].attribute_name !==
      $obj->mProduct.attributes[k.index_next].attribute_name}
  </select>
  {/if}

{/section}

I keep getting more than two select statements.

PLEASE HELP...

here is the picture of the database,

I expect:

Color:<select name="attr_Color">
             <option value="White">White</option>
             <option value="Black">Black</option>
            //....
         </select>
   Size:<select name="attr_Size">
            <option value="XL">XL</option>
            <option value="L">L</option>
            //....
        </select>

However, I am getting multiple select tags instead of just two:

Color: <select name="attr_Color">
       <option value="White">White</option>
</select>
Color: <select name="attr_Color">
       <option value="black">black</option>
</select>
//...
Size: <select name="attr_Size">
       <option value="l">l</option>
</select>
Size: <select name="attr_Size">
       <option value="XL">XL</option>
</select>
//...

I can solve this pretty well with PHP, however, Smarty 3 is proving stubborn. You guys should help me out here.

I might migrate to twigs for separating presentation tier from business logic.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

First, move your tag outside of the smarty for loop. For example:

{* Generate a new select tag? *}
<select name="attr_{$obj->mProduct.attributes[k].attribute_name}">

  {section name=k loop=$obj->mProduct.attributes}

    {* Generate a new option tag *}
    <option value="{$obj->mProduct.attributes[k].attribute_value}">
      {$obj->mProduct.attributes[k].attribute_value}
    </option>

  {/section}

</select>

This will return all attributes into one select box. You'll still need to group by attribute name somehow. This a bit outside the scope of your question, but:

SQL Method You'll need to perform a GROUP BY DISTINCT SQL query on your attribute name to find all relevant attribute types, then iterate over the values for each type.

PHP Method Continue querying the same way you are now, but preprocess your data on the backend before you render it in your smarty template:

$attributes = array()
[for each row in sql query]
  if ($attributes[$row.name] == null) {
    $attributes[$row.name] = array()
  }
  $array_push($attributes[$row.name], $row.value)
[/end for]

In the end you'll have a nested for loop that does something like this (pseudo code):

[For each attribute_name in names]
    <select name="{attribute_name}">
      [For each attribute]
          <option value="{$obj->mProduct.attributes[k].attribute_value}">
            {$obj->mProduct.attributes[k].attribute_value}
          </option>
      [/end for (attribute)]
    </select>
[/end for (names)]

If needed, you could wrap the whole <select/> in an if statement to hide it if there are no attributes to choose from.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!