📄 quote.class.php
字号:
<?php/** PHP STOCK TRACKER* -----------------* A multi-user utility for creating and managing stock* watchlists and historical analysis.* Copyright (C) 2005 Joshua Eldridge (joshuae74@hotmail.com)*** This program is free software; you can redistribute it and/or modify* it under the terms of the GNU General Public License as published by* the Free Software Foundation; either version 2 of the License, or* (at your option) any later version.** This program is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the* GNU General Public License for more details.** You should have received a copy of the GNU General Public License* along with this program; if not, write to the Free Software* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA** See the COPYING file for full details.*/class Quote {/* Instantiate the class by passing in the ticker symbolfor new quote objects, it will be blank. If that is the case, the stock_id will be set to -1 -- Otherwise call theinternal _init() function to load information about the stock*/ function Quote($stock_symbol) { $this->stock_symbol = $stock_symbol; if(trim($this->stock_symbol) != '') { $this->_init($this->stock_symbol); } else { $this->stock_id = -1; } } function _init($ticker) { global $conn; $sql = 'SELECT stock_id, stock_name, stock_exchange, stock_date_added FROM stocks_info WHERE stock_symbol="' . $ticker . '"'; $result = $conn->GetRow($sql); if( $result[0] == 0 ) { $this->stock_id = -1; } else { $this->stock_id = $result[0]; $this->stock_name = $result[1]; $this->stock_exchange = $result[2]; $this->stock_date_added = $result[3]; } } function is_new() { if ($this->stock_id == -1 || trim($this->stock_id) == '') { return true; } else { return false; } } function is_valid() { $this->query_yahoo(); if($this->error != '') { return false; } else { return true; } } function add_to_list($user_id) { global $conn; $sql = 'INSERT INTO stocks_watchlists (stock_id, user_id, date_added) VALUES(' . $this->stock_id . ', ' . $user_id . ',NOW())'; $conn->Execute($sql); } function query_yahoo() { $fp = fopen("http://quote.yahoo.com/d/quotes.csv?s=" . $this->stock_symbol . "&f=sl1d1t1c1ohgv&e=.csv", "r"); $data = fread($fp, 2000); fclose($fp); $data = str_replace("\"", "", $data); $data = explode(",", $data); if ( $data[1] == 0 ) { $this->error = 'Could not find ticker symbol ' . $this->stock_symbol . '!'; return; } else { $this->error = ''; } $this->last_trade = $data[1]; $this->trade_date = date("Y-n-j", strtotime($data[2])); $this->trade_time = $data[3]; $this->pct_change = $data[4]; $this->opened = $data[5]; $this->high = $data[6]; $this->low = $data[7]; $this->volume = $data[8]; $this->chart_url = "http://ichart.yahoo.com/t?s=" . $this->stock_symbol; } function load_data($date = -1) { global $conn; if($date == -1) { $sql = 'SELECT max(trade_date) FROM stocks_data WHERE stock_id=' . $this->stock_id; $date = $conn->GetOne($sql); } $this->trade_date = $date; $sql = 'SELECT last_trade, pct_change, opened, low, high, volume FROM stocks_data WHERE stock_id=' . $this->stock_id . ' AND trade_date="' . $this->trade_date . '"'; $date = $conn->GetCol($sql); $this->last_trade = $result[0]; $this->pct_change = $result[1]; $this->opened = $result[2]; $this->low = $result[3]; $this->high = $result[4]; $this->volume = $result[5]; $conn->Close(); } function save_info() { global $conn; $record = array(); if ($this->is_new()) { $dummySql = "SELECT * FROM stocks_info WHERE 1 = 2"; $qryFunc = "GetInsertSQL"; } else { $dummySql = "SELECT * FROM stocks_info WHERE stock_id = " . $this->stock_id; $qryFunc = "GetUpdateSQL"; $record['stock_id'] = $this->stock_id; } $rs = $conn->Execute($dummySql); $record['stock_symbol'] = $this->stock_symbol; $record['stock_name'] = $this->stock_name; $record['stock_exchange'] = $this->stock_exchange; $record['stock_date_added'] = date('Y-m-d'); print_r($record); $dbQueryString = $conn->$qryFunc($rs,$record); print($dbQueryString); if ($conn->Execute($dbQueryString) === false) { die("An Error Occurred: " . $conn->ErrorMsg()); } } /* We don't need to check is_new() for this function, as we already know that the stocks exist in the database. Also, this should never be an update, this gets called once per day and records history (or is used for batch history loads */ function save_data() { global $conn; /* Note: This function will NOT die on invalid values, any records with N/A or NULL values will be discarded. This is so we can process batch input. */ $record = array(); $record['stock_id'] = $this->stock_id; $record['last_trade'] = $this->last_trade; $record['trade_date'] = $this->trade_date; $record['opened'] = $this->opened; $record['low'] = $this->low; $record['high'] = $this->high; $record['volume'] = $this->volume; $empty_rs = "SELECT * FROM stocks_data WHERE 1 = 2"; $rs = $conn->Execute($empty_rs); $insert_sql = $conn->GetInsertSQL($rs, $record); if($conn->Execute($insert_sql) === false) { //die("An Error Occurred: " . $conn->ErrorMsg()); } } function update_data() { global $conn; $sql = 'UPDATE stocks SET symbol="' . $this->stock_symbol . '", stock_name="' . $this->stock_name .'", target=' . $this->target . ', date_added=NOW() WHERE id=' . $this->stock_id; $conn->Execute($sql); } function delete_stock($user_id) { global $conn; $sql = 'DELETE FROM stocks_watchlists WHERE stock_id=' . $this->stock_id . ' AND user_id = ' . $user_id; $conn->Execute($sql); $count = check_watchlists($this->stock_id); if($count == 0) { $sql = 'DELETE FROM stocks_data WHERE stock_id=' . $this->stock_id; $conn->Execute($sql); } }}?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -