Jhunter Posted May 23, 2009 Posted May 23, 2009 Hi every one, i am using code that i found here on CE, and i am wondering if anyone can help me to add a limit on how many stocks you can buy in total. i have spent hours trying to work it out so far, and it is really getting on my nerves. i am not the best coder, so thats why i am asking for your help I was a member on here before i posted this but i forgot all my log in details and the email i used to sign up. my old profile id John1, so i am not a noob that is looking for free help, i will thank you, and i might be able to help you some day. Here is the code <?php include_once('globals.php'); echo ' <style language="text/css"> .red { color: red; } .green { color: green; } .stock td { border: 1px black solid; } .stock th { border: 1px black solid; } </style> <h2>Stock Market</h2>'; $arrs = array('buy','log','sell','view'); $x = (isset($_GET['trip']) && ctype_alnum($_GET['trip']) && in_array($_GET['trip'], $arrs)) ? trim($_GET['trip']) : FALSE; # False making it not exist making it default page. switch($x) { case 'buy': buy_stock(); break; case 'sell': sell_stock(); break; case 'view': view_stock(); break; case 'log': view_stock_log(); break; default: stock_default(); break; } function view_stock_log() { global $ir,$h; $log = mysql_query("SELECT recordTIME,recordTEXT FROM `stock_records` WHERE `recordUSER` = ".$ir['userid']." ORDER BY `recordTIME` DESC") or die(mysql_error()); if(mysql_num_rows($log) == 0) { echo 'You do not have any log records.'; } else { while($soc = mysql_fetch_assoc($log)) { $time = date('F jS Y g:i:s', $soc['recordTIME']); echo '[b]'.$time.'[/b] '.str_replace('me', 'You', $soc['recordTEXT']).' '; } } } function sell_stock() { global $ir,$h; $id = abs(@intval($_GET['ID'])); $holding = mysql_query("SELECT holdingID,holdingSTOCK,holdingQTY FROM `stock_holdings` WHERE `holdingID` = ".$id." AND `holdingUSER` = ".$ir['userid']) or die(mysql_error()); if(mysql_num_rows($holding) == 0) { echo 'This holding ID does not exist, or you do not own it.'; } else { $soc = mysql_fetch_assoc($holding); $stock = mysql_query("SELECT stockNAME,stockNPRICE FROM `stock_stocks` WHERE `stockID` = ".$soc['holdingSTOCK']) or die(mysql_error()); $stock = mysql_fetch_assoc($stock); $tot = ($stock['stockNPRICE'] * $soc['holdingQTY']); mysql_query("DELETE FROM `stock_holdings` WHERE `holdingID` = ".$soc['holdingID']); mysql_query("INSERT INTO `stock_records` (`recordUSER`,`recordTIME`,`recordTEXT`) VALUES (".$ir['userid'].",unix_timestamp(),'me sold ".$soc['holdingQTY']." of stock ".$stock['stockNAME']." for ".money_formatter($tot)."')") or die(mysql_error()); mysql_query("UPDATE `users` SET `money` = (`money` + ".$tot.") WHERE `userid` = ".$ir['userid']) or die(mysql_error()); echo 'You sold '.number_format($soc['holdingQTY']).' stock of '.$stock['stockNAME'].' for '.money_formatter($tot).'!'; } } function view_stock() { global $ir,$h; $holdings = mysql_query("SELECT holdingID,holdingSTOCK,holdingQTY FROM `stock_holdings` WHERE `holdingUSER` = ".$ir['userid']) or die(mysql_error()); if(mysql_num_rows($holdings) == 0) { echo 'You do not hold any shares in any stocks atm.'; } else { echo ' <table border="0" cellspacing="0" cellpadding="0" class="stocks" width="50%"> <tr> <th>Stock Name</th> <th>Shares</th> <th>Sell Price</th> <th>Sell</th> </tr>'; while($soc = mysql_fetch_assoc($holdings)) { $stock = mysql_query("SELECT stockNAME,stockNPRICE FROM `stock_stocks` WHERE `stockID` = ".$soc['holdingSTOCK']) or die(mysql_error()); $stock = mysql_fetch_assoc($stock); echo ' <tr> <td>'.$stock['stockNAME'].'</td> <td>'.number_format($soc['holdingQTY']).'</td> <td>'.money_formatter($stock['stockNPRICE'] * $soc['holdingQTY']).'</td> <td>[url="'.$_SERVER['PHP_SELF'].'?trip=sell&ID='.$soc['holdingID'].'"]Sell Stock[/url]</td> </tr>'; } echo ' </table>'; } } function buy_stock() { global $ir,$h; $id = abs(@intval($_GET['ID'])); $rows = mysql_query("SELECT stockID FROM `stock_stocks` WHERE `stockID` = ".$id) or die(mysql_error()); if(mysql_num_rows($rows) == 0) { echo 'This stock does not exist.'; exit($h->endpage()); } if(isset($_POST['buy'])) { $amnt = abs(@intval($_POST['amnt'])); $fetch_stock = mysql_query("SELECT stockID,stockNPRICE,stockNAME FROM `stock_stocks` WHERE `stockID` = ".$id) or die(mysql_error()); $stock = mysql_fetch_assoc($fetch_stock); $mprice = ($stock['stockNPRICE'] * $amnt); if($ir['money'] <= $mprice) { echo 'You do not have enough money for this amount of stocks.'; exit($h->endpage()); } $stock_holds = mysql_query("SELECT holdingID FROM `stock_holdings` WHERE `holdingUSER` = ".$ir['userid']." AND `holdingSTOCK` = ".$stock['stockID']) or die(mysql_error()); if(mysql_num_rows($stock_holds)) { mysql_query("UPDATE `stock_holdings` SET `holdingQTY` = (`holdingQTY` + ".$amnt.") WHERE `holdingUSER` = ".$ir['userid']." AND `holdingSTOCK` = ".$stock['stockID']) or die(mysql_error()); } else { mysql_query("INSERT INTO `stock_holdings` (`holdingUSER`,`holdingSTOCK`,`holdingQTY`) VALUES (".$ir['userid'].",".$stock['stockID'].",".$amnt.")") or die(mysql_error()); } mysql_query("UPDATE `users` SET `money` = (`money` - ".$mprice.") WHERE `userid` = ".$ir['userid']); mysql_query("INSERT INTO `stock_records` (`recordUSER`,`recordTIME`,`recordTEXT`) VALUES (".$ir['userid'].",unix_timestamp(),'me bought ".$amnt." of stock ".$stock['stockNAME']." for ".money_formatter($mprice)."')") or die(mysql_error()); echo 'You successfuly bought '.number_format($amnt).' stocks of '.$stock['stockNAME'].'.'; } else { $stock = mysql_query("SELECT stockID,stockNAME,stockCHANGE,stockNPRICE,stockUD,stockOPRICE FROM `stock_stocks` WHERE `stockID` = ".$id) or die(mysql_error()); $stock = mysql_fetch_assoc($stock); $symb = ($stock['stockUD'] == 1) ? '<big>?</big>' : '<big>?</big>'; $class = ($stock['stockUD'] == 1) ? 'green' : 'red'; $blip = ($stock['stockUD'] == 1) ? '+' : '-'; $clor = ($stock['stockOPRICE'] < $soc['stockNPRICE']) ? 'green' : 'red'; echo ' <table border="0" cellspacing="0" width="50%"> <tr> <td>Stock Name</td> <td>'.$stock['stockNAME'].'</td> </tr> <tr> <td>Price Change</td> <td><span class="'.$class.'">'.$blip.' '.money_formatter($stock['stockCHANGE']).' '.$symb.'</span></td> </tr> <tr> <td>Original Price</td> <td>'.money_formatter($stock['stockOPRICE']).'</td> </tr> <tr> <td>Now Price</td> <td>'.money_formatter($stock['stockNPRICE']).'</td> </tr> <tr> <td>Buy Stock</td> <td> <form action="'.$_SERVER['PHP_SELF'].'?trip=buy&ID='.$id.'" method="post"> QTY: <input type="text" name="amnt" value="0" /> <input type="submit" value="Buy" name="buy" /> </form> </td> </tr> </table>'; } } function stock_default() { global $ir,$h; echo ' Here is a list of stocks available along with their price and such. -> [url="'.$_SERVER['PHP_SELF'].'?trip=view"]View your stocks[/url] -> [url="'.$_SERVER['PHP_SELF'].'?trip=log"]View your log[/url] <table border="0" cellspacing="0" cellpadding="0" class="stock" width="70%"> <tr> <th>Stock Name</th> <th>Stock Orig Price</th> <th>Stock Now Price</th> <th>Price Change</th> <th>View</th> </tr>'; $stocks = mysql_query("SELECT stockID,stockNAME,stockOPRICE,stockNPRICE,stockCHANGE,stockUD FROM `stock_stocks`") or die(mysql_error()); if(mysql_num_rows($stocks) == 0) { echo ' <tr> <td align="center" colspan="5">No stocks available</td> </tr> </table>'; exit($h->endpage()); } while($soc = mysql_fetch_assoc($stocks)) { $symb = ($soc['stockUD'] == 1) ? '?' : '?'; $class = ($soc['stockUD'] == 1) ? 'green' : 'red'; $blip = ($soc['stockUD'] == 1) ? '+' : '-'; $clor = ($soc['stockOPRICE'] < $soc['stockNPRICE']) ? 'green' : 'red'; $blic = ($soc['stockOPRICE'] < $soc['stockNPRICE']) ? '+' : '-'; echo ' <tr> <td>'.$soc['stockNAME'].'</td> <td>'.money_formatter($soc['stockOPRICE']).'</td> <td><span class="'.$clor.'">'.$blic.' '.money_formatter($soc['stockNPRICE']).'</span></td> <td><span class="'.$class.'">'.$blip.' '.money_formatter($soc['stockCHANGE']).' '.$symb.'</span></td> <td>[url="'.$_SERVER['PHP_SELF'].'?trip=buy&ID='.$soc['stockID'].'"]Buy Or View[/url]</td> </tr>'; } echo ' </table>'; } $h->endpage(); ?> Quote
gideon prewett Posted May 23, 2009 Posted May 23, 2009 Re: Stock market limit im not sure how to do this but it shouldnt be hard my 2 pence this mod looks like haunted dawgs but without a bit of the file /* Start Copy Right Notice! */ /* # You do not re-distribute my files. # You do not claim this as your work. # You do not ask for money to other people for my work. # This is free not paid. # Leaving the copyright is a must (i know alot of you will just take it down :/) # All the work under this file is by written consent kyle mulder's and shall be in tact at all times! # A description of this file is bellow: This is a stock market, a working one, a non buggy one, a non exploitable one, a better one, more feature. Users can buy stocks and every 5 minutes the stock rate will change (like real life). Gives the users something to do. The stocks can crash every 5 minutes making everyone loose there investment and there cash. There will be a .15% that a stock can crash, more likely to happen quite often, but not all the time. If there is a possibility that a stock can crash every 5 minute's. That makes it: # 5 minutes => 1 Crash # 1 Hour => 12 Crashes # 1 Day => 288 Crashes The above is possibilites, does not mean it WILL always happen. Anyways lets get on with the file :) */ /* End Copy Right Notice! */ if your going to use peoples mods keep the copyright on its respectful to the person who made it Quote
Jhunter Posted May 23, 2009 Author Posted May 23, 2009 Re: Stock market limit i took it out when i added it to my game, just to keep file sizes down Quote
pennystock6 Posted September 19, 2011 Posted September 19, 2011 There is an option "Limit" which can be very handy and great stops in time if you want to get more specific with your system. If you want to buy or sell shares at a specified price, when you want to use the option "Limit". Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.