Question

I have been trying for hours to assign mysql rows in smarty php with no luck.

I have this in product.php file:

require 'libs/Smarty.class.php';

$smarty = new Smarty;

$smarty->compile_check = true;
$smarty->debugging = false;
$smarty->use_sub_dirs = false;
$smarty->caching = false;

if (isset($_GET['id'])) {
    // Connect to the MySQL database  
    include "config/connect.php"; 
    $id = preg_replace('#[^0-9]#i', '', $_GET['id']); 
    // Use this var to check to see if this ID exists, if yes then get the product 
    // details, if no then exit this script and give message why
    $storeShop = isSubdomain();
    $sql = "SELECT * FROM $storeShop WHERE id='$id' LIMIT 1";
    $query = mysqli_query($db_conx, $sql);
    $productCount = mysqli_num_rows($query); // count the output amount
    if ($productCount > 0) {
        // get all the product details
            while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){ 

             $value[] = $line;
             $id = $row["id"];
             $product_name = $row["product_name"];
             $price = $row["price"];
             $shipping = $row["shipping"];
             $details = $row["details"];
             $category = $row["category"];
             $manu = $row["manu"];
         }

    }

}

$smarty->display('product.tpl');

and this is what I have in product.tpl file:

    {if isset($smarty.get.id)} 


      {$smarty.get.id.product_name)
<img src="images/{$smarty.get.id}Image1.jpg" alt="" border="0" width="120" height="100" />
{/if}

the only thing that I could get to display is the image but I cannot get rows. for example: $product_name.

Could someone please help me with this as it literally is giving me a massive headache.

Thank you in advance,

Was it helpful?

Solution

From the manual: http://www.smarty.net/crash_course

include('Smarty.class.php');

// create object
$smarty = new Smarty;

// assign some content. This would typically come from
// a database or other source, but we'll use static
// values for the purpose of this example.
$smarty->assign('name', 'george smith');
$smarty->assign('address', '45th & Harris');

// display it
$smarty->display('index.tpl');

You need to call $smarty->assign() for each variable you want to pass to the template.

--------- EDIT -----------
$smarty->assign() takes 2 parameters:
1. The name you'd like to use to access the value in the template file (a string)
2. The actual value you want to access

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top