如何在For循环中获得结果的总COUNT

时间:2016-03-14 18:39:58

标签: php mysql sql

我有一个查询我的搜索功能。 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>';
}

我获得了所需的结果,结果将按照上面的代码进行过滤和显示。

我的问题是我没有得到显示结果的总COUNT。请建议解决方案。

提前多多谢意。

1 个答案:

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