This one is worth two gold stars.

I am working out of Cristian Darie's PHP and MySQL Ecommerce 2nd edition book and am woefully stuck on chapter 6. For some reason, on the main page load (i.e., index pulling in products_list.tpl) I am getting a repeating option dropdown and all of the choices are a single attribute.

Attached is a picture of the problem. Also, the following is, what I believe, is the relevant code:

From the file called products_list.tpl:

{* Generate the list of attribute values *}
		<p class="attributes">
		
		{* Parse the list of attributes and attribute values *}
		{section name=1 loop=$obj->mProducts[k].attributes}
		
		  {* Generate a new select tag? *}
		  {if $smarty.section.1.first ||
		      $obj->mProducts[k].attributes[1].attribute_name !==
			  $obj->mProducts[k].attributes[1.index_prev].attribute_name}
			{$obj->mProducts[k].attributes[1].attribute_name}:
		  <select name="attr_{$obj->mProducts[k].attributes[1].attribute_name}">
		  {/if}
		  
		    {* Generate a new option tag *}
			<option value="{$obj->mProducts[k].attributes[1].attribute_value}">
			  {$obj->mProducts[k].attributes[1].attribute_value}
			</option>
			
		  {* Close the select tag? *}
		  {if $smarty.section.1.last ||
		      $obj->mProducts[k].attributes[1].attribute_name !==
			  $obj->mProducts[k].attributes[1.index_next].attribute_name}
		  </select>
		  {/if}
			
		{/section}
		</p>		  
	  </td>

To help visually express this problem, I have attached two screen shots. The first shows what the index file shows, which is a strange attribute error. The second picture shows what clicking on an individual product displays: a nice, properly rendered page.

Before you suggest that I simply copy the code block from the latter tpl to replace the former, I will let you know that that experiment yielded fascinatingly bizarre errors that lacked any backtrace save only references to the Smarty framework files.

I am upping the ante on this one: 10 bucks on Amazon to the sleuth who can solve this! problem

Recommended Answers

All 3 Replies

You have the loop in the wrong place, I'm afraid.

{section name=1 loop=$obj->mProducts[k].attributes}

You want to start (and end) looping between the <select></select> tags so that only the option tags are looping.

You have the loop in the wrong place, I'm afraid.

{section name=1 loop=$obj->mProducts[k].attributes}

You want to start (and end) looping between the <select></select> tags so that only the option tags are looping.

I appreciate your advice, but I have to admit I am a little confused as to what it is. What, specifically, do you suspect is incorrect here (as far as I can tell, the loop is contained within the <select> tags)?

I beleive what he meant was this:
in your code (below with line numbers for reference) the loop is started on line 5 and ended on line 27, the <select> is started on line 12 and ended on line 24, you need to have the <select> before line 5 and </select> after line 27. If that makes sense.

At the moment what it is doing is looping (which it should be) but since the <select> is within the loop it processes this every time it loops through, it should be:

<select>
{start_loop}
//looping here
{end_loop}
</select>
{* Generate the list of attribute values *}
		<p class="attributes">
		
		{* Parse the list of attributes and attribute values *}
		{section name=1 loop=$obj->mProducts[k].attributes}
		
		  {* Generate a new select tag? *}
		  {if $smarty.section.1.first ||
		      $obj->mProducts[k].attributes[1].attribute_name !==
			  $obj->mProducts[k].attributes[1.index_prev].attribute_name}
			{$obj->mProducts[k].attributes[1].attribute_name}:
		  <select name="attr_{$obj->mProducts[k].attributes[1].attribute_name}">
		  {/if}
		  
		    {* Generate a new option tag *}
			<option value="{$obj->mProducts[k].attributes[1].attribute_value}">
			  {$obj->mProducts[k].attributes[1].attribute_value}
			</option>
			
		  {* Close the select tag? *}
		  {if $smarty.section.1.last ||
		      $obj->mProducts[k].attributes[1].attribute_name !==
			  $obj->mProducts[k].attributes[1.index_next].attribute_name}
		  </select>
		  {/if}
			
		{/section}
		</p>		  
	  </td>
commented: Thank you, sometimes I'm just plain too lazy to be articulate. +4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.