Re: Grouping records

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

 



Ok, this looks quite "databasey" so I guess you will have a query like:

SELECT Rowid, Person, Timediff FROM transitional_records ORDER BY Timediff

I don't know what DB you use. let's say it is MySQL (the others are similar).
Always provide such things when asking for advice. Else it's not easy to help.

EXAMPLE
<?php
mysql_connect("localhost", "mysql_user", "mysql_password") or
    die("Could not connect: " . mysql_error());
mysql_select_db("mydb");
$result = mysql_query("SELECT Rowid, Person, Timediff FROM
transitional_records ORDER BY Timediff");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    // do something with the result here
}
mysql_free_result($result);
?>

So what we get is an array like this ..

array (
Person => (string),
Timediff => (numeric)
);

Rowid, Person and Timediff are the only Values we need to get each
entries Groupid.
Now back to our example ..

EXAMPLE
<?php
mysql_connect("localhost", "mysql_user", "mysql_password") or
    die("Could not connect: " . mysql_error());
mysql_select_db("mydb");
$result = mysql_query("SELECT Rowid, Person, Timediff FROM
transitional_records ORDER BY Timediff");
$persons = array(); // array containing each persons current Groupid
$rowids = array(); // array containing each row's groupid
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $rowids[$row['Rowid']] = 0; // default for a row's groupid
	if ($row['Timediff'] > 60) {
		if (!isset($persons[$row['Person']])) $persons[$row['Person']] = 0;
		++$persons[$row['Person']];
	}
	if (isset($persons[$row['Person']])) $rowids[$row['Rowid']] =
$persons[$row['Person']];
}
var_dump($rowids);
mysql_free_result($result);
?>

So what is that script doing?
Well, first of all we set 2 arrays $persons and $rowids;
$persons is some sort of buffer for each person's current Groupid
whilst processing the table entries.
$rowids contains Groupid for each Row (set whilst processing)

WHILE LOOP:
First the current row's Groupid is set to the default value, which is
zero ( $rowids[CURRENT_ID] = 0 ).
Then we check whether the Timediff of the current row is greater than 60 or not.
IF it is the person's current Groupid is incremented (
$persons[CURRENT_PERSON] += 1 )
After the IF statement the current rowid's Groupid is changed to
$persons[CURRENT_PERSON] if that one exists.

As result we should get an array $rowids with the Groupid for each array.

So simply loop through that array and have the database SET the
Groupid somehow like this ..

EXAMPLE
<?php
foreach ($rowids as $row => $group) {
echo <<<SQL
UPDATE transitional_records SET Groupid = $group WHERE Rowid = $row;

SQL;
}
?>

I hope I could help a little
Be more specific next time

//A yeti

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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux