0

I'm sorry, if this is duplicate post, but somehow i can't get my loop working with variable. if i echo this inside foreach i get my data but if i use variable i get data for only the first item from my array.

my code `

        $user - My arrray
        $count = $user['count'];
        $echo = "";
        foreach($user as $val => $key)
        {   
            if($val == 'true')
            {   
                for($x = 0; $x < $count; $x++)
                {
                    $name = $user[$val][$x]['name'];
                    $id = $user[$val][$x]['id'];
                    $staatus = $user[$val][$x]['status'];
                    $feed = $user[$val][$x]['feed'];
                    /* Now when am using only echo, instead of $echo it will work flawlessly,
                    but i want to echo it using an variable called $echo. 
                    I call the variable on my html page where the forms are,
                    but i'm only getting data for the first element. 
                    Short, i want to display my data under the form.*/
                    $echo = "
                    <table class='table'>
                        <tr>
                            <th>NIMI</th>
                            <th>ID</th>
                            <th>STAATUS</th>
                            <th>FEED</th>
                        </tr>
                        <tr>
                            <td>$name</td>
                            <td>$id</td>
                            <td>$staatus</td>
                            <td>$feed</td>
                        </tr>
                    </table>
                        ";
                }
            }

        }
         <?php  echo $echo; ?> for calling.

I'm quite a beginner in this thing so maybe someone could help. I also searched around, tried different things but it's not working. I also tried to display data without for loop, but it's not working either.

Also if there's any improvments i could do with my code please tell me. Like make it shorter etc, i'm here to improve so why not :D

1 Answer 1

1

You're missing the . in your $echo .= "

$echo .= "
<table class='table'>
    <tr>
        <th>NIMI</th>
        <th>ID</th>
        <th>STAATUS</th>
        <th>FEED</th>
    </tr>
    <tr>
        <td>$name</td>
        <td>$id</td>
        <td>$staatus</td>
        <td>$feed</td>
    </tr>
</table>
    ";
Sign up to request clarification or add additional context in comments.

2 Comments

It's working now, thanks. I really didn't knew i need .= on echo. I didn't see that when i was google'ing either.
.= just means to "add this on to what already exists in this variable". So basically it's extending the string in that variable.

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.