The php code is embedded in the smarty template. this is a joke. 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 the corresponding content of several keywords in each row of the table. $ Items is the php code embedded in the smarty template. it is a joke. 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.
------ Solution --------------------
This can be written in PHP and replaced with tags. in the template, what is the connection to the database?
------ Solution --------------------
Let's Change Your php. just add the content to be added to the page in a loop and call it directly.
------ Solution --------------------
Do not write logic code in the template, and try to write it to php.

Reference
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.
------ Solution --------------------
This is OK

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.