{"id":1154,"date":"2010-08-11T16:51:30","date_gmt":"2010-08-11T15:51:30","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=1154"},"modified":"2010-08-11T16:51:30","modified_gmt":"2010-08-11T15:51:30","slug":"cs71-ass1-finance-part-5-view-users-details-buy-stock-change-password","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/08\/11\/cs71-ass1-finance-part-5-view-users-details-buy-stock-change-password\/","title":{"rendered":"CS71 &#8211; Ass1 &#8211; Finance &#8211; Part 5 &#8211; View users details &#8211; buy stock &#8211; change password"},"content":{"rendered":"<p><span id=\"zipfile\"><a href=\"http:\/\/www.codingfriends.com\/wp-content\/uploads\/2010\/08\/cs75-ass1.zip\"><\/a><\/span>I am doing the <a href=\"http:\/\/www.harvard.edu\/\">Harvards<\/a> building dynamic websites called <a href=\"http:\/\/www.cs75.net\/\">CS-75<\/a> (also could be called E-75), because someone told me about it and I just thought might as well, it is all learning \ud83d\ude42 even if allot of it you may already know.<\/p>\n<p>As from the <a href=\"http:\/\/www.codingfriends.com\/index.php\/2010\/08\/11\/cs71-ass1-finance-part-2-login-register-forgotten-password\/\">previous post<\/a>, most of this page is in similar in nature it is just displaying data to the user that was built up from the classes files.<\/p>\n<p>So to buy some stock, what this page will do is wait for the user to type in a stock symbol and then use the stock class to get the data back from that symbol and then have another input that will allow the user to buy some of that stock, also if the user clicks on the link it will display at the bottom of the page new items about the stock itself.<\/p>\n<pre lang=\"php\">\r\n\r\n<?php\r\n\trequire(\"functions_start.php\");\r\n\t\r\n\tglobal $theStock;\r\n\t\r\n\tif (!isset($_SESSION[\"authenticated\"]) || $_SESSION[\"authenticated\"]== false)\r\n\t{\r\n\t\theader(\"Location: \/index.php\");\r\n\t\texit;\r\n\t}\r\n\t\r\n\t\/\/ of course the product price may change from searching to buying, depending on the time period so need to pull back \r\n\t\/\/ the new value, just incase.\r\n\tif (isset($_REQUEST[\"buyme\"]))\r\n\t{\r\n\t\t$stockID = strtoupper($_REQUEST[\"STOCK\"]);\r\n\t\t$stockAmount = $_REQUEST[\"AMOUNT\"];\r\n\t\t$stockPurchased = $theStock->BuyStock($_SESSION[\"username\"], $stockID, $stockAmount);\r\n\t}\r\n\t\r\n\tif (isset($_REQUEST[\"searchSymbol\"]))\r\n\t{\r\n\t\tif (strlen($_REQUEST[\"searchSymbol\"]) >=3)\r\n\t\t\t$result = $theStock->GetStocksFromYahoo(strtoupper($_REQUEST[\"searchSymbol\"]));\r\n\t}\r\n\r\n\tHTMLHeader(\"Buy some stock, search and buy\");\r\n\r\n\tif (isset ($stockPurchased)) {\r\n\t\techo \"<p class=\\\"details\\\">\";\r\n\t\tif ($stockPurchased >0 )\r\n\t\t\techo \"Stock was purchased ($stockID amount of $StockAmount total price of $stockPurchased each price was \". number_format($stockPurchased \/ $stockAmount,2).\")\"; \r\n\t\telse\r\n\t\t\techo \"Problem with purchasing the stock, please email the error above to the support team\";\r\n\t\techo \"<\/p>\";\r\n\t}\r\n?>\r\n<form action=\"<?php echo $_SERVER[\"PHP_SELF\"]; ?>\" method=\"post\" onsubmit=\"return CheckBuy()\"\/>\r\n<?php\r\nif (isset($result)) { \r\n  if ($result > 0) {\r\n\techo \"<h2>{$_REQUEST[\"searchSymbol\"]}<\/h2>\";\r\n    echo \"The price of {$_REQUEST[\"searchSymbol\"]} is $result, do you want to buy ?<br\/>Please enter how many and tick the box.<br\/>\";\r\n\techo \"<input type=\\\"text\\\" name=\\\"AMOUNT\\\" id=\\\"AMOUNT\\\" onkeyup=\\\"javascript:CheckKey(this)\\\"\/>\";\r\n\techo \"<input type=\\\"hidden\\\" name=\\\"STOCK\\\" value=\\\"{$_REQUEST[\"searchSymbol\"]}\\\"\/>\";\r\n\techo \"<input type=\\\"checkbox\\\" name=\\\"buyme\\\" id=\\\"BUYME\\\"\/>\";\r\n\techo \"<input type=\\\"submit\\\" value=\\\"Submit\\\"\/>\";\r\n\techo \"<p><b>Or search for another stock<\/b><\/p>\";\r\n }\r\nelse\r\n    echo \"<h2>Not stock of that symbol - {$_REQUEST[\"searchSymbol\"]}<\/h2>\";\r\n}\r\n?>\r\n<br\/>\r\nSearch Symbol :<input type=\"text\" name=\"searchSymbol\" id=\"searchSymbol\"\/>\r\n<input type=\"submit\" value=\"Submit\"\/>\r\n<\/form>\r\n<h2>Your present cash flow is<\/h2>\r\n<?php\r\n\techo number_format($theUser->GetCash($_SESSION[\"username\"]),2);\r\n\t\r\n\tHTMLFooter();\r\n?>\r\n<\/pre>\n<p>To view the stock details of the user, I just display the stock that the user has from data once again from the stock class, which also includes the current price of the stock.  There is a checkbox that will allow the user to sell some of there stock (if they have any!!) and once again use the stock class to sell the stock and update the tables in the database.<\/p>\n<pre lang=\"php\">\r\n<?php\r\n\trequire(\"functions_start.php\");\r\n\t\r\n\tglobal $theStock;\r\n\t\r\n\tif (!isset($_SESSION[\"authenticated\"]) || $_SESSION[\"authenticated\"]== false)\r\n\t{\r\n\t\theader(\"Location: \/index.php\");\r\n\t\texit;\r\n\t}\r\n\tforeach ($_REQUEST as $key => $value)\r\n\t{\r\n\t\tif (strpos($key, \"remove_\") === 0)\r\n\t\t\t$theStock->SellStock($_SESSION[\"username\"], $value);\r\n\t\tif ($key==\"stock\")\r\n\t\t\t$getStock = $value;\r\n\t}\r\n\tHTMLHeader(\"The details of your account\");\r\n?>\r\n<h2>The stocks that the user has<\/h2>\r\n<form action=\"<?php echo $_SERVER[\"PHP_SELF\"];?>\" method=\"post\" onsubmit=\"return checkSell()\">\r\n<table>\r\n<tr><td>Stock Name<\/td><td>Quantity<\/td><td>Price of Stock<\/td><td>Value<\/td><td>Sell<\/td><\/tr>\r\n<?php\r\n    $totalValue = 0;\r\n\t$usersStock = $theStock->ReturnAllStocks($_SESSION[\"username\"]);\r\n\tforeach ($usersStock as $theStocks)\r\n\t{\r\n\t\techo \"<tr><td><a href=\\\"{$_SERVER[\"PHP_SELF\"]}?stock={$theStocks[0]}\\\">{$theStocks[0]}<\/a><\/td><td>{$theStocks[1]}<\/td><td>{$theStocks[2]}<\/td><td>\".\r\n\t\t\tnumber_format($theStocks[2] * $theStocks[1],2).\"<\/td><td><input type=\\\"checkbox\\\" value=\\\"{$theStocks[0]}\\\" name=\\\"remove_{$theStocks[0]}\\\"\/><\/td><\/tr>\";\r\n\t\t$totalValue += $theStocks[2] * $theStocks[1];\r\n\t}\r\n\techo \"<tr><td colspan=\\\"2\\\"><\/td><td>Total value<\/td><td>\".number_format($totalValue,2).\"<\/td><\/tr>\";\r\n?>\r\n<\/table>\r\n<input id=\"center\" type=\"submit\" value=\"Submit\"\/>\r\n<\/form>\r\n<h2>Your present cash flow is<\/h2>\r\n<?php\r\n\techo number_format($theUser->GetCash($_SESSION[\"username\"]),2);\r\n\techo \"<h2>Your cash + investments<\/h2>\" . number_format($theUser->GetCash($_SESSION[\"username\"]) + $totalValue,2);\r\n\t\r\n\tif (isset($getStock))\r\n\t{\r\n\t\techo \"<h2>$getStock more information<\/h2><table><tr><td>Date<\/td><td>Title\/Link<\/td><\/tr>\";\r\n\t\t$stockDetailsArray = $theStock->ArrayOfStockDetails($getStock);\r\n\t\tfor ($i = 0; $i < sizeof($stockDetailsArray); $i++)\r\n\t\t{\r\n\t\t\techo \"<tr><td>{$stockDetailsArray[$i][\"Date\"]}<\/td><td><a href=\\\"{$stockDetailsArray[$i][\"Link\"]}\\\" target=\\\"_blank\\\">{$stockDetailsArray[$i][\"Title\"]}<\/a>\";\r\n\t\t}\r\n\t\techo \"<\/table>\";\r\n\t}\r\n\r\n\tHTMLFooter();\r\n?><\/pre>\n<p>The last part is the change of the password, which just will use the user class to update the users password to the requested input from the change password page.<\/p>\n<pre lang=\"php\">\r\n<?php\r\n\trequire(\"functions_start.php\");\r\n\t\r\n\tif (isset($_REQUEST[\"password1\"]) &#038;&#038; isset($_REQUEST[\"password2\"]))\r\n\t{\r\n\t\tif ($_REQUEST[\"password1\"] == $_REQUEST[\"password2\"])\r\n\t\t\t$error = $theUser->ChangePassword($_SESSION[\"username\"], $_REQUEST[\"password1\"]);\r\n\t\telse\r\n\t\t\t$notSamePassword = true;\r\n\t}\r\n\r\n\tif (!isset($_SESSION[\"authenticated\"]))\r\n\t{\r\n\t\theader(\"Location: \/index.php\");\r\n\t\texit;\r\n\t}\r\n\r\n\tHTMLHeader(\"Change password\",true);\r\n\r\n\tif (isset($error))\r\n\t{\r\n\t\tif ($error)\r\n\t\t\techo \"<div>Password updated<\/div>\";\r\n\t\telse \r\n\t\t\techo \"<div id=\\\"error\\\">Cannot update the password, please contact the system admin<\/div>\";\r\n\t}\r\n\tif (isset($notSamePassword))\r\n\t\techo \"<div>Passwords are not the same<\/div>\";\r\n?>\r\n<!-- taken from http:\/\/www.html.it\/articoli\/nifty\/index.html-->\r\n<div id=\"login\">\r\n<b class=\"rtop\">\r\n  <b class=\"r1\"><\/b> <b class=\"r2\"><\/b> <b class=\"r3\"><\/b> <b class=\"r4\"><\/b>\r\n<\/b>\r\n<form method=\"post\" action=\"<?php echo $_SERVER[\"PHP_SELF\"]; ?>\" onsubmit=\"return CheckChangePasswords()\">\r\nChange your password <p>\r\nNew Password : <input type=\"password\" name=\"password1\" id=\"password1\" value=\"\"\/><\/p>\r\nRetype New Password : <input type=\"password\" name=\"password2\" id=\"password2\" value=\"\"\/><\/p>\r\n<input type=\"submit\"\/>\r\n<\/p>\r\n<\/form>\r\n<b class=\"rbottom\">\r\n  <b class=\"r4\"><\/b> <b class=\"r3\"><\/b> <b class=\"r2\"><\/b> <b class=\"r1\"><\/b>\r\n<\/b>\r\n<\/div>\r\n<?php\r\n\tHTMLFooter();\r\n?><\/pre>\n<p>The actual pages that are displayed to the user are all very basic as them selves, but the actual logic and formula are all within the classes that is the gold dust as such, since it is one point of checking and also one file to update if there is a update to the yahoo etc stocks details, because would have to update the different pages that display the stocks and that is not good for testing and also updating.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am doing the Harvards building dynamic websites called CS-75 (also could be called E-75), because someone told me about it and I just thought might as well, it is all learning \ud83d\ude42 even if allot of it you may already know. As from the previous post, most of this page is in similar in &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/08\/11\/cs71-ass1-finance-part-5-view-users-details-buy-stock-change-password\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">CS71 &#8211; Ass1 &#8211; Finance &#8211; Part 5 &#8211; View users details &#8211; buy stock &#8211; change password<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[246,249],"class_list":["post-1154","post","type-post","status-publish","format-standard","hentry","category-php","tag-assignment-1","tag-cs75"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1154","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=1154"}],"version-history":[{"count":1,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1154\/revisions"}],"predecessor-version":[{"id":1157,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1154\/revisions\/1157"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=1154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=1154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=1154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}