keyem
keyem

Reputation: 51

How to get the total COUNT for the results in For Loop

I have a query for one of my search function. A while loop is applied to this query. I have used many sets of if functions inside this while loop to further filter the results as per the user search requirement. And the {email id} (one of the variable in the step1 result) of the final filter result in the if statement set is used to query another table where in a foreach loop is used & obtain other specific details in that table.

// 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>';
}

I'm getting the desired result and The results are filtered and shown as per the code above.

My problem is that I'm not getting the total COUNT of the results displayed. Kindly advise for a solution.

Lot of thanks in advance.

Upvotes: 0

Views: 489

Answers (1)

chalasr
chalasr

Reputation: 13167

SQL :

$count = mysql_num_rows($result_finda);

PHP :

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

EDIT

Use the following :

$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

In your code :

$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

Upvotes: 1

Related Questions