2017-03-06 167 views
0

對不起,如果這是重複的帖子,但不知何故,我不能讓我的循環與變量工作。如果我回聲這裏面foreach我得到我的數據,但如果我使用變量我只獲得我的數組中的第一項數據。PHP Foreach循環無法與變量

我的代碼`

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

我在這件事情很是初學者所以也許有人可以幫助。我也四處搜尋,嘗試了不同的東西,但它不起作用。我也嘗試在沒有for循環的情況下顯示數據,但它也不起作用。

此外,如果有任何改進我可以用我的代碼做請告訴我。就像把它縮短等,我在這裏,以提高豈非:d

回答

1

你缺少.$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> 
    "; 
+0

它的工作現在,謝謝。我真的不知道我需要。= on echo。我沒有看到,當我谷歌。 – drpzz

+0

'。='只是意味着「將此添加到已經存在於此變量中」。所以基本上它擴展了該變量中的字符串。 –