dongyu5104 2014-04-29 21:22
浏览 39
已采纳

如何在smarty php中分配mysql行?

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,

  • 写回答

1条回答 默认 最新

  • dtd793353 2014-04-29 21:26
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?