Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

157
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda