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

歪着看世界 2014-03-03 05:21:18
首先,各位大神,咱们就不纠结在应该不应该把php代码嵌入smarty模板中这类问题了,好不?因为我也是在别人代码上增加功能,只要能快速解决问题,啥办法都行。

问题描述:
在表格中,采用foreach的方式从$items读取每个记录,并且在表格中每行显示若干关键词相应的内容。$items是来自于后端php代码处理的结果。现在我需要在显示某个关键词内容时加个判断,读取相应内容,并且做个小链接。
代码如下:

{foreach $items as $item}
{strip}
。。。。。。。。。。。。。。
。。。。。。。。。。。。。。
{if $item->kind == "folder"}
<td class="name"><p><a href="/Views/Regions/{$item->id}">{$item->name}</a></p></td>
{elseif $item->type != "link"}
<td class="name"><p><a href="/Regions/Files/Download/{$item->id}">{$item->name}</a></p></td>
{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]}

<td class="name"><p><a href="{$link_content}" target="_blank">{$item->name}</a></p></td>
{/if}
........................
...........................
{/strip}
{/foreach}



奇怪的问题发生了,网页功能显示的完全正确,但在表格上方出现若干“1”。我一句一句删除代码并检测,发现{$con = mysqli_connect("localhost","root","root")}产生了“1”。只要删除这句话,就没有1产生、出现出错信息;如果不删除这句话而删除后面的代码,则有1,且后面出现出错信息。

大牛们,这个是咋回事情?我该如何修改呢?
其实我很想把这部分代码放到后端文件中去,但这里一个关键变量$item->id是在循环中读取的,我后面没有办法多次读取、并搜索该条记录中的关键词content中的值,且返回到tpl文件中去显示出来。

该如何处理呢?请大牛们赐教啊。
...全文
210 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
歪着看世界 2014-03-03
  • 打赏
  • 举报
回复
那好,徐版主说没有问题了,那大概八九不离十了。 结贴。
引用 14 楼 xuzuning 的回复:
这是没有问题的
xuzuning 2014-03-03
  • 打赏
  • 举报
回复
这是没有问题的
歪着看世界 2014-03-03
  • 打赏
  • 举报
回复
徐版主,这样删掉没有大问题吧?
引用 11 楼 xuzuning 的回复:
如过是{mysqli_close($con)}这句话产生了“1” 那就把 {mysqli_close($con)} 删掉
歪着看世界 2014-03-03
  • 打赏
  • 举报
回复
xu版主来了,欢迎啊。 我就是这么干的,确实没有“1”了。但这么干,不会产生什么坏影响吧?比如,与数据库的链接占用内存?对系统还产生其他影响? 但细想想,应该没有多大事情,因为系统都采用$con这个表示链接,最多也就一个变量占用内存而得不到释放?
xuzuning 2014-03-03
  • 打赏
  • 举报
回复
如过是{mysqli_close($con)}这句话产生了“1” 那就把 {mysqli_close($con)} 删掉
歪着看世界 2014-03-03
  • 打赏
  • 举报
回复
更正:
多次测试后,发现是{mysqli_close($con)}这句话产生了“1”,其结果为
一起混吧 2014-03-03
  • 打赏
  • 举报
回复
模板中不要写逻辑代码,尽量都写到php中去。
引用
2.smarty模板中前端的循环,如果与后端php代码处理动态的进行数值传输呢? 我需要把每次循环中的关键变量传到后端,经过处理后又把结果返回该循环中呢,并且在该次循环中显示php传送过来的值。如果做到呢?
交互不外乎两种,form提交到后端,或者ajax传值。
歪着看世界 2014-03-03
  • 打赏
  • 举报
回复
奶奶的,我的语文是体育老师教的。 更正啊:在php代码部分,第一条注释为: //该页面的基本功能就是在表格中显示某个区域里的文件或者文件夹,除文件和文件夹类型外我要增加一个类型:链接link,并且还增加其他一些与link链接这种类型相关的功能
歪着看世界 2014-03-03
  • 打赏
  • 举报
