两个mysql查询在一个聪明的PHP文件?

时间:2014-04-29 11:22:20

标签: php mysql smarty

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

现在问题,

我试图在一个聪明的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}

有人可以让我知道我做错了吗?

提前致谢

1 个答案:

答案 0 :(得分:1)

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

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

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