[英]php query called in tpl using Smarty
Im n00b, so basically i want to call my query which is located in my index.php file from my .tpl file using smarty: 我是n00b,所以基本上我想使用smarty从.tpl文件中调用位于index.php文件中的查询:
Index.php Index.php
<?php
//Database connection
$db = mysqli_connect('xx','xx','','xx')
or die('Error connecting to MySQL server.');
//access Smarty template engine
require_once('Smarty-3.1.30/libs/Smarty.class.php');
$smarty = new Smarty();
$smarty->template_dir = 'views';
$smarty->compile_dir = 'tmp';
//query product page
$query = "SELECT * FROM cs_shop";
mysqli_query($db, $query) or die('Error querying database.');
$result = mysqli_query($db, $query);
$row = mysqli_fetch_array($result);
//query
while ($row = mysqli_fetch_array($result)) {
$row['product_category'] . ' ' . $row['product_price'] . ': ' .
$row['product_quantity'] . ' ' . $row['product_about'] .' '
.$row['product_color'] .'<br />';
}
//db collect data
$smarty->assign('row', $row);
//template
$smarty->display('index.tpl');
mysqli_close($db);
?>
The while loop i use in the index.php is what i want to call in my .tpl file, im new to smarty and cant get it to work , test database connection and it worked, my , Smarty gets called no errors. 我在index.php中使用的while循环是我想在我的.tpl文件中调用的内容,它是smarty的新手,无法正常工作,无法测试数据库连接,但我的Smarty不会出错。
Its a basic static page im just doing experiment, using Smarty, So i just want to display the query as list no td's or anything like that. 它是一个基本的静态页面,我正在使用Smarty做实验,因此我只想将查询显示为列表,而不是td或类似的内容。
So can someone give me a example how my .tpl file would look located in my 'views' directory if i want to display the query? 因此,如果我想显示查询,有人可以给我一个例子,我的.tpl文件在我的“视图”目录中看起来如何?
Thanks in advance 提前致谢
A short example how to display a few words from an array as options within a <p>
tag: 一个简短的示例,如何显示数组中的几个单词作为
<p>
标记内的选项:
index.php index.php
$rows = ['hello', 'there', 'this', 'is', 'me'];
$smarty->assign('rows', $rows);
$smarty->display('index.tpl');
index.tpl index.tpl
// head, css etc, doesn't matter here // head,css等,在这里都没关系
<p>
{foreach from=$rows item="item"}
{$item}<br>
{/foreach}
</p>
This will produce some code which evaluates to: 这将产生一些代码,其结果为:
<p>
hello<br>
there<br>
this<br>
is<br>
me<br>
</p>
And as interpreted HTML: 并解释为HTML:
hello
there
this
is
me
As the content of the variables you can pass (almost) whatever you want. 作为变量的内容,您可以(几乎)传递任何所需的内容。
Got this to work, here is what i did. 得到这个工作,这就是我所做的。
In my index.php (showing only what i know is not-correct and correct now) 在我的index.php中 (仅显示我所知道的不正确并且现在正确)
<?php
$new = ['product_category','product_price','product_quantity','product_about','product_color'];
//added an array - rows
$rows = array();
//while loop calling array adding products columns to it
while ($row = mysqli_fetch_array($result)) {
$rows[] = array(
'product_category' => $row['product_category'],
'product_price' => $row['product_price'],
'product_quantity' => $row['product_quantity'],
'product_about' => $row['product_about'],
'product_color' => $row['product_color']
);
}
//db collect data - calls rows
$smarty->assign('row', $rows);
//template
$smarty->display('index.tpl');
index.tpl index.tpl
<p>
{foreach from=$row item="item"}
{$item['product_category'] }<br />
{/foreach}
</p>
like i said im totally noob in this, but got it to display. 就像我说的那样,我完全是菜鸟,但是却要展示它。 Thanks @Tobias .F
谢谢@Tobias .F
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.