0

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>';
2
  • 1
    You don't have a semicolon after the first echo. You have to close the first echo and start a new echo after the loop Commented Aug 12, 2014 at 17:31
  • another solution would be to build the string up inside a variable first, and then just echo the variable once at the end. Commented Aug 12, 2014 at 17:33

6 Answers 6

1

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>
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Too bad...it's still not working. Now I'm getting an error message: 'Catchable fatal error: Method () must return a string value' ...using the __toString method
0

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

Comments

0

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

Comments

0

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>");

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.