while循环后显示结果计数

时间:2015-04-05 10:57:42

标签: php php-5.3

我试图在whileloop外面显示点数。我正在拿这样的结果。

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));
  }
?>

1 个答案:

答案 0 :(得分: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;
    ?>