The php code is embedded in the smarty template. the error is ridiculous. please help me!

Source: Internet
Author: User
The php code is embedded in the smarty template. the error is ridiculous. please help me! First of all, you guys, we don't have to worry about this type of problem because we should not embed php code into the smarty template, okay? Because I also add features to other people's code, as long as the problem can be solved quickly, what can be done.

Problem description:
In a table, you can use foreach to read each record from $ items and display several keywords in each row of the table. $ Items is the result of backend php code processing. Now I need to add a judgment when displaying a keyword, read the corresponding 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}


A strange problem occurs. the webpage function is displayed completely correctly, but several "1" appear above the table ". I deleted the code one by one and detected that {$ con = mysqli_connect ("localhost", "root", "root")} generated "1 ". If this sentence is deleted, no error information is generated or displayed. if this sentence is not deleted, only one code is deleted, and an error message is displayed later.

What is this? How can I modify it?
In fact, I really want to put this part of code in the back-end file, but here a key variable $ item-> id is read in the loop, I cannot read and search for the value in the keyword content in this record multiple times, and return it to the tpl file for display.

What should we do? Please enlighten me.


Reply to discussion (solution)

This can be written in PHP and replaced with tags. in the template, what is the connection to the database?

Let's Change Your php. just add the content to be added to the page in a loop and call it directly.

Thanks for your attention. I used smarty for the first time and added new features based on other code.

I don't know how to replace the tag mentioned above. how can I replace it? I have also considered using the following method to transfer variables:

$ Smarty-> assign ("action", $ action );

$ Smarty-> display ("folders. tpl ");
In my opinion, the above aspect can only be a one-time transfer. However, every record in my table is repeatedly displayed. if I want to transfer the key variable id of each loop at the front end to the backend for processing, what should I do?

The problem is that the front-end of the smarty template uses a loop to display the content. how can I transmit the key variables in each loop to the backend, after processing, return the result to the loop?

Actually, it's not hard to think about logic. do you have any code examples? Thank you.

Paste your background code to identify it. let's take a look.

In fact, I am very puzzled. why is there so many "1?
The backend php code corresponding to this smarty is: (I have tried to add comments)

 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 // Another action on this page is normally displayed {$ 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); // Obtain the display record and combine multiple records $ items = sort_items ($ items ); // sorting functions $ smarty-> assign ("items", $ items); $ smarty-> assign ("action", $ action ); $ smarty-> display ("folders. tpl ");?>


There are too many front-end code. I still take the part of the loop for your 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 $ 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 cocould be found! {Else} This folder is empty! {/If}

In fact, no matter the code, my problem comes down to two details:
1. in the smarty template, how does {$ con = mysqli_connect ("localhost", "root", "root")} generate so many "1? Indicates that the database is successfully connected? Is there any way to hide this display?

2. what if the front-end loop in the smarty template is dynamically transmitted with the backend php code processing?
I need to upload the key variables in each loop to the backend, and then return the results to the loop after processing, and display the values transmitted by php in this loop. What if this is done?

Do you have a solution? I hope to give a sample code, otherwise the younger brother will not do it. Thank you.

Grandma, my language is taught by a PE teacher.

Correct: in the php code section, the first comment is:
// The basic function of this page is to display files or folders in a certain area in the table. in addition to the file and folder types, I want to add a type: link, other functions related to the link type are also added.

Do not write logic code in the template, and try to write it to php.

2. what if the front-end loop in the smarty template is dynamically transmitted with the backend php code processing?
I need to upload the key variables in each loop to the backend, and then return the results to the loop after processing, and display the values transmitted by php in this loop. What if this is done? There are two types of interaction: form submitted to the backend, or ajax passed values.

Correction:
After multiple tests, it is found that {mysqli_close ($ con)} produces "1" and the result is

For example, if {mysqli_close ($ con)} is used, "1" is generated"
Delete {mysqli_close ($ con )}.

The xu moderator is here. welcome.

This is what I did. there is indeed no "1. But will this do not have any bad effects? For example, does the connection to the database occupy memory? Other impacts on the system?

But think about it, there should be no big deal, because the system uses $ con to indicate the link. at most one variable occupies the memory and cannot be released?

Xu moderator, isn't it a big problem to delete it like this?

For example, if {mysqli_close ($ con)} is used, "1" is generated"
Delete {mysqli_close ($ con )}.

This is OK

Well, Xu said that there is no problem, and it will be around.
Close the post.

This is OK

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.