2017-08-09 110 views
0

我想保存喜歡(likes)的數量在一個變量($likes),並在狀態後($statuslist)打印出來,如果它可能沒有foreach循環怎麼一回事,因爲我不能把$statuslist內我試過的循環。
但代碼只能部分工作。它回聲喜歡的數量,但只有當每個狀態帖子得到0,1,2,3等喜歡。我不想要這個。我想回復EACH與其他人分享的帖子數量。
對於這個foreach循環和fetch_object會正常工作,但如果我會使用foreach循環,我不得不把$statuslist裏面,我不想這樣。
那麼有沒有解決方案?回聲價值觀查詢

MySQL的:

$statusQuery = $conn->query(" 
     SELECT 
     status.id, 
     COUNT(status_likes.id) AS likes, 
     GROUP_CONCAT(users.username SEPARATOR '|') AS liked_by 
     FROM status 

     LEFT JOIN status_likes 
     ON status.id = status_likes.status 

     LEFT JOIN users 
     ON status_likes.user = users.id 

     GROUP BY status.id 
    "); 

    $likes = ""; 
    while($row = mysqli_fetch_array($statusQuery, MYSQLI_ASSOC)){ 
     $likes = $row["likes"]; 
    } 

狀態帖子:

$statuslist .= '<div id="status_'.$statusid.'" class="status_boxes">'; 
$statuslist .= '<div>'.$statusDeleteButton.'<p id="status_date">'; 
$statuslist .= '<b>Post: </b>'.$postdate.'</p>'.$user_image.'; 
$statuslist .= '<p id="status_text">'.$data.'</p>'; 
$statuslist .= '.$shareButton.''.$likeButton.''.$likes.' people like it. 
$statuslist .= '</div>'.$status_replies.'</div>'; 

編輯:
我已經嘗試過了,工作,但我不能喜歡的號碼保存到varable和在$statuslist中回顯它。

while($row = $statusQuery -> fetch_object()){ 
     $statuses[] = $row; 
    } 

foreach ($statuses as $status) { 
    echo $status->likes; 
} 
+0

你不這樣做與循環內部'$ likes',其覆蓋每個迭代東西。並且在「狀態帖子」代碼中出現語法錯誤。看到突出顯示。 – Qirel

+0

是的,我在這個webiste的文本編輯器中編輯過,但在我的文本編輯器中,我沒有語法錯誤。我該如何處理'$ likes'變量? –

回答

0
require("MyPost.php"); // Needs to be at the top of the script!! 

$statusQuery = $conn->query(" 
    SELECT 
    status.id, 
    COUNT(status_likes.id) AS likes, 
    GROUP_CONCAT(users.username SEPARATOR '|') AS liked_by 
    FROM status 

    LEFT JOIN status_likes 
    ON status.id = status_likes.status 

    LEFT JOIN users 
    ON status_likes.user = users.id 

    GROUP BY status.id 
"); 

$posts = array(); 

while($row = mysqli_fetch_array($statusQuery, MYSQLI_ASSOC)){ 

    $mypost = new MyPost($row); 
    array_push($posts, $mypost); 

} 


foreach ($posts as $key => $post_value) { 
    // echo $post_value->id 
    echo $post_value->display_post(); 
} 

MyPost.php創建一個新的文件,並把它放在同一文件夾中的腳本。 MyPost.php的內容需要如下代碼。

class MyPost 
{ 

    // You can change these to private if you would like (change public to private) 
    public $statusDeleteButton = "DELETE_BUTTON_HERE"; 
    public $likeButton = "LIKE_BUTTON_HERE"; 
    public $shareButton = "LIKE_BUTTON_HERE"; 


    // Keep public 
    public $id, $postdate, $user_image, $liked_by, $likes, $data, $status_replies; 

    // Code that is ran when a new MyPost object is created. 
    public function __construct($array) 
    { 
     // I am assuming you are getting these from the sql also? 
     //If you are then you can delete these variables. 
     $this->user_image = ""; 
     $this->postdate = ""; 
     $this->status_replies = ""; 
     $this->data = ""; 

     // Set the variables (Don't delete) 
     foreach ($array as $key => $value) { 
      $this->$key = $value; 
     } 
    } 

    // Return the string for the post. 
    public function display_post() 
    { 
     return '<div id="status_'.$this->id.'" class="status_boxes"> 
      <div>'.$this->statusDeleteButton.'<p id="status_date"> 
       <b>Post: </b>'.$this->postdate.'</p>'.$this->user_image. 
       '<p id="status_text">'.$this->data.'</p>'. 
       $this->shareButton.''.$this->likeButton.''.$this->likes.' people like it. 
     </div>'.$this->status_replies.'</div>'; 
    } 
}