11
ghia
Re: Basic foreach structure help
  • 2010/2/16 10:04

  • ghia

  • Community Support Member

  • Posts: 4953

  • Since: 2008/7/3 1


See this.
Don't start topics for the same subject. (Topics merged)

12
JS
Re: Basic shop module
  • 2010/2/16 14:59

  • JS

  • Just popping in

  • Posts: 87

  • Since: 2002/12/16


Ghai thanks for the information. I believe that is exactly what I am trying to do but I will be honest I am having a difficult time translating the example over into my code.

So far I have gotten the categories to all display however for each category it is displaying all the products for all categories.

Here is a copy of my updated code

Now there is a value of $prod.catid which is the same as $cat.id also there is $prod.catname that is the same as $cat.name I am just not sure how to connect the dots correctly in the smarty.
<table>
<
tr>

<{foreach 
item=cat from=$lcats}> 
    <
td>
        <
center><DIV><SPAN style="FONT-SIZE: 24pt; COLOR: #bcb947; FONT-FAMILY: 'Bernard MT Condensed'; language: en-US"><{$cat.name}></SPAN></DIV></center>
    </
td>
 </
tr>
<
tr>
<
td>
<
table class="item">
<
tr>
<{foreach 
item=prod key=count from=$allprod}>
      <
td style='margin:0px;padding:5px 0px;'>
          <
table><tr><td>
                <
DIV><SPAN style="FONT-WEIGHT: bold; FONT-SIZE: 14pt; COLOR: #bcb947; FONT-FAMILY: Constantia; language: en-US"><{$prod.p_name}></SPAN></DIV>
        
        <
br />
        <
DIV><SPAN style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; COLOR: #bcb947; FONT-FAMILY: Constantia; language: en-US"><{$prod.p_desc_long}></SPAN></DIV>
        </
td>
        <
td align="right"><{if $spec.special eq 1}>
        <
span style="text-decoration:line-through; color:#FF0000">$<{$prod.p_price}></span><br />$<{$prod.p_sp_price}>
        <{else}>
        <
DIV><SPAN style="FONT-WEIGHT: bold; FONT-SIZE: 12pt; COLOR: #bcb947; FONT-FAMILY: Constantia; language: en-US">$<{$prod.p_price}></SPAN></DIV>
        <{/if}>
        </
span>
        <
br />
        <
form method="post" action="add_to_cart.php?id=<{$prod.id}>">

           <
input type="text" name="qty" value="1" size="3" maxlength="4" title="quantity">
        <
br />
        <
input type="submit" value="AddToCart"
  
</
form>
        </
td>
        </
tr></table>
        
      </
td>
      <{if 
$count is div by $colnum}>
        </
tr>
        <
tr>
      <{/if}>
    <{/foreach}>
  </
tr>
</
table>
</
td><tr>
<{/foreach}>
<
br />
</
tr>
</
table>



For the most part this code does exactly what I want it to do however if I could get some help getting it to only list the products for the associated category you would be a life saver.

Thanks!

13
JS
Re: Basic shop module
  • 2010/2/16 17:20

  • JS

  • Just popping in

  • Posts: 87

  • Since: 2002/12/16


I had a thought and I was wondering if someone could give me a sanity check. I am not at home right now so I am unable to test this.

If I insert an "IF" statment after the second foreach statment would that give me the desired result?

Example: Also I am not sure if I have the if statment formatted correctly.

<{foreach item=prod key=count from=$allprod}> 

<{if 
$prod.catid==$cat.id}> 

      <
td style='margin:0px;padding:5px 0px;'
          <
table><tr><td
                <
DIV><SPAN style="FONT-WEIGHT: bold; FONT-SIZE: 14pt; COLOR: #bcb947; FONT-FAMILY: Constantia; language: en-US"><{$prod.p_name}></SPAN></DIV>


Thanks!

14
JS
Re: Basic shop module
  • 2010/2/17 19:08

  • JS

  • Just popping in

  • Posts: 87

  • Since: 2002/12/16


got it to work here is the completed code section for this



<table
<
tr

<{foreach 
item=cat from=$lcats}>  
    <
td
        <
center><DIV><SPAN style="FONT-SIZE: 24pt; COLOR: #bcb947; FONT-FAMILY: 'Bernard MT Condensed'; language: en-US"><{$cat.name}></SPAN></DIV></center
    </
td
 </
tr
<
tr
<
td
<
table class="item"
<
tr
<{foreach 
item=prod key=count from=$allprod}> 

 <{if 
$cat.id == $prod.catid}>

      <
td style='margin:0px;padding:5px 0px;'
          <
table><tr><td
                <
DIV><SPAN style="FONT-WEIGHT: bold; FONT-SIZE: 14pt; COLOR: #bcb947; FONT-FAMILY: Constantia; language: en-US"><{$prod.p_name}></SPAN></DIV
         
        <
br /> 
        <
DIV><SPAN style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; COLOR: #bcb947; FONT-FAMILY: Constantia; language: en-US"><{$prod.p_desc_long}></SPAN></DIV
        </
td
        <
td align="right"><{if $spec.special eq 1}> 
        <
span style="text-decoration:line-through; color:#FF0000">$<{$prod.p_price}></span><br />$<{$prod.p_sp_price}> 
        <{else}> 
        <
DIV><SPAN style="FONT-WEIGHT: bold; FONT-SIZE: 12pt; COLOR: #bcb947; FONT-FAMILY: Constantia; language: en-US">$<{$prod.p_price}></SPAN></DIV
        <{/if}> 
        </
span
        <
br /> 
        <
form method="post" action="add_to_cart.php?id=<{$prod.id}>"

           <
input type="text" name="qty" value="1" size="3" maxlength="4" title="quantity"
        <
br /> 
        <
input type="submit" value="AddToCart">  
   
</
form
        </
td
        </
tr></table
         
      </
td
      <{if 
$count is div by $colnum}> 
        </
tr
        <
tr
      <{/if}> 
<{/if}>
    <{/foreach}> 
  </
tr
</
table
</
td><tr
<{/foreach}> 
<
br /> 
</
tr
</
table>


thanks!

Login

Who's Online

177 user(s) are online (132 user(s) are browsing Support Forums)


Members: 0


Guests: 177


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: May 31
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits