RE: Assistance on Query

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi People,

I would like some assistance on the following scenario.

I have to pull out all records for a particular userid (easy enough) and
then only show those entries where the follwing occurs.

These records from the table will contain either an entry in the
services_type field or the non_services_type field. What I need to do is
show only those where the number of consecutive records that contain an
entry in the non_services_type field is greater than or equal to 3

so example:-

record 1 contains an entry in non_services_type
record 2 contains an entry in services_type
record 3 contains an entry in non_services_type
record 4 contains an entry in non_services_type
record 5 contains an entry in non_services_type
record 6 contains an entry in services_type


so I would need to display records 3,4,5 only

Can anyone assist me with this?

Cheers,

Shannon

When you do your while ($row = mysql_fetch_assoc($result)) { ... } you will need to keep a list of consecutive records.


Try this:

<?php
$i = 0;
$j = 0;
while ($row = mysql_fetch_assoc($result)) {
$current_string = ( !empty($row['non_services_type']) ) ? 'non_services_type' : 'services_type' ;


   if ($previous_string == $current_string) {
       $i++;
       if ($i >= 3) {
           $records[$j] = $row;
           $j++;
       }
   } else {
       $i = 0;
       $previous_string = $current_string;
   }

}
?>

Then simply echo $records. This may have some bugs to iron out, but this is a way to get the job done, please reply if you have found a better way.

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux