您好,欢迎访问一九零五行业门户网

smarty模板中嵌入php代码,出现啼笑皆非的错误,请大神帮助!

首先,各位大神,咱们就不纠结在应该不应该把php代码嵌入smarty模板中这类问题了,好不?因为我也是在别人代码上增加功能,只要能快速解决问题,啥办法都行。
问题描述:
在表格中,采用foreach的方式从$items读取每个记录,并且在表格中每行显示若干关键词相应的内容。$items是来自于后端php代码处理的结果。现在我需要在显示某个关键词内容时加个判断,读取相应内容,并且做个小链接。
代码如下:
{foreach $items as $item} {strip}。。。。。。。。。。。。。。。。。。。。。。。。。。。。{if $item->kind == folder} id}>{$item->name}
{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]} {$item->name}
{/if}................................................... {/strip} {/foreach}
奇怪的问题发生了,网页功能显示的完全正确,但在表格上方出现若干“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 代码为:(我已经尽量加注释了)
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} id}> {if $item->kind == folder}
{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} id}>{$item->name}
{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]} {$item->name}
{/if} {$item->createdbyname}
{$item->created}
{$item->lastupdated}
visible
{if $item->size > 1000} {round(($item->size / 1024), 1)}mb {else} {$item->size}kb
{/if}
{if $item->kind == folder} id}/move')>
{elseif $item->kind == file} id}/move')>
{/if} {if $item->kind == folder} id}>
{elseif $item->kind == file} id}>
{/if} 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?');>
{else} onclick=javascript:return confirm('do you really want to delete this folder?');>
{/if} {elseif $item->kind == file} id} onclick=javascript:return confirm('do you really want to delete this file?');>
{/if}
{/strip} {/foreach} {else} {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)} 删掉
这是没有问题的
那好,徐版主说没有问题了,那大概八九不离十了。
结贴。
这是没有问题的
其它类似信息

推荐信息