一个smarty php文件中有两个mysql查询


two mysql query in one smarty php file?

首先,请忽略mysql函数,我正处于学习smarty的阶段,所以这只是我的测试,一旦我知道我在用smarty做什么,我会在后期切换到mysqli。

现在的问题是,

我试图在一个聪明的PHP文件中使用两个查询,但它根本不起作用。我说它不起作用的意思是,它只得到index.php页面中的第一个查询!无论我先把哪一个放在顶部,它都不适用于第二个查询!

这是我的index.php:代码

<?php
// These are the smarty files
require 'libs/Smarty.class.php';
// This is a file which abstracts the DB connecting functionality (Check out PEAR)
include "config/connect_to_mysql.php";
$smarty = new Smarty;
$smarty->compile_check = true;
$smarty->debugging = false;
$smarty->use_sub_dirs = false;
$smarty->caching = true;
// This SQL statement will get the 5 most recently added new items from the database
$storeShop = isSubdomain();

$sql = "SELECT DISTINCT category FROM $storeShop";
$result = mysql_query($sql) or die("Query failed : " . mysql_error());
// For each result that we got from the Database
while ($line = mysql_fetch_assoc($result))
{
 $cvalue[] = $line;
}
// Assign this array to smarty...
$smarty->assign('category', $cvalue);

// Assign this array to smarty...
$smarty->assign('$category', $cvalue);
// Display the news page through the news template
$smarty->display('index.tpl.html');
// Thanks to David C James for a code improvement :)
?>
<?php
// These are the smarty files
require 'libs/Smarty.class.php';
// This is a file which abstracts the DB connecting functionality (Check out PEAR)
include "config/connect_to_mysql.php";
$smarty = new Smarty;
$smarty->compile_check = true;
$smarty->debugging = false;
$smarty->use_sub_dirs = false;
$smarty->caching = true;
// This SQL statement will get the 5 most recently added new items from the database
$storeShop = isSubdomain();

$sql = 'SELECT * ';
$sql .= "FROM $storeShop ";
$sql .= 'ORDER BY `id` ';
$result = mysql_query($sql) or die("Query failed : " . mysql_error());
// For each result that we got from the Database
while ($line = mysql_fetch_assoc($result))
{
 $value[] = $line;
}
// Assign this array to smarty...
$smarty->assign('storeShop', $value);

// Assign this array to smarty...
$smarty->assign('$storeShop', $value);
// Display the news page through the news template
$smarty->display('index.tpl.html');
// Thanks to David C James for a code improvement :)
?>

我可以把第二个代码放在页面顶部,这个代码可以工作,但下面的代码停止工作!因此,似乎只有页面中的第一个查询才会触发

这是我的index.tpl.html代码:

    {section name=category loop=$category}
<li class="odd"><a href="#">{$category[category].category}</a></li>
{/section}
  {section name=storeShop loop=$storeShop}


  <div class='prod_box'>
    <div class='center_prod_box'>
      <div class='product_title'><a href='#'>{$storeShop[storeShop].product_name}</a></div>
      <div class='product_img'><a href='#'><img src='product_images/{$storeShop[storeShop].id}Image1.jpg' alt='' border='0' /></a></div>
      <div class='prod_price'><span class='reduce'><span>{$storeShop[storeShop].currency}</span>&nbsp;{$storeShop[storeShop].price}</span> <span class='price'><span>{$storeShop[storeShop].currency}</span>&nbsp;{$storeShop[storeShop].price}</span></div>
    </div>
    <div class='prod_details_tab'> <a href='#' class='prod_buy'>Add to Cart</a> <a href='#' class='prod_details'>Details</a> </div>
  </div>
  {/section}

有人能告诉我我做错了什么吗?

提前感谢

在渲染模板之前,您只需要在文件中显示第二个查询。

在您运行了第二个查询并分配了数组之后,您可以将两个数组绑定到模板并渲染它

使用此方法,您可以在渲染模板后几乎删除所有内容,因为它只是您已经完成的所有内容的副本。