<?php
// (C) 2000 LinuxFutures.com, Inc.
//
// Part of UPmarkets - the software behind the LinuxFutures game site
// visit www.LinuxFutures.com to play
//
// We are making the software available to you under the GNU General Public License,
// a copy of which should accompany this software in the file LICENSE.
//
// A web-based copy of the LICENSE is located at http://www.gnu.org/copyleft/gpl.txt
//
// While there is no charge for using this software, there are important restrictions.
//
// This LICENSE does not allow, in general, proprietary modifications or proprietary derivative works.
//
// This software carries ABSOLUTELY NO WARRANTY. It is offered AS-IS, and may not work at all
// or may have harmful or hazardous side effects.
//
//
// -------------------- what this file does ---------------------
// profides function user_holdings_array()
// from table LEDGERS, calculates total holdings of each held symbol for a single userid
// oddity: symbols never held will be undef, symbols held but zeroed out will be zeros.
//
// If you are using these numbers for something important,
// you should LOCK tables ledgers or they might change on you!
// Because amounts are calculated aggregates, you can't commit the
// classic textbook problem of 2 people updating holdings and making
// stuff appear or disappear from/to nowhere.
// BUT, if you are calling the function more than once, or using
// the data to decide elgibility for trading, debt/equity ratios,
// or even trying to aggregate pretty functions over users, watch out.
function user_holdings_array($userid) {
global $db_link;
$u = (int) $userid; // better safe than sorry
if($u <= 0){ echo "<br>error: f_user_holdings_array called with userid <= 0 <br>"; exit(); }
$sql = "SELECT sum(amount) as holdings, symbol from ledgers, symbols ";
$sql .= " WHERE ledgers.userid = '".$u."' AND symbols.symbolid = ledgers.symbolid GROUP BY symbol ORDER BY symbols.symbolid ASC;";
$q = mysql_query($sql, $db_link);
mysql_error_sanity("f_user_holdings_array"); // this exits on sql errors
$n_rows = mysql_num_rows($q);
$holdings = array();
for($i_row = 0; $i_row < $n_rows; $i_row++){
$holding = mysql_fetch_object($q);
$holdings[$holding->symbol] = $holding->holdings;
}
mysql_free_result($q);
return($holdings);
}