【问题标题】:How to get the total COUNT for the results in For Loop如何获取 For Loop 中结果的总 COUNT
【发布时间】:2016-03-15 20:21:11
【问题描述】:

我有一个关于我的搜索功能的查询。对这个查询应用了一个 while 循环。我在这个 while 循环中使用了多组 if 函数来根据用户搜索要求进一步过滤结果。并且 if 语句集中最终过滤器结果的 {email id}(step1 结果中的变量之一)用于查询在 foreach 循环中使用的另一个表并获取该表中的其他特定详细信息。

// WHILE SEARCH PROFILES
while ($result = mysql_fetch_array($result_finda)) {
    $emails=$result['email'];
    $age=$result['age'];
    $name=$result['name'];
    $height=$result['height'];
    $groups=$result['groups'];
    $country=$result['country'];

    if (($age_from <= $age)&& ($age <= $age_to)) {
        $a_emails =$emails;
        $a_age=$age;
        $a_firstname=$name;
        $a_height=$height;
        $a_groups=$groups;
        $a_country=$country;
    }

    if (($height_from <= $a_height)&& ($a_height <= $height_to)) {
        $h_emails =$a_emails;
        $h_age=$a_age;
        $h_firstname=$a_firstname;
        $h_groups=$a_groups;
        $h_country=$a_country;
    }

    foreach ($_POST['groups'] as $workgroup) {
        if (strpos($workgroup, $h_groups) !== false) {
            $m_emails =$h_emails;
            $m_age=$h_age;
            $m_name=$h_name;
            $m_groups=$h_groups;
            $m_country=$h_country;
        }
    }

    foreach ($_POST['country'] as $workcountry) {
        if (strpos($workcountry, $m_country) !== false) {
            $c_emails =$m_emails;
            $c_age=$m_age;
            $c_name=$m_name;
            $c_groups=$m_groups;
            $c_country=$m_country;
        }
    }

    $findeducation="SELECT * FROM education WHERE email='$c_emails'";
    $result_findeducation=mysql_query($findeducation);
    $educationstatus = mysql_fetch_array($result_findeducation);
    $profile_ed=$educationstatus['education'];

    foreach ($_POST['education'] as $educationstatus) {
        if (strpos($educationstatus, $profile_ed) !== false) {
            $e_emails =$_c_emails;
            $e_age=$c_age;
            $e_name=$c_name;
            $e_groups=$c_groups;
            $e_country=$c_country;
            $e_education=$profile_ed;
        }
    }

    if ($e_name) {
        $count++;
    }

    echo $e_name;echo '&nbsp'; 
    echo $e_emails;echo '&nbsp';
    echo $e_age;echo '</br>';
    echo $e_groups;echo '</br>';
    echo $e_country;echo '</br>';
}

我得到了想要的结果,结果按照上面的代码过滤和显示。

我的问题是我没有得到显示结果的总数。请提出解决方案。

非常感谢。

【问题讨论】:

  • SELECT COUNT(*) FROM USERS WHERE ...mysql_num_rows( $result_finda ); 在您的原始查询之后。此外, mysql_ 已弃用,现在已删除。考虑改用 mysqli_ 或 PDO。
  • 您要在代码中的哪个位置获取计数或打印它?
  • 您好,感谢 cmets。 @fusion3k 当我使用查询时,即使我有 2 个结果,它也只会给出计数 1
  • 也许我的评论不清楚。我会写一个答案(或者你可以在文档中看到......)
  • "DISTINCT *" ??那是行不通的。

标签: php mysql sql


【解决方案1】:

SQL:

$count = mysql_num_rows($result_finda);

PHP:

$count = count(mysql_fetch_array($result_finda));

编辑

使用以下内容:

$count = 0; // Initialize $count

while ($result = mysql_fetch_array($result_finda))
{
    // Filter your results
    $count++; // Increment $count for each result that matches with your filters
} 

print $count; // number of results after filtering

在您的代码中:

$finda="SELECT * FROM USERS WHERE gender='$gender'";
$result_finda=mysql_query($finda);
$count = 0;

while ($result = mysql_fetch_array($result_finda)) {
    $emails=$result['email'];
    $age=$result['age'];
    $name=$result['name'];
    $height=$result['height'];
    $groups=$result['groups'];
    $country=$result['country'];

    if (($age_from <= $age)&& ($age <= $age_to)) {
        $a_emails =$emails;
        $a_age=$age;
        $a_firstname=$name;
        $a_height=$height;
        $a_groups=$groups;
        $a_country=$country;
    }

    if (($height_from <= $a_height)&& ($a_height <= $height_to)) {
        $h_emails =$a_emails;
        $h_age=$a_age;
        $h_firstname=$a_firstname;
        $h_groups=$a_groups;
        $h_country=$a_country;
    }

    foreach ($_POST['groups'] as $workgroup) {
        if (strpos($workgroup, $h_groups) !== false) {
            $m_emails =$h_emails;
            $m_age=$h_age;
            $m_name=$h_name;
            $m_groups=$h_groups;
            $m_country=$h_country;
        }
    }

    foreach ($_POST['country'] as $workcountry) {
        if (strpos($workcountry, $m_country) !== false) {
            $c_emails =$m_emails;
            $c_age=$m_age;
            $c_name=$m_name;
            $c_groups=$m_groups;
            $c_country=$m_country;
        }
    }

    $findeducation="SELECT * FROM education WHERE email='$c_emails'";
    $result_findeducation=mysql_query($findeducation);
    $educationstatus = mysql_fetch_array($result_findeducation);
    $profile_ed=$educationstatus['education'];

    foreach ($_POST['education'] as $educationstatus) {
        if (strpos($educationstatus, $profile_ed) !== false) {
            $e_emails =$_c_emails;
            $e_age=$c_age;
            $e_name=$c_name;
            $e_groups=$c_groups;
            $e_country=$c_country;
            $e_education=$profile_ed;
        }
    }

    if ($e_name) {
        $count++;
    }

    echo $e_name;echo '&nbsp'; 
    echo $e_emails;echo '&nbsp';
    echo $e_age;echo '</br>';
    echo $e_groups;echo '</br>';
    echo $e_country;echo '</br>';
}

print "$count RESULTS"; // [number] RESULTS

【讨论】:

  • 嗨 chalasr。但是我对 $result_finda 的查询结果是通过 if 语句中的条件过滤的,所以 $count = count(mysql_fetch_array($result_finda)); 与最终结果计数不匹配
  • 我得到 0 个结果,即使我有 2 个结果
  • 你肯定做错了什么......确保$count = 0while之前,$count++在最后一个条件:if (strpos($educationstatus, $profile_ed) !== false) { $count++; // ... }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-14
相关资源
最近更新 更多