2015-04-05 139 views
0

我試圖顯示whileloop以外的點。我正在獲取像這樣的結果。while循環後顯示結果計數

Facebook 
Twitter,Facebook,Instagram,Youtube 
Facebook 
Facebook 

它從每一行提取Facebook並最終計數次數。

<?php 
    $q = "select * FROM users"; 
    $r = mysql_query($q); 
    $total = mysql_num_rows($r); 

    while($row = mysql_fetch_assoc($r)) { 
    $fb= $row['social']; 
    $dbreq = implode(',',explode(',', $fb)); 
    $fa=array("Twitter,",",Instagram,","Youtube"); 
    $newstring = str_replace($fa, "", $dbreq); 

    echo $points= count(explode(',', $newstring)); 
    } 
?> 
+0

爲什麼不使用SELECT COUNT(*)? – 2015-04-05 11:00:59

+2

你的問題是什麼?你想要的輸出是什麼? – 2015-04-05 11:03:26

+0

我想通過直接或修改字符串來計算Facebook中保存的facebook的次數 – Roger 2015-04-05 11:04:34

回答

2

請嘗試這樣,

<?php 
$q = "select * FROM users"; 
$r = mysql_query($q); 
$total = mysql_num_rows($r); 
$cnt =0 ; 
while($row = mysql_fetch_assoc($r)) { 
$fb= $row['social']; 
if (strpos($fb,'facebook') !== false) { 
    $cnt++; 
} 
} 
echo "TOTAL:".$cnt; 
    ?> 
+0

我在哪裏迴應結果? – Roger 2015-04-05 11:25:38

+0

我編輯了我的anwser,請檢查,如果有效,請接受我的回答 – 2015-04-05 11:28:07

+0

感謝您的幫助 – Roger 2015-04-05 11:29:51