Fab
Fab

Reputation: 101

php foreach inside an echo which is html code

I'm in trouble with some php output. I'm already echoing out some html and that works fine! 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. 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.

Dreamweaver is telling me, that this part

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>';

Upvotes: 0

Views: 4380

Answers (6)

Muhammad Wasim Akram
Muhammad Wasim Akram

Reputation: 1079

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. same logic is working fine with my code. Hope this also.

Upvotes: 0

chersull_99
chersull_99

Reputation: 11

Or this?...just for readibility sake. I have to jump in and out of PHP to make such long strings logical to me.

<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>

Upvotes: 1

Malovich
Malovich

Reputation: 971

To be a bit more explicit, foreach is a keyword that cannot be concatenated (echo"x". foreach () {} ."y";) into a string. It requires its own block statement to execute properly.

echo("<tag>");

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

echo("</tag>");

Upvotes: 0

afaolek
afaolek

Reputation: 8831

Rather than echo HTML, why don't you separate it? It helps performance having to echo less.

<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>';

Upvotes: 0

beeef
beeef

Reputation: 2702

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>';

Upvotes: 0

Robin Kanters
Robin Kanters

Reputation: 5146

foreach returns nothing (AFAIK), you chaining it in your echo wouldn't do anything. Finish the echo, and begin the foreach call on a new line. After that, start a new 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";

Upvotes: 0

Related Questions