Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

156
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda