drpzz
drpzz

Reputation: 55

PHP Foreach loop not working with variable

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

Upvotes: 0

Views: 399

Answers (1)

DiddleDot
DiddleDot

Reputation: 744

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

Upvotes: 1

Related Questions