Jump to content

Recommended Posts

I am trying to create a dynamic drop down box of possible colours for a product, the query returns the colours in a string ie Green,Blue,Red...

 

I know that normally you would use the explode function, well i imagine you still do. But I want the text box to appear in the following tpl file:

 

{load_product assign="product"} 
<br/><br/>
<span class="ListDescription">{$product->mProduct.name}</span><br><br>
<table>      
  <tr>       
    <td width="250">
       <img src='product_images/{$product->mProduct.image_file_1}' 
            height="150" border="0"></a>
    </td>    
    <td>     
       <img src='product_images/{$product->mProduct.image_file_2}' 
            height="150" border="0"></a>
    </td>    
  </tr>                                           
</table>                                          
<br/>                                             
<span class="ProductDescription">{$product->mProduct.description}<br/><br/>
Fabric: </span>
<span class ="ProductFabric">{$product->mProduct.fabric}<br/><br/>
Weight: </span>
<span class ="ProductWeight">{$product->mProduct.weight}<br/><br/>
Price:  </span>                                   
<span class="ProductPrice">£{$product->mProduct.price}<br/><br/>
Sizes: </span>
<span class="ProductSize">{$product->mProduct.sizes}<br/><br/>
Colours: </span>
<span class="ProductColour">{$product->mProduct.colours}<br/><br/>
</span>
                                        
<a class="Link" href="{$product->mPageLink}">Continue Shopping</a>

 

 

But their is also a seperate function which retrives the product details, i imagine this is where you would create the array of colours:

 

<?php                                             
// plugin function for the load_product function plugin
function smarty_function_load_product($params, $smarty)
{                                                 
  $product = new Product();                       
  $product->init();                               
  // assign template variable                     
  $smarty->assign($params['assign'], $product);   
}                                                 
// class that handles product details             
class Product                                     
{                                                 
  // public variables to be used in Smarty template 
  public $mProduct;
  public $mPageLink = "index.php";
  // private stuff 
  private $mBoCatalog;
  private $mProductId;
  // class constructor
  function __construct()
  {          
    // create the middle tier object
    $this->mBoCatalog = new BoCatalog();
    // variable initialization
    if (isset($_GET['ProductID']))
       $this->mProductId = (int)$_GET['ProductID'];
    else     
       trigger_error("ProductID required in product.php");
  }          
  // init    
  function init()
  {          
    // get product details from business tier 
    $this->mProduct = 
       $this->mBoCatalog->GetProductDetails($this->mProductId);
    if (isset($_SESSION['PageLink'])) 
       $this->mPageLink = $_SESSION['PageLink'];
  }          
} //end class
?>           

 

I have had a few attempts at this and not had much sucess so any ideas anyone has on this issue would be much appreicated.

 

Andy

Heres is some example code for turning a comma delimited list in to a pull down menu

$color_str = 'red,blue,green';

$colors = explode(',', $color_str);

echo '<select name="color">';

foreach($colors as $color)
{
    echo "\n  <option value=\"$color\">" . ucwords($color) . '</option>';
}

echo "\n</select>";

How is that variable created, Sorry I have not used smarty so I don't know the language.

 

Edit. Those smarty variables must be set from this line:

$smarty->assign($params['assign'], $product);

 

$params['assign'] must hold an array of variables to be set in smarty.

So would i have to create the array from the string in the template? Something like:

 

{load_product assign="product"} 
<br/><br/>
[b]<?php
$product_colours={$product->mProduct.colours};
$colours_array = explode(',', $product_colours);
?>[/b]
<span class="ListDescription">{$product->mProduct.name}</span><br><br>
<table>      
  <tr>       
    <td width="250">
       <img src='product_images/{$product->mProduct.image_file_1}' 
            height="150" border="0"></a>
    </td>    
    <td>     
       <img src='product_images/{$product->mProduct.image_file_2}' 
            height="150" border="0"></a>
    </td>    
  </tr>                                           
</table>                                          
<br/>                                             
<span class="ProductDescription">{$product->mProduct.description}<br/><br/>
Fabric: </span>
<span class ="ProductFabric">{$product->mProduct.fabric}<br/><br/>
Weight: </span>
<span class ="ProductWeight">{$product->mProduct.weight}<br/><br/>
Price:  </span>                                   
<span class="ProductPrice">£{$product->mProduct.price}<br/><br/>
Sizes: </span>
<span class="ProductSize">{$product->mProduct.sizes}<br/><br/>
Colours: </span>
<span class="ProductColour">{$product->mProduct.colours}<br/><br/>
</span>
                                        
<a class="Link" href="{$product->mPageLink}">Continue Shopping</a>

After messing about a bit I reckon the explode statement needs to go in the function; function.load_product.php

 

At present I am just trying to create a variable that contains only the colours so i can explode it inot an array. I have tried the following but I get "Undefined variable: product."

 

<?php                                             
// plugin function for the load_product function plugin
function smarty_function_load_product($params, $smarty)
{                                                 
  $product = new Product();                       
  $product->init();
  $product->getColours();
  // assign template variable                     
  $smarty->assign($params['assign'], $product);   
}                                                 
// class that handles product details             
class Product                                     
{                                                 
  // public variables to be used in Smarty template 
  public $mProduct;
  public $mcolours;
  public $string1 = "hello";
  public $mPageLink = "index.php";
  // private stuff 
  private $mBoCatalog;
  private $mProductId;
  // class constructor
  function __construct()
  {          
    // create the middle tier object
    $this->mBoCatalog = new BoCatalog();
    // variable initialization
    if (isset($_GET['ProductID']))
       $this->mProductId = (int)$_GET['ProductID'];       
    else     
       trigger_error("ProductID required in product.php");
  }          
  // init    
  function init()
  {          
    // get product details from business tier 
    $this->mProduct = 
       $this->mBoCatalog->GetProductDetails($this->mProductId);
    if (isset($_SESSION['PageLink'])) 
       $this->mPageLink = $_SESSION['PageLink'];
  }
  function getColours()
{
//get colours
$this->mcolours=
$this->$product->mProduct.colours;

}
} //end class
?>           

 

If anyone has anyone has any ideas I would be most grateful.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.