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

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

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)