Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Question with foreach

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
kraigballa
Smarty Rookie


Joined: 04 Jun 2010
Posts: 8

PostPosted: Fri Jun 04, 2010 3:35 pm    Post subject: Question with foreach Reply with quote

I have a products table that has a field called sizing. The sizing field contains an ID from another table called products_sizing. The main reason for the sizing field in the products table is to decipher which products have what kind of sizing method. For instance, some products (shirts) only are available in small and medium not large, but others have the option for all three. In the products table the sizing field shows which sizing method a product will have if any.

Ok here is what I have come up with. The only problem with this code is that if I have more than one product in the cart the sizing methods for each product will be the last product to go through the loop. For instance, the first and second product will have the same sizing methods as the third even though the first and second product don't have the same sizing methods. Any ideas on how to fix this?

Code:
{if $ProductRow.sizing != ''}   [color=red]this is used to pick out the products that have any sizing methods. Works fine.[/color]
           
          {foreach from=$ProdsCarts key=ProductID item=ProductRow name=ProductsLoop}   [color=red]this will loop through the products that are in the users cart. Works fine.[/color]
                 
         
          <tr class="head">
            <td height="15" colspan="3" style="border:none;"><div align="justify">{#Sizing_Method#} ({$ProductRow.name})</div></td>
          </tr>
        <tr>
           <td colspan="2">
           <div align="left">



         {if is_array($ProductSizing)}
         {foreach from=$ProductSizing key=key item=isOn} [color=red]this grabs the sizing methods for the products. Not working right!!![/color]
         <input name="sizingmethods" type="radio"  id="sizingmethods{$SizingID}" value="{$Sizing.$key.sizingid}"/>
            <label for="sizingmethods{$SizingID}"> {$Sizing.$key.name} | {if $Sizing.$key.value ne 0} +{$Sizing.$key.value} {if $Sizing.$key.typename eq 'absolute'}{#CURRENCY#}{else}%{/if}{else} {#free#} {/if}</label><br />
         {/foreach}
         {/if}
         

            </div></td>
          </tr>
         
          {/foreach}
         
          {else}
         
          {/if}


Thanks for your help!
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Fri Jun 04, 2010 6:30 pm    Post subject: Reply with quote

when you assign the products to an array, assign a sizing array to each product. Then you can independently loop the correct sizes for each product within the product loop.
Back to top
View user's profile Send private message Visit poster's website
kraigballa
Smarty Rookie


Joined: 04 Jun 2010
Posts: 8

PostPosted: Sun Jun 06, 2010 4:40 am    Post subject: Reply with quote

mohrt wrote:
when you assign the products to an array, assign a sizing array to each product. Then you can independently loop the correct sizes for each product within the product loop.


Isn't that what this is...?

Code:
{foreach from=$ProductSizing key=key item=isOn}


The first foreach assigns the products to an array. Then the second assigns the sizing method to each product...or at least that what it looks like to me. What are you thinking about?
Back to top
View user's profile Send private message
kraigballa
Smarty Rookie


Joined: 04 Jun 2010
Posts: 8

PostPosted: Thu Jun 10, 2010 9:56 pm    Post subject: Reply with quote

This is the php page that assigns all of the variables:

Code:
$SizingMethods = $Sizing->getAllSizing();
$smarty->assign('SizingMethods',$SizingMethods);


$ProductInfo = $Products->getProduct($ProdID,TRUE);
$ProductRow = $Products->getProduct($ProdID,TRUE);

$ProductInfo = $Products->getProduct($ProdID,TRUE);
$ProductSizing = $Product->extractSizing($ProductInfo['sizing']);
$AllSizing = $Sizing->getAllSizing();
$BasePrice = $Product->getBasePrice($AllSizing,$ProductSizing,$ProductInfo['sizing']);
$smarty->assign('Product', $ProductInfo);
$smarty->assign('Sizing', $AllSizing);
$smarty->assign('ProductSizing', $ProductSizing);
$smarty->assign('BasePrice', $BasePrice);


And then here is where the classes are created:

Code:
class Sizing
{
   var $AllSizing;
   function __construct(){}
   function getAllSizing($all = false)
   {
      global $DB;
      if ($all) $sql = "SELECT * FROM `products_sizing`";
      else $sql  = "SELECT * FROM `products_sizing` WHERE `hidden` != 'on'";
      $DB->Query($sql,'getAllSizing');
      while ($row = $DB->Fetch('getAllSizing')){
         if ($row['type'] == 'percent') $typeCode = '%';
         else $typeCode = '$';
         $this->AllSizing[$row['sizingid']] = array('name' => $row['sizingname'], 'value' => $row['value'], 'typename' => $row['type'],  'typecode' => $typeCode, 'hidden' => $row['hidden'], 'freesize' => $row['freesize']);
      }
      return $this->AllSizing;
   }
   function getSize($sizingid = null){
      global $DB;
      $sql = "SELECT * FROM `products_sizing` WHERE `sizingid` = '$sizingid' ";
      $DB->Query($sql,'getSize');
      return $DB->Fetch('getSize');
   }
   function newSize(){
      global $DB;
      $sizeName = $_POST['sizeName'];
      $sizeValue = $_POST['sizeValue'];
      $sizeType = $_POST['sizeType'];
      $sql = "INSERT INTO `products_sizing` (`sizingid`,`sizingname`,`value`,`type`) VALUES (NULL,'$sizeName','$sizeValue','$sizeType')";
      $DB->Query($sql,'newSize');
      return $DB->LastInsertID();
   }
   function updateSize($SizeID){
      global $DB;
      $sizeName = $_POST['sizeName'];
      $sizeValue = $_POST['sizeValue'];
      $sizeType = $_POST['sizeType'];
      if (isset($_POST['minOrderPrice'])) $minOrderPrice = $_POST['minOrderPrice']; else $minOrderPrice = '';
      $sql = "UPDATE `products_sizing` SET `sizingname`='$sizeName',`value`='$sizeValue',`type`='$sizeType',`freesize`='$minOrderPrice' WHERE (`sizingid`='$SizeID')";
      $DB->Query($sql,'updateSize');
   }
   function inactivateSize($SizeID){
      global $DB;
      $sql = "UPDATE `products_sizing` SET `value`='0',`type`='absolute' ,`hidden`='on' WHERE (`sizingid`='$SizeID')";
      $DB->Query($sql,'inactivateSize');
   }
   function activateSize($SizeID){
      global $DB;
      $sql = "UPDATE `products_sizing` SET `hidden`='' WHERE (`sizingid`='$SizeID')";
      $DB->Query($sql,'activateSize');
   }
   function isFreeSize($sizeID,$orderprice){
      $sizerow = $this->getSize($sizeID);
      if ($sizerow['freesize'] >= 1 && $orderprice > $sizerow['freesize']) return true;
      else false;
   }

}
$Sizing = new Sizing();
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Thu Jun 10, 2010 10:03 pm    Post subject: Reply with quote

dump your data in the template, paste the output here.

{$Product|@print_r}
{$Sizing|@print_r}
{$ProductSizing|@print_r}
Back to top
View user's profile Send private message Visit poster's website
kraigballa
Smarty Rookie


Joined: 04 Jun 2010
Posts: 8

PostPosted: Thu Jun 10, 2010 10:47 pm    Post subject: Reply with quote

I put two products with different sizing methods in the cart, and this is the output.

{$Product|@print_r}:
Code:
Array (
[productid] => 30
[categoryid] => 16
[productcode] => DG-HT-MS-RB
[producturl] => dg-mesh-cap-royal-blue
[name] => DG Mesh Cap Royal Blue
[price] => 0.00
[listprice] => 0.00
[listpriceperiod] => 0000-00-00
[stock] => instock
[stockcount] => 0
[weight] => 0
[featured] =>
[available] =>
[new] =>
[taxes] =>
[payment] => 1
[sizing] => 6,7
[freesizing] =>
[shipping] => 3,4
[freeshipping] =>
[details] => DG Mesh Cap Royal Blue
[manufacturer] => 10
[image0] =>
[image1] =>
[image2] =>
[image3] =>
[image4] =>
[file] =>
[forumid] => 0
[wholesale] =>
[options] => 0 [forumCatID] => [forumPostID] => [privateinfo] => [guaranty] => [webpage] => [date] => 2010-05-27 [usefullacc] => [relatedproducts] => [freeacc] => [OptCats] => [Options] => ) 1



{$Sizing|@print_r}:
Code:
Array (
[4] => Array (
   [name] => Large
   [value] => 1
   [typename] => absolute
   [typecode] => $
   [hidden] =>
   [freesize] => )
   [5] => Array (
       [name] => Medium
       [value] => 2
       [typename] => absolute
       [typecode] => $
       [hidden] =>
       [freesize] => )
   [6] => Array (
       [name] => Small
       [value] => 0
       [typename] => absolute
       [typecode] => $
       [hidden] =>
       [freesize] => )
   [7] => Array (
       [name] => Flex Fit
       [value] => 0
       [typename] => absolute
       [typecode] => $
       [hidden] =>
       [freesize] => ) ) 1



{$ProductSizing|@print_r}:
Code:
Array ( [6] => on [7] => on ) 1
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Thu Jun 10, 2010 11:49 pm    Post subject: Reply with quote

One problem, $Product.sizing is not an array, it is a comma separated list of values. You want to iterate over this, so you want to make that an array. Explode it in PHP before you assign.

If $ProductSizing is set per product, you can do this:

Code:
{foreach from=$ProductSizing item="size"}
    {$Sizing.$size.name}
    {$Sizing.$size.value}
    {$Sizing.$size.typename}
    {$Sizing.$size.typecode}
    {$Sizing.$size.hidden}
    {$Sizing.$size.freesize}
{/foreach}
Back to top
View user's profile Send private message Visit poster's website
kraigballa
Smarty Rookie


Joined: 04 Jun 2010
Posts: 8

PostPosted: Fri Jun 11, 2010 2:10 am    Post subject: Reply with quote

Alright I am not seeing $Products.sizing anywhere.

Also I tried to use what you put and both products are still getting the sizing methods for the last one to go through the loop.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Fri Jun 11, 2010 2:20 am    Post subject: Reply with quote

ok so $ProductSizing has the sizes from the last item. You really need to work on the code that assigns items to Smarty. assign your products in an array, and each product should contain an array of sizing information. Either all the information for each size, or an id for each size that can be used to access a sizing array which contains all possible sizes.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP