CS71 – Ass1 – Finance – Part 5 – View users details – buy stock – change password

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 🙂 even if allot of it you may already know.

As from the previous post, 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.

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.

 
<?php
	require("functions_start.php");
 
	global $theStock;
 
	if (!isset($_SESSION["authenticated"]) || $_SESSION["authenticated"]== false)
	{
		header("Location: /index.php");
		exit;
	}
 
	// of course the product price may change from searching to buying, depending on the time period so need to pull back 
	// the new value, just incase.
	if (isset($_REQUEST["buyme"]))
	{
		$stockID = strtoupper($_REQUEST["STOCK"]);
		$stockAmount = $_REQUEST["AMOUNT"];
		$stockPurchased = $theStock->BuyStock($_SESSION["username"], $stockID, $stockAmount);
	}
 
	if (isset($_REQUEST["searchSymbol"]))
	{
		if (strlen($_REQUEST["searchSymbol"]) >=3)
			$result = $theStock->GetStocksFromYahoo(strtoupper($_REQUEST["searchSymbol"]));
	}
 
	HTMLHeader("Buy some stock, search and buy");
 
	if (isset ($stockPurchased)) {
		echo "<p class=\"details\">";
		if ($stockPurchased >0 )
			echo "Stock was purchased ($stockID amount of $StockAmount total price of $stockPurchased each price was ". number_format($stockPurchased / $stockAmount,2).")"; 
		else
			echo "Problem with purchasing the stock, please email the error above to the support team";
		echo "</p>";
	}
?>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" onsubmit="return CheckBuy()"/>
<?php
if (isset($result)) { 
  if ($result > 0) {
	echo "<h2>{$_REQUEST["searchSymbol"]}</h2>";
    echo "The price of {$_REQUEST["searchSymbol"]} is $result, do you want to buy ?<br/>Please enter how many and tick the box.<br/>";
	echo "<input type=\"text\" name=\"AMOUNT\" id=\"AMOUNT\" onkeyup=\"javascript:CheckKey(this)\"/>";
	echo "<input type=\"hidden\" name=\"STOCK\" value=\"{$_REQUEST["searchSymbol"]}\"/>";
	echo "<input type=\"checkbox\" name=\"buyme\" id=\"BUYME\"/>";
	echo "<input type=\"submit\" value=\"Submit\"/>";
	echo "<p><b>Or search for another stock</b></p>";
 }
else
    echo "<h2>Not stock of that symbol - {$_REQUEST["searchSymbol"]}</h2>";
}
?>
<br/>
Search Symbol :<input type="text" name="searchSymbol" id="searchSymbol"/>
<input type="submit" value="Submit"/>
</form>
<h2>Your present cash flow is</h2>
<?php
	echo number_format($theUser->GetCash($_SESSION["username"]),2);
 
	HTMLFooter();
?>

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.

<?php
	require("functions_start.php");
 
	global $theStock;
 
	if (!isset($_SESSION["authenticated"]) || $_SESSION["authenticated"]== false)
	{
		header("Location: /index.php");
		exit;
	}
	foreach ($_REQUEST as $key => $value)
	{
		if (strpos($key, "remove_") === 0)
			$theStock->SellStock($_SESSION["username"], $value);
		if ($key=="stock")
			$getStock = $value;
	}
	HTMLHeader("The details of your account");
?>
<h2>The stocks that the user has</h2>
<form action="<?php echo $_SERVER["PHP_SELF"];?>" method="post" onsubmit="return checkSell()">
<table>
<tr><td>Stock Name</td><td>Quantity</td><td>Price of Stock</td><td>Value</td><td>Sell</td></tr>
<?php
    $totalValue = 0;
	$usersStock = $theStock->ReturnAllStocks($_SESSION["username"]);
	foreach ($usersStock as $theStocks)
	{
		echo "<tr><td><a href=\"{$_SERVER["PHP_SELF"]}?stock={$theStocks[0]}\">{$theStocks[0]}</a></td><td>{$theStocks[1]}</td><td>{$theStocks[2]}</td><td>".
			number_format($theStocks[2] * $theStocks[1],2)."</td><td><input type=\"checkbox\" value=\"{$theStocks[0]}\" name=\"remove_{$theStocks[0]}\"/></td></tr>";
		$totalValue += $theStocks[2] * $theStocks[1];
	}
	echo "<tr><td colspan=\"2\"></td><td>Total value</td><td>".number_format($totalValue,2)."</td></tr>";
?>
</table>
<input id="center" type="submit" value="Submit"/>
</form>
<h2>Your present cash flow is</h2>
<?php
	echo number_format($theUser->GetCash($_SESSION["username"]),2);
	echo "<h2>Your cash + investments</h2>" . number_format($theUser->GetCash($_SESSION["username"]) + $totalValue,2);
 
	if (isset($getStock))
	{
		echo "<h2>$getStock more information</h2><table><tr><td>Date</td><td>Title/Link</td></tr>";
		$stockDetailsArray = $theStock->ArrayOfStockDetails($getStock);
		for ($i = 0; $i < sizeof($stockDetailsArray); $i++)
		{
			echo "<tr><td>{$stockDetailsArray[$i]["Date"]}</td><td><a href=\"{$stockDetailsArray[$i]["Link"]}\" target=\"_blank\">{$stockDetailsArray[$i]["Title"]}</a>";
		}
		echo "</table>";
	}
 
	HTMLFooter();
?>

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.

<?php
	require("functions_start.php");
 
	if (isset($_REQUEST["password1"]) && isset($_REQUEST["password2"]))
	{
		if ($_REQUEST["password1"] == $_REQUEST["password2"])
			$error = $theUser->ChangePassword($_SESSION["username"], $_REQUEST["password1"]);
		else
			$notSamePassword = true;
	}
 
	if (!isset($_SESSION["authenticated"]))
	{
		header("Location: /index.php");
		exit;
	}
 
	HTMLHeader("Change password",true);
 
	if (isset($error))
	{
		if ($error)
			echo "<div>Password updated</div>";
		else 
			echo "<div id=\"error\">Cannot update the password, please contact the system admin</div>";
	}
	if (isset($notSamePassword))
		echo "<div>Passwords are not the same</div>";
?>
<!-- taken from http://www.html.it/articoli/nifty/index.html-->
<div id="login">
<b class="rtop">
  <b class="r1"></b> <b class="r2"></b> <b class="r3"></b> <b class="r4"></b>
</b>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>" onsubmit="return CheckChangePasswords()">
Change your password <p>
New Password : <input type="password" name="password1" id="password1" value=""/></p>
Retype New Password : <input type="password" name="password2" id="password2" value=""/></p>
<input type="submit"/>
</p>
</form>
<b class="rbottom">
  <b class="r4"></b> <b class="r3"></b> <b class="r2"></b> <b class="r1"></b>
</b>
</div>
<?php
	HTMLFooter();
?>

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.

Leave a Reply

Your email address will not be published. Required fields are marked *