Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Smarty在tpl中调用php查询_Php_Mysql_Smarty - Fatal编程技术网

使用Smarty在tpl中调用php查询

使用Smarty在tpl中调用php查询,php,mysql,smarty,Php,Mysql,Smarty,Im n00b,因此基本上我想使用smarty从.tpl文件调用位于index.php文件中的查询: Index.php 我在index.php中使用的while循环是我想在我的.tpl文件中调用的,我是smarty的新手,无法让它工作,测试数据库连接,它工作了,我的smarty被称为无错误 这是一个基本的静态页面,我只是在用Smarty做实验,所以我只想把查询显示为list no td之类的 那么,如果我想显示查询,有人能给我举个例子,说明我的.tpl文件在我的“视图”目录中的位置吗 提前感谢

Im n00b,因此基本上我想使用smarty从.tpl文件调用位于index.php文件中的查询:

Index.php

我在index.php中使用的while循环是我想在我的.tpl文件中调用的,我是smarty的新手,无法让它工作,测试数据库连接,它工作了,我的smarty被称为无错误

这是一个基本的静态页面,我只是在用Smarty做实验,所以我只想把查询显示为list no td之类的

那么,如果我想显示查询,有人能给我举个例子,说明我的.tpl文件在我的“视图”目录中的位置吗


提前感谢

一个简短的示例,介绍如何将数组中的一些单词显示为标签中的选项:

index.php

index.tpl

//头部、css等在这里并不重要

<p>
  {foreach from=$rows item="item"}
    {$item}<br>
  {/foreach}
</p>

作为变量的内容,您几乎可以传递任何您想要的信息。

这就是我所做的

在我的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


就像我说的,我完全不知道这件事,但是我把它展示出来了。感谢@Tobias.F

在数组上循环并访问其中的项,您可以使用foreach:{foreach from=$row item=item}{$item}{/foreach}。这将输出$row中的所有内容,这是一个数组。此外,您的代码现在不起作用,存在错误,例如,在while中设置字符串,但没有将其分配给变量。很好,因此我可以请您给我一个将例如“product_category”分配给变量的示例。这会发生在我所有代码顶部的index.php文件中吗?我先声明变量,然后声明查询?while循环调用变量时会是什么样子?谢谢你的while循环没有任何意义。您正在创建字符串,但未将其分配给任何变量。您必须读取所有数据,将其分配给数组变量,然后将该变量传递给smartythanks allot!!这正是我要找的。没问题,很高兴我能帮助你。对不起,有一件事我忘了问,所以在我的数据库中有“product\u category”和“product\u price”表。。。。。。如何将它们分配给变量以在while循环中调用它们。希望它有意义?如果你查看我上面的代码,你可以看到我通过从cs_shop中选择all调用查询,然后$row调用查询..我有点困惑。你说的是列,而不是表。请记住以下几点:使用mysqli_fetch_数组循环遍历结果这意味着,如果让我们说一下表中的列id和值,while循环中的$row看起来像[['id']=>23,['value']=>anime']。因此,当您在smarty的foreach中时,您将获得$item。然后你可以访问$item['id']之类的值。我明白你的意思,是的,对不起,我是指列,再次感谢你教训我哈哈,但我的语法不正确,你介意告诉我这是什么样子吗?现在我知道我在突破极限,但这让我感到。。。比如,我不确定是否应该为每列声明一个变量,然后在循环中调用该变量。。然后调用循环中为.tpl分配的变量。不指定数据库列也可以,因为您可以创建自己的自定义变量,但我会将我的变量指定给右边的列,我认为这样做是错误的。在$item['product_category']周围添加括号{}。我现在这样做了,没有显示任何结果,甚至没有错误。我分配列并在循环中调用它们的方式正确吗?我的错…似乎我犯了一个错误,请尝试以{$value.product_catagory}的形式访问这些值。
<p>
  {foreach from=$rows item="item"}
    {$item}<br>
  {/foreach}
</p>
<p>
  hello<br>
  there<br>
  this<br>
  is<br>
  me<br>
</p>
hello
there
this
is
me
<?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');
 <p>
  {foreach from=$row item="item"} 

     {$item['product_category'] }<br />

  {/foreach}
 </p>