First of all, you great God, we do not have to worry about the PHP code should not be embedded in the Smarty template such problems, okay? Because I also add the function in other people's code, as long as can solve the problem quickly, what method is OK.
Problem Description:
In the table, each record is read from $items in a foreach manner, and each row in the table displays the corresponding contents of several keywords. $items is the result of the back-end PHP code processing. Now I need to add a judgment when displaying a keyword, read the content, and make a small link.
The code is as follows:
{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}
The strange problem has occurred, the webpage function appears completely correct, but appears above the table several "1". I deleted the code and detected it, and found that {$con = Mysqli_connect ("localhost", "root", "root")} produced "1". As long as the deletion of this sentence, there is no 1 generation, error message, if you do not delete this sentence and delete the following code, there is 1, and the following error message appears.
Daniel, what's the situation? How do I change it?
In fact, I would like to put this part of the code into the back-end file, but here a key variable $item->id is read in the loop, I have no way to read, and search for the key words in the content of the value, and return to the TPL file to display.
What to do with it? Please enlighten them.
Reply to discussion (solution)
This can be written in PHP, and then replace it with a tag, what is written in the template to connect the database?
Let's change your PHP. To add the content loop added to the page directly called on the line
Thanks for the attention, I used smarty for the first time, or added new features based on other people's code.
Do not know what the upstairs said to replace with the label, how to replace it? I've also considered transferring variables in the following ways:
$smarty->assign ("action", $action);
$smarty->display ("Folders.tpl");
In my opinion, the above aspect can only be a one-time transmission. But what should I do if I want to pass the key variable ID of each loop of the front end to the backend for processing, and every record in my table is continuously looped and displayed?
The problem boils down to this: the front end of the Smarty template uses a loop to display the content, so how do I pass the key variables in each loop to the backend, and then return the results back to the loop after processing?
In fact, it is not difficult to think logically, the great gods, there is a code example? It's heavens here.
Well, your backstage code is sticking out and identifying me.
In fact, I was very puzzled, why so many "1"?
The back-end PHP code corresponding to the Smarty is: (I've tried to annotate it as much as possible)
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 = "Searc H Result "; $smarty->assign (" Paths ", Array ($path));} else//Another action on this page is normally displayed {$folders = List_items ("Folders", $id), $folder = get_item ("Folders", $id); $file s = 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); Get to display records and combine multiple records $items = Sort_items ($items); sorting function $smarty->assign ("items", $items), $smarty->assign ("action", $action) $smarty->display ("Folders.tpl ");? >
There's too much code on the front end, and I'm still intercepting that part of the Loop for reference:
{if count ($items) > 0} {foreach $items as $item} {Strip}{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 was not empty. If you delete the It all content would also be deleted. 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} In fact, if no matter the code, my problem comes down to two details: 1. In the Smarty template, how do {$con = Mysqli_connect ("localhost", "root", "root")} produce so many "1"? Does the connection to the database succeed? Is there any way to hide this display? 2.smarty template in the front end of the loop, if with the back-end PHP code processing dynamic value transfer? I need to pass the key variable in each loop to the back end, and then return the result to the loop after processing, and show the value that PHP sent over in that loop. What if we do? Do you have a solution, big God? Want to give the code example, or the younger brother will not do ah. Thanks, man. Grandma's, my Chinese is taught by the P.E. teacher. Correction: In the PHP code section, the first comment is: The basic function of this page is to display the file or folder in an area in the table, except for the file and folder Type I want to add a type: Link links, and also add some other links to link this type of function related Do not write logic code in the template, try to write to PHP. 2.smarty template in the front end of the loop, if with the back-end PHP code processing dynamic value transfer? I need to pass the key variable in each loop to the back end, and then return the result to the loop after processing, and show the value that PHP sent over in that loop. What if we do? There are two types of interactions, form commits to the backend, or Ajax values. Correct: After several tests, it was found that the phrase {Mysqli_close ($con)} produced "1", the result is If it is {mysqli_close ($con)} This sentence produces a "1" Then delete {mysqli_close ($con)} Xu Moderator came, welcome ah. That's what I did, and there's really no "1". But this is not going to have any bad effects, is it? For example, a link to a database consumes memory? Have other effects on the system? But think about it, there should be no big thing, because the system uses $con this means that the link, up to a variable to occupy memory and not be released? Xu Moderator, so delete no big problem? If it is {mysqli_close ($con)} This sentence produces a "1" Then delete {mysqli_close ($con)} It's no problem. Well, Xu Moderator said no problem, that probably sorta. Knot stickers. It's no problem. |