简体   繁体   English

php foreach里面的回声是html代码

[英]php foreach inside an echo which is html code

I'm in trouble with some php output. 我遇到了一些php输出问题。 I'm already echoing out some html and that works fine! 我已经在回显一些html了,并且效果很好! Those are groups that should contain more elements -> I need to output some anchor elements dynamically from the database inside each group. 这些是应该包含更多元素的组->我需要从每个组内的数据库动态输出一些锚点元素。 And that needs to happen inside the html which is already an echo. 这需要在已经是回显的html内发生。 I thought I could put another foreach loop inside there...I tried and tried but I can't figure it out...I always get an error. 我以为我可以在里面放另一个foreach循环...我试了又试,但我不知道...我总是遇到错误。

Dreamweaver is telling me, that this part Dreamweaver告诉我,这部分

foreach($links as $item){echo $item;}

is definitely wrong (syntax error), but I don't know how to put it in there correctly. 肯定是错误的(语法错误),但是我不知道如何正确地将其放入其中。

Thanks for your help! 谢谢你的帮助!

echo '<li id="todo-'.$this->data['id'].'" class="todo">     
    <div class="group__name"><div class="text input_groupname">'.$this->data['text'].'</div>       
    </div>
    <div class="group_more_button">more <i class="fa fa-arrow-circle-o-down"></i></div>
    <div class="group_list_wrapper">
            <div class="group__list_SortMe">'


    '.foreach($links as $item){
        echo $item;
    }.'

               '<div class="add_button">
                  <div class="plus open">
                       <span></span>
                       <span></span>
                  </div> 
               </div> 
            </div> <!-- group__list_SortMe-->
    </div> <!-- group_list_wrapper--> 

            <div class="actions">
                <a href="#" class="edit">Edit</a>
                <a href="#" class="deleteICON">Delete</a>
            </div>

        </li>';

Or this?...just for readibility sake. 还是这个?...仅出于可读性考虑。 I have to jump in and out of PHP to make such long strings logical to me. 我必须跳入和跳出PHP才能使这么长的字符串对我来说合乎逻辑。

<li id="todo-<?=$this->data['id']?>" class="todo">     
    <div class="group__name"><div class="text input_groupname"><?=$this->data['text']?></div>       
    <div class="group_more_button">more <i class="fa fa-arrow-circle-o-down"></i></div>
    <div class="group_list_wrapper">
        <div class="group__list_SortMe">


        <?php 
        foreach($links as $item){
            echo $item;
        }
        ?>
            <div class="add_button">
                <div class="plus open">
                    <span></span>
                    <span></span>
                 </div> 
             </div> 
          </div> <!-- group__list_SortMe-->
    </div> <!-- group_list_wrapper--> 

    <div class="actions">
        <a href="#" class="edit">Edit</a>
        <a href="#" class="deleteICON">Delete</a>
    </div>
</li>

foreach returns nothing (AFAIK), you chaining it in your echo wouldn't do anything. foreach返回任何内容(AFAIK),将其链接到echo中将不会执行任何操作。 Finish the echo , and begin the foreach call on a new line. 完成echo ,然后在新行上开始foreach调用。 After that, start a new echo . 之后,开始一个新的echo Like this: 像这样:

echo "blah blah this is a whole lotta text ";

foreach($links as $item){
    echo $item;
}

echo " and this is some more text";

Why so complicated? 为什么这么复杂? Try it like this: 像这样尝试:

echo '<li id="todo-'.$this->data['id'].'" class="todo">     
<div class="group__name"><div class="text input_groupname">'.$this->data['text'].'</div>       
</div>
<div class="group_more_button">more <i class="fa fa-arrow-circle-o-down"></i></div>
<div class="group_list_wrapper">
        <div class="group__list_SortMe">';

foreach($links as $item){
    echo $item;
}

echo '<div class="add_button">
              <div class="plus open">
                   <span></span>
                   <span></span>
              </div> 
           </div> 
        </div> <!-- group__list_SortMe-->
</div> <!-- group_list_wrapper--> 

        <div class="actions">
            <a href="#" class="edit">Edit</a>
            <a href="#" class="deleteICON">Delete</a>
        </div>

    </li>';

Rather than echo HTML, why don't you separate it? 而不是echo HTML,您为什么不分开它呢? It helps performance having to echo less. 它有助于减少echo

<li id="todo-<?php echo $this->data['id']; ?>" class="todo">     
    <div class="group__name"><div class="text input_groupname"><?php echo $this->data['text']; ?></div>       
    </div>
    <div class="group_more_button">more <i class="fa fa-arrow-circle-o-down"></i></div>
    <div class="group_list_wrapper">
        <div class="group__list_SortMe">
<?php
foreach($links as $item)
{
    echo $item;
}
?>

            <div class="add_button">
                <div class="plus open">
                    <span></span>
                    <span></span>
                </div> 
            </div> 
        </div> <!-- group__list_SortMe-->
    </div> <!-- group_list_wrapper--> 
    <div class="actions">
        <a href="#" class="edit">Edit</a>
        <a href="#" class="deleteICON">Delete</a>
    </div>
</li>';

To be a bit more explicit, foreach is a keyword that cannot be concatenated ( echo"x". foreach () {} ."y"; ) into a string. 更明确一点,foreach是一个不能串联的关键字( echo"x". foreach () {} ."y"; )成字符串。 It requires its own block statement to execute properly. 它需要自己的block语句才能正确执行。

echo("<tag>");

foreach ($items as $item) {
    /* execute logic here */
}

echo("</tag>");

Below i correct your code and hope it will work fine. 下面我更正您的代码,并希望它能正常工作。

    echo '
<li id="todo-'.$this->data['id'].'" class="todo">     
<div class="group__name"><div class="text input_groupname">'.$this->data['text'].'</div>       
</div>
<div class="group_more_button">more <i class="fa fa-arrow-circle-o-down"></i></div>
<div class="group_list_wrapper">
        <div class="group__list_SortMe">

';
foreach($links as $item){
    echo $item;
}

    echo '<div class="add_button">
                <div class="plus open">
                    <span></span>
                    <span></span>
                </div> 
            </div> 
        </div> <!-- group__list_SortMe-->
</div> <!-- group_list_wrapper--> 

        <div class="actions">
            <a href="#" class="edit">Edit</a>
            <a href="#" class="deleteICON">Delete</a>
        </div>

    </li>';

Problem was that , you have to close echo before foreach and after foreach start a new echo. 问题是,您必须在foreach之前关闭回显,并且在foreach开始新的回显之后。 same logic is working fine with my code. 同样的逻辑在我的代码中也可以正常工作。 Hope this also. 希望这个也。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM