如何在smarty php中分配mysql行?

时间:2014-04-29 21:22:06

标签: php mysql smarty

我一直在尝试assign mysql rows in smarty php几个小时而没有运气。

我在product.php文件中有这个:

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');

这就是我在product.tpl文件中的内容:

    {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}

我唯一可以展示的是图片,但我无法获得rows。例如:$product_name

有人可以帮我解决这个问题,因为它让我头疼不已。

提前谢谢你,

1 个答案:

答案 0 :(得分:1)

从手册中: 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');

您需要为要传递给模板的每个变量调用$smarty->assign()

---------编辑-----------
$smarty->assign()需要2个参数:
1.您要用来访问模板文件中的值的名称(字符串)
2.您要访问的实际值