回复
其实如果不管代码的话,我的问题归结下来就两个细节: 1. 在smarty模板中,怎么{$con = mysqli_connect("localhost","root","root")}就会产生这么多的“1”呢?表明连接数据库成功么?有无方法隐藏这个显示? 2.smarty模板中前端的循环,如果与后端php代码处理动态的进行数值传输呢? 我需要把每次循环中的关键变量传到后端,经过处理后又把结果返回该循环中呢,并且在该次循环中显示php传送过来的值。如果做到呢? 大神们,你们有办法解决么?希望给出代码示例,否则小弟不会做啊。谢谢啊。
歪着看世界 2014-03-03
  • 打赏
  • 举报
回复
其实,我就非常纳闷了,咋就出现这么多“1”呢? 与该smarty相对应的后端php 代码为:(我已经尽量加注释了)

<?php
load_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}
	            <tr class="light" style="display:table-row;border-bottom:1px solid #DADAE3;" id="{$item->id}">
	          
	          
	          	{if $item->kind == "folder"}
	          
	               <td class="type"><p><img src="/Images/iconset/Folder2.png" title="folder"/></p></td>
	              
	             {else}
	             
	             {if $item->type == "docx" || $item->type == "doc"}
	            	
	            		<td class="type"><p><img src="/Images/iconset/page_white_word.png" title="word document"/></p></td>
	            	{else if $item->type == "xlsx" || $item->type == "xls"}
	            		<td class="type"><p><img src="/Images/iconset/page_white_excel.png" title="excel document"/></p></td>	
	            		
	            	{elseif $item->type == "pdf"}
	            		<td class="type"><p><img src="/Images/iconset/page_white_acrobat.png" title="PDF"/></p></td>
	            	
	            	{elseif $item->type == "png" || $item->type == "gif" || $item->type == "jpg"}
	            		<td class="type"><p><img src="/Images/iconset/picture.png" title="png"/></p></td>
	            	
	            	{elseif $item->type == "ppt" || $item->type == "pptx"}
	            		<td class="type"><p><img src="/Images/iconset/page_white_powerpoint.png" title="powerpoint document"/></p></td>
	            	
	            	{elseif $item->type == "mp4"}
	            		<td class="type"><p><img src="/Images/iconset/film.png" title="movie"/></p></td>
	            		
	            	{elseif $item->type == "link"}
	            		<td class="type"><p><img src="/Images/iconset/link.png" title="link"/></p></td>	
	            		
	            	{else}
	            		<td class="type"><p><img src="/Images/iconset/page_white.png" title="document"/></p></td>	
	            	{/if}
	             
	             
	             {/if}
	               
	               {if $item->kind == "folder"}
	               	<td class="name"><p><a href="/Views/Regions/{$item->id}">{$item->name}</a></p></td>
	               {elseif $item->type != "link"}
	               	<td class="name"><p><a href="/Regions/Files/Download/{$item->id}">{$item->name}</a></p></td> 
	               {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]}
												  
					<td class="name"><p><a href="{$link_content}" target="_blank">{$item->name}</a></p></td>
	               {/if}
	               
	               <td class="creator"><p>{$item->createdByName}</p></td>
	               <td class="created"><p>{$item->created}</p></td>
	               <td class="modified"><p>{$item->lastUpdated}</p></td>
	               <td class="visibility"><p>Visible</p></td>
	               <td class="size"><p>{if $item->size > 1000}
               			{round(($item->size / 1024), 1)}MB
               		{else}
               			{$item->size}KB</p></td>
               		{/if}</p></td>
               		
               		{if $item->kind == "folder"}
	               		<td class="id" align="middle"><p><a href="JavaScript:void(0);" onclick="return popitup('/Views/Folders/{$item->id}/Move')"><img src="/Images/iconset/move.png" style="width:18px;" /></a></p></td>
	               	{elseif $item->kind == "file"}
	               		<td class="id" align="middle"><p><a href="JavaScript:void(0);" onclick="return popitup('/Views/Files/{$item->id}/Move')"><img src="/Images/iconset/move.png" style="width:18px;" /></a></p></td>
	               	{/if}
	               	
	               
	               {if $item->kind == "folder"}
	               	<td class="edit"><p><a href="/Views/Folders/Edit/{$item->id}"><img src="/Images/iconset/wrench2.png" width="16px" title="edit"/></a></p></td>
	               {elseif $item->kind == "file"}
	               	<td class="edit"><p><a href="/Views/Files/Edit/{$item->id}"><img src="/Images/iconset/wrench2.png" width="16px" title="edit"/></a></p></td>
	               {/if}
	               
	               <td class="remove"><p><a href="/Views/Folders/Delete/{$item->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?');"><img src="/Images/iconset/cross.png" width="16px" title="delete"></a></p></td>
		               
		               {else}
		               		onclick="JavaScript:return confirm('Do you really want to delete this folder?');"><img src="/Images/iconset/cross.png" width="16px" title="delete"></a></p></td>
		               {/if}
		            {elseif $item->kind == "file"}
		            
		            <td class="remove"><p><a href="/Views/Files/Delete/{$item->id}" onclick="JavaScript:return confirm('Do you really want to delete this file?');"><img src="/Images/iconset/cross.png" width="16px"/ title="delete"></a></p></td>   
		               
	               {/if}
	              
	            </tr>
	            
	            {/strip}
	            {/foreach}
	     {else}
	     
	     <tr><td colspan="10"><table>
    <tr valign="middle" >
        <td class="light">
        
        	{if $action != "search"}
            	<img src="/Images/emptyfolder.png" width="130px"/>
          	{/if}
            
            
        </td>
        <td class="light">
        
         {if $action == "search"}
             <br />
             	<h1>No files or folders could be found!</h1>
        {else}
        
        <h1>This folder is empty!</h1>
        
        {/if}

嘻哈大咖秀 2014-03-03
  • 打赏
  • 举报
回复
吧你的后台代码贴出来 标识好 我看看
歪着看世界 2014-03-03
  • 打赏
  • 举报
回复
那问题就归结为: smarty模板中前端采用一个循环来显示内容,那我如何把每次循环中的关键变量传到后端,经过处理后又把结果返回该循环中呢? 其实,想想逻辑上也不难,大神们,有代码例子么?这里叩谢了。
歪着看世界 2014-03-03
  • 打赏
  • 举报
回复
感谢关注啊,我第一次用smarty,还是在别人代码基础上增加新功能。 不知道楼上所说的用标签替换掉,怎么替换掉?我也考虑过采用如下方式传送变量: $smarty->assign("action", $action); $smarty->display("folders.tpl"); 在我看来,上面的方面只能是一次性的传送。但在我的表格中每条记录是不停的循环并且显示的,如果我要把前端每次循环的关键变量id传送到后端进行处理,那又应该如何做呢?
嘻哈大咖秀 2014-03-03
  • 打赏
  • 举报
回复
还是改你php吧 吧要添加的内容循环添加进去 页面直接调用就行了
lf_1031a 2014-03-03
  • 打赏
  • 举报
回复
这个可以写在PHP里面,然后用用标签替换掉啊,在模板里面写什么连接数据库呢?

20,398

社区成员

发帖
与我相关
我的任务
社区描述
“超文本预处理器”,是在服务器端执行的脚本语言,尤其适用于Web开发并可嵌入HTML中。PHP语法利用了C、Java和Perl,该语言的主要目标是允许web开发人员快速编写动态网页。
phpphpstorm 技术论坛(原bbs)
社区管理员
  • 开源资源社区
  • phpstory
  • xuzuning
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