首先,各位大神,咱们就不纠结在应该不应该把php代码嵌入smarty模板中这类问题了,好不?因为我也是在别人代码上增加功能,只要能快速解决问题,啥办法都行。
问题描述:
在表格中,采用foreach的方式从$items读取每个记录,并且在表格中每行显示若干关键词相应的内容。$items是来自于后端php代码处理的结果。现在我需要在显示某个关键词内容时加个判断,读取相应内容,并且做个小链接。
代码如下:
{foreach $items as $item} {strip}。。。。。。。。。。。。。。。。。。。。。。。。。。。。{if $item->kind == "folder"}{elseif $item->type != "link"} id}">{$item->name}
{else} {$link_content=""} {$this_id=$item->id} {$search_link_query="select * from archive.file where id='$this_id'"} {$con = mysqli_connect("localhost","root","root")} {$result = mysqli_query($con,$search_link_query)} {mysqli_close($con)} {$row=mysqli_fetch_row($result)} {$link_content=$row[8]} id}">{$item->name}
{/if}................................................... {/strip} {/foreach} {$item->name}
奇怪的问题发生了,网页功能显示的完全正确,但在表格上方出现若干“1”。我一句一句删除代码并检测,发现{$con = mysqli_connect("localhost","root","root")}产生了“1”。只要删除这句话,就没有1产生、出现出错信息;如果不删除这句话而删除后面的代码,则有1,且后面出现出错信息。
大牛们,这个是咋回事情?我该如何修改呢?
其实我很想把这部分代码放到后端文件中去,但这里一个关键变量$item->id是在循环中读取的,我后面没有办法多次读取、并搜索该条记录中的关键词content中的值,且返回到tpl文件中去显示出来。
该如何处理呢?请大牛们赐教啊。
回复讨论(解决方案)
这个可以写在PHP里面,然后用用标签替换掉啊,在模板里面写什么连接数据库呢?
还是改你php吧 吧要添加的内容循环添加进去 页面直接调用就行了
感谢关注啊,我第一次用smarty,还是在别人代码基础上增加新功能。
不知道楼上所说的用标签替换掉,怎么替换掉?我也考虑过采用如下方式传送变量:
$smarty->assign("action", $action);
$smarty->display("folders.tpl");
在我看来,上面的方面只能是一次性的传送。但在我的表格中每条记录是不停的循环并且显示的,如果我要把前端每次循环的关键变量id传送到后端进行处理,那又应该如何做呢?
那问题就归结为: smarty模板中前端采用一个循环来显示内容,那我如何把每次循环中的关键变量传到后端,经过处理后又把结果返回该循环中呢?
其实,想想逻辑上也不难,大神们,有代码例子么?这里叩谢了。
吧你的后台代码贴出来 标识好 我看看
其实,我就非常纳闷了,咋就出现这么多“1”呢?
与该smarty相对应的后端php 代码为:(我已经尽量加注释了)
<?phpload_module("folders");load_module("files");//该页面的基本功能就是在表格中显示某个区域里的文件或者文件夹,我要增加一个链接link,并且其他一些链接等功能$id = getParamStr("id"); //此处id应该是这表格所属区域的Id,与我的问题相联系的数据库中储存的文件或链接id不一样,这个是我的理解,我也是刚看这代码if ($action == "search") //对这个页面有两个动作,一个是search{ $search = postParamStr("search"); $folders = do_action("folders","search", array($search, $currentUser->allowedRegion)); $files = do_action("files","search", array($search, $currentUser->allowedRegion)); $smarty->assign("folder", NULL); $paths = array("Search Result"); $path = new stdClass(); $path->id = NULL; $path->name = "Search Result"; $smarty->assign("paths", array($path));}else //对这个页面的另一动作是正常显示{ $folders = list_items("folders", $id); $folder = get_item("folders", $id); $files = list_items("files", $id); $smarty->assign("folder",$folder); $paths = $folder->path; if ($currentUser->level == 2) { unset($paths[0]); $paths = array_values($paths); } $smarty->assign("paths", $paths);}$items = array_merge($folders,$files); //获取到显示记录,并把多个记录组合起来$items = sort_items($items); //排序功能$smarty->assign("items", $items);$smarty->assign("action", $action);$smarty->display("folders.tpl");?>
前端的代码太多,我还是截取循环的那部分,供参考:
{if count($items) > 0} {foreach $items as $item} {strip}{if $item->kind == "folder"} {/strip} {/foreach} {else}{else} {if $item->type == "docx" || $item->type == "doc"} {else if $item->type == "xlsx" || $item->type == "xls"} {elseif $item->type == "pdf"} {elseif $item->type == "png" || $item->type == "gif" || $item->type == "jpg"} {elseif $item->type == "ppt" || $item->type == "pptx"} {elseif $item->type == "mp4"} {elseif $item->type == "link"} {else} {/if} {/if} {if $item->kind == "folder"} {elseif $item->type != "link"} id}">{$item->name}
{else} {$link_content=""} {$this_id=$item->id} {$search_link_query="select * from archive.file where id='$this_id'"} {$con = mysqli_connect("localhost","root","goldmine")} {$result = mysqli_query($con,$search_link_query)} {mysqli_close($con)} {$row=mysqli_fetch_row($result)} {$link_content=$row[8]} id}">{$item->name}
{/if} {$item->name}
{$item->createdByName}
{$item->created}
{$item->lastUpdated}
Visible
{/if} {if $item->kind == "folder"} {if $item->size > 1000} {round(($item->size / 1024), 1)}MB {else} {$item->size}KB
{elseif $item->kind == "file"} id}/Move')">
{/if} {if $item->kind == "folder"} id}/Move')">
{elseif $item->kind == "file"} id}">
{/if} id}">
{else} onclick="JavaScript:return confirm('Do you really want to delete this folder?');"> {/if} {elseif $item->kind == "file"} id}" {if $item->kind == "folder"} {if $item->folderCount > 0 || $item->fileCount > 0} onclick="JavaScript:return confirm('This folder is not empty. If you delete it all content will also be deleted. Do you still want to continue?');">
{/if} id}" onclick="JavaScript:return confirm('Do you really want to delete this file?');">
{if $action != "search"} {/if} {if $action == "search"}
No files or folders could be found!
{else}This folder is empty!
{/if}其实如果不管代码的话,我的问题归结下来就两个细节:
1. 在smarty模板中,怎么{$con = mysqli_connect("localhost","root","root")}就会产生这么多的“1”呢?表明连接数据库成功么?有无方法隐藏这个显示?
2.smarty模板中前端的循环,如果与后端php代码处理动态的进行数值传输呢?
我需要把每次循环中的关键变量传到后端,经过处理后又把结果返回该循环中呢,并且在该次循环中显示php传送过来的值。如果做到呢?
大神们,你们有办法解决么?希望给出代码示例,否则小弟不会做啊。谢谢啊。奶奶的,我的语文是体育老师教的。
更正啊:在php代码部分,第一条注释为:
//该页面的基本功能就是在表格中显示某个区域里的文件或者文件夹,除文件和文件夹类型外我要增加一个类型:链接link,并且还增加其他一些与link链接这种类型相关的功能模板中不要写逻辑代码,尽量都写到php中去。
2.smarty模板中前端的循环,如果与后端php代码处理动态的进行数值传输呢?
我需要把每次循环中的关键变量传到后端,经过处理后又把结果返回该循环中呢,并且在该次循环中显示php传送过来的值。如果做到呢? 交互不外乎两种,form提交到后端,或者ajax传值。更正:
多次测试后,发现是{mysqli_close($con)}这句话产生了“1”,其结果为如过是{mysqli_close($con)}这句话产生了“1”
那就把 {mysqli_close($con)} 删掉xu版主来了,欢迎啊。
我就是这么干的,确实没有“1”了。但这么干,不会产生什么坏影响吧?比如,与数据库的链接占用内存?对系统还产生其他影响?
但细想想,应该没有多大事情,因为系统都采用$con这个表示链接,最多也就一个变量占用内存而得不到释放?徐版主,这样删掉没有大问题吧?
如过是{mysqli_close($con)}这句话产生了“1”
那就把 {mysqli_close($con)} 删掉这是没有问题的
那好,徐版主说没有问题了,那大概八九不离十了。
结贴。
这是没有问题的本文是由用户编写整理,所有内容的版权归原作者所有。如果侵犯了您的权益,请联系我删除