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 (标签-STM32|关键词-智能小车)
  • ¥20 关于#stm32#的问题,请各位专家解答!
  • ¥15 (标签-python)
  • ¥15 第一个已完成,求第二个做法
  • ¥20 搭建awx,试了很多版本都有错
  • ¥15 java corba的客户端该如何指定使用本地某个固定IP去连接服务端?
  • ¥15 activiti工作流问题,求解答
  • ¥15 有人写过RPA后台管理系统么?
  • ¥15 Bioage计算生物学年龄
  • ¥20 如何将FPGA Alveo U50恢复原来出厂设置哇?