一个smarty php文件中的两个mysql查询?

一个smarty php文件中的两个mysql查询?,php,mysql,smarty,Php,Mysql,Smarty,首先,请忽略mysql函数,我正处于学习smarty的阶段,因此这只是为了我的测试,我将在稍后的阶段切换到mysqli,一旦我知道我在使用smarty做什么 现在的问题是 我试图在一个smarty PHP文件中使用两个查询,但它根本不起作用。我的意思是它不起作用,因为它只获取index.php页面中的第一个查询!不管我先把哪个放在顶部,第二个查询都不起作用 以下是我的index.php代码: <?php // These are the smarty files require 'libs

首先,请忽略mysql函数,我正处于学习smarty的阶段,因此这只是为了我的测试,我将在稍后的阶段切换到mysqli,一旦我知道我在使用smarty做什么

现在的问题是

我试图在一个smarty 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 :)
?>

在呈现模板之前,只需在文件中启动第二个查询

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


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

Smarty完全没有问题。这是您试图编写代码的方式的问题。一旦smarty被呈现,页面很可能会死掉,因此没有控件从那里继续移动。如果是这样的话,您将收到关于两次尝试要求并包含相同文件的错误。@lagbox,您能更详细地说明我的编码方式吗?到底是怎么回事?做一个查询。。。获取您的值数组。。。执行另一个查询以获取另一个值数组。。。将两个数组绑定到模板并渲染it@lagbox,在伴侣身上。。。这很有魅力。你可以发布与答案相同的东西,我会接受的。谢谢你。。。祝你好运
    {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}