2021-03-10

Mar 10 In-Class Exercise.

Please post your solutions to the Mar 10 In-Class Exercise to this thread.
Best,
Chris
Please post your solutions to the Mar 10 In-Class Exercise to this thread. Best, Chris
2021-03-11

-- Mar 10 In-Class Exercise
StoreGold.php
<?php $goldNum = 0;
if(isset($_POST['store'])) {
	if(file_exists("gold.txt")) 
        {
		$goldNum = file_get_contents("gold.txt");
                if(isset($_REQUEST['goldAmount']))
                {
                     $goldNum = $goldNum + $_REQUEST['goldAmount'];
                }
		file_put_contents("gold.txt", $goldNum);
	}
        else
        {
             file_put_contents("gold.txt",0);
        }
    
        header("Location:SafeStorage.php");
} ?>
<!DOCTYPE html> <html>
    <head>
    <meta charset="utf-8"/>
 	<title>Store Gold</title>
    </head>
    <body>
        <form method="post" action="SafeStorage.php">
            <label for="text"> Amount of Gold:</label>
            <input type="number" name="goldAmount" id="amount"/>
            <input type="submit" name='submit button' value='Put Somewhere Safe' />
        </form>
    </body>
</html>
SafeStorage.php
<!DOCTYPE html> <html>
    <head>
    <meta charset="utf-8"/>
 	<title>SafeStorage</title>
    </head>
    <body>
        <p>Total Gold Stored: 
            <?php
            if(!file_exists("gold.txt")){
                echo file_get_contents("gold.txt") 
            }
            ?>
        </p>
    </body>
</html>
(Edited: 2021-03-11)
StoreGold.php <?php $goldNum = 0; if(isset($_POST['store'])) { if(file_exists("gold.txt")) { $goldNum = file_get_contents("gold.txt"); if(isset($_REQUEST['goldAmount'])) { $goldNum = $goldNum + $_REQUEST['goldAmount']; } file_put_contents("gold.txt", $goldNum); } else { file_put_contents("gold.txt",0); } header("Location:SafeStorage.php"); } ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title>Store Gold</title> </head> <body> <form method="post" action="SafeStorage.php"> <label for="text"> Amount of Gold:</label> <input type="number" name="goldAmount" id="amount"/> <input type="submit" name='submit button' value='Put Somewhere Safe' /> </form> </body> </html> SafeStorage.php <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title>SafeStorage</title> </head> <body> <p>Total Gold Stored: <?php if(!file_exists("gold.txt")){ echo file_get_contents("gold.txt") } ?> </p> </body> </html>
2021-03-12

-- Mar 10 In-Class Exercise
StoreGold.php
<?php
if(isset($_REQUEST["numGold"])){
    if(!file_exists("gold.txt")){
      file_put_contents("gold.txt", 0);
    }//if
    else{}//else
    $numGold = $_REQUEST["numGold"]+file_get_contents("gold.txt");
    file_put_contents("gold.txt", $numGold); 
 
}//if
else{}//else
header("Location:SafeStorage.php"); 
 
?>
<!DOCTYPE html><html>
  <head>
      <title>exercise mar 10- store gold</title>
      <meta charset="utf-8" />
  </head>
  <body>
    <form method="post" action="">
      <label for="gold">Amount of Gold: </label>
      <input type="text" name="numGold" id="numGold" />
      <input type="submit" value="Put Somewhere Safe" />
    </form>
  </body>
</html> 
 
<?php 
 
SafeStorage.php 
 
<?php
$totalGold; 
 
if(file_exists("gold.txt")){
    $totalGold = file_get_contents("gold.txt");
}//if
else{}//else 
 
?>
<!DOCTYPE html><html>
  <head>
      <title>exercise mar 10- safe storage</title>
      <meta charset="utf-8" />
  </head>
  <body>
    <p> Total Gold Stored: <?php echo $totalGold ?></p>
  </body>
</html> 
 
<?php 
 
(Edited: 2021-03-12)
<pre> StoreGold.php <?php if(isset($_REQUEST["numGold"])){ if(!file_exists("gold.txt")){ file_put_contents("gold.txt", 0); }//if else{}//else $numGold = $_REQUEST["numGold"]+file_get_contents("gold.txt"); file_put_contents("gold.txt", $numGold); }//if else{}//else header("Location:SafeStorage.php"); ?> <!DOCTYPE html><html> <head> <title>exercise mar 10- store gold</title> <meta charset="utf-8" /> </head> <body> <form method="post" action=""> <label for="gold">Amount of Gold: </label> <input type="text" name="numGold" id="numGold" /> <input type="submit" value="Put Somewhere Safe" /> </form> </body> </html> <?php SafeStorage.php <?php $totalGold; if(file_exists("gold.txt")){ $totalGold = file_get_contents("gold.txt"); }//if else{}//else ?> <!DOCTYPE html><html> <head> <title>exercise mar 10- safe storage</title> <meta charset="utf-8" /> </head> <body> <p> Total Gold Stored: <?php echo $totalGold ?></p> </body> </html> <?php </pre>

-- Mar 10 In-Class Exercise
StoreGold.php
<?php
fopen("gold.txt", "a");
$gold = intval(file_get_contents("gold.txt"));
if (isset($_POST['AddButton'])) {
    $gold = $gold + intval($_POST['input']);
    file_put_contents("gold.txt", strval($gold));
    header("Location:./SafeStorage.php");
}
?> 
 
<!doctype html>
<html>
<head>
<title>Gold Storage</title>
</head>
<body>
<form method="post">
    <input type="text" name="input" placeholder="Amount of Gold" />
    <input type="submit" name="AddButton" value="Put Somewhere Safe" />
</form>   
</body>
</html>
SafeStorage.php
<?php
fopen("gold.txt", "a");
$gold = intval(file_get_contents("gold.txt"));
?> 
 
<!doctype html>
<html>
<head>
<title>Gold Storage</title>
</head>
<body>
    <p><?php echo "Total Gold Stored: ".$gold; ?></p>
</body>
</html>
StoreGold.php <pre> <?php fopen("gold.txt", "a"); $gold = intval(file_get_contents("gold.txt")); if (isset($_POST['AddButton'])) { $gold = $gold + intval($_POST['input']); file_put_contents("gold.txt", strval($gold)); header("Location:./SafeStorage.php"); } ?> <!doctype html> <html> <head> <title>Gold Storage</title> </head> <body> <form method="post"> <input type="text" name="input" placeholder="Amount of Gold" /> <input type="submit" name="AddButton" value="Put Somewhere Safe" /> </form> </body> </html> </pre> SafeStorage.php <pre> <?php fopen("gold.txt", "a"); $gold = intval(file_get_contents("gold.txt")); ?> <!doctype html> <html> <head> <title>Gold Storage</title> </head> <body> <p><?php echo "Total Gold Stored: ".$gold; ?></p> </body> </html> </pre>
2021-03-14

-- Mar 10 In-Class Exercise
<?php
if(isset($_POST["aGold"]))
{
    $data = $_POST["aGold"]; 
 
    $fp = fopen("gold.txt", "a+");
    $gold = fgets($fp);
    $gold = $gold + $data;
    file_put_contents("gold.txt", "");
    fwrite($fp, $gold);
    fclose($fp); 
 
    header("Location:SafeStorage.php");
} 
 
?>
<!DOCTYPE html>
<html lang="en-US">
<head>
    <title>Gold Store Page</title> 
 
    <meta name="description" content="Gold Storage">
    <meta name="author" content="Mark Silva">
    <meta charset="utf-8"> 
 
</head>
<body>
    <h1>Store Gold</h1>
    <form name="goldForm" method="post">
        <label for="aGold">Amount of Gold:</label>
        <input type="text" id="aGold" name="aGold"> 
        <input type="submit" value="Put Somewhere Safe">     </form> </body> </html>
<!DOCTYPE html>
<html lang="en-US">
<head>
    <title>Storage Gold</title> 
 
    <meta name="description" content="Storage Gold">
    <meta name="author" content="Mark Silva">
    <meta charset="utf-8"> 
 
</head>
<body>
    <h1>Amount of Gold Stored</h1>
    <?php
     $fp = fopen("gold.txt", "r+");
     $gold = fgets($fp);
     fclose($fp);  
    ?>
     <p> Total <?= $gold ?> </p>
</body>
</html>
<pre> <?php if(isset($_POST["aGold"])) { $data = $_POST["aGold"]; $fp = fopen("gold.txt", "a+"); $gold = fgets($fp); $gold = $gold + $data; file_put_contents("gold.txt", ""); fwrite($fp, $gold); fclose($fp); header("Location:SafeStorage.php"); } ?> <!DOCTYPE html> <html lang="en-US"> <head> <title>Gold Store Page</title> <meta name="description" content="Gold Storage"> <meta name="author" content="Mark Silva"> <meta charset="utf-8"> </head> <body> <h1>Store Gold</h1> <form name="goldForm" method="post"> <label for="aGold">Amount of Gold:</label> <input type="text" id="aGold" name="aGold"> <br> <input type="submit" value="Put Somewhere Safe"> </form> </body> </html> </pre> <pre> <!DOCTYPE html> <html lang="en-US"> <head> <title>Storage Gold</title> <meta name="description" content="Storage Gold"> <meta name="author" content="Mark Silva"> <meta charset="utf-8"> </head> <body> <h1>Amount of Gold Stored</h1> <?php $fp = fopen("gold.txt", "r+"); $gold = fgets($fp); fclose($fp); ?> <p> Total <?= $gold ?> </p> </body> </html> </pre>

-- Mar 10 In-Class Exercise
StoreGold.php:
<!DOCTYPE html>
<html> 
 
<head>
    <meta charset="utf-8">
    <title>Store Gold</title>
</head> 
 
<body>
    <h2>Store Gold</h2>
    <form method="post">
        <label for="amountOfGold">Amount of Gold: </label>
        <input type="number" name="amountOfGold" id="amountOfGold" />
        <input type="submit" value="Put Somewhere Safe">
    </form>
</body> 
 
</html> 
 
<?php
header("Location:SafeStorage.php"); 
 
if (isset($_POST["amountOfGold"])) {
  if (!is_file("gold.txt")) {
    file_put_contents("gold.txt", 0);
  }
  $amountOfGold = file_get_contents("gold.txt") + $_POST["amountOfGold"];
  file_put_contents("gold.txt", $amountOfGold);
}
?> 
 
SafeStorage:
<!DOCTYPE html>
<html lang="en"> 
 
<head>
    <meta charset="utf-8">
    <title>Safe Storage</title>
</head> 
 
<body>
    <h2>Safe Storage</h2>
    <p>Total Gold Stored: <?php echo file_get_contents("gold.txt") ?></p>
</body> 
 
</html>
(Edited: 2021-03-14)
<pre> StoreGold.php: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Store Gold</title> </head> <body> <h2>Store Gold</h2> <form method="post"> <label for="amountOfGold">Amount of Gold: </label> <input type="number" name="amountOfGold" id="amountOfGold" /> <input type="submit" value="Put Somewhere Safe"> </form> </body> </html> <?php header("Location:SafeStorage.php"); if (isset($_POST["amountOfGold"])) { if (!is_file("gold.txt")) { file_put_contents("gold.txt", 0); } $amountOfGold = file_get_contents("gold.txt") + $_POST["amountOfGold"]; file_put_contents("gold.txt", $amountOfGold); } ?> SafeStorage: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Safe Storage</title> </head> <body> <h2>Safe Storage</h2> <p>Total Gold Stored: <?php echo file_get_contents("gold.txt") ?></p> </body> </html> </pre>

-- Mar 10 In-Class Exercise
StoreGold.php <?php fopen("gold.txt", "a"); $gold = intval(file_get_contents("gold.txt")); if (isset($_POST['button'])) {
  $gold = $gold + intval($_POST['input']);
  file_put_contents("gold.txt", strval($gold));
  header("Location:SafeStorage.php");
} ?>
<!doctype html> <html>
<head>
  <title>Gold Storage</title>
</head>
<body>
  <form method="post">
    <input type="text" name="input" />
    <input type="submit" name="button" value="Put Somewhere Safe" />
  </form>
</body>
</html>
SafeStorage.php <?php fopen("gold.txt", "a"); $gold = intval(file_get_contents("gold.txt")); ?>
<!doctype html> <html>
<head>
    <title>Gold Storage</title>
</head>
<body>
    <p><?php echo "Total Gold Stored: " . $gold; ?></p>
</body>
</html>
StoreGold.php <?php fopen("gold.txt", "a"); $gold = intval(file_get_contents("gold.txt")); if (isset($_POST['button'])) { $gold = $gold + intval($_POST['input']); file_put_contents("gold.txt", strval($gold)); header("Location:SafeStorage.php"); } ?> <!doctype html> <html> <head> <title>Gold Storage</title> </head> <body> <form method="post"> <input type="text" name="input" /> <input type="submit" name="button" value="Put Somewhere Safe" /> </form> </body> </html> SafeStorage.php <?php fopen("gold.txt", "a"); $gold = intval(file_get_contents("gold.txt")); ?> <!doctype html> <html> <head> <title>Gold Storage</title> </head> <body> <p><?php echo "Total Gold Stored: " . $gold; ?></p> </body> </html>
2021-03-15

-- Mar 10 In-Class Exercise
StoreGold.php Deposit amount: Gold
SafeStorage.php

Gold in storage:

StoreGold.php <nowiki><!DOCTYPE html> <html> <body> <form action="StoreGold" method="post"> <label for="amt">Deposit amount: </label> <input type="number" id="amt" name="amt" min="0"> Gold <input type="submit" name="depo" value="Deposit"/> </form> <?php $total = 0; $file_name = 'goldstorage.txt'; if (file_exists($file_name)){ $total = file_get_contents($file_name); } if (isset($_REQUEST['amt'])) { $total = $_REQUEST['amt'] + $total; file_put_contents($file_name, $total); } if(isset($_POST['depo'])){ header("Location:http://localhost/SafeStorage.php"); }?> </body> </html></nowiki> SafeStorage.php <nowiki><!DOCTYPE html> <html> <body> <p>Gold in storage: <?php $total = 0; if (file_exists('goldstorage.txt')){ $total = file_get_contents('goldstorage.txt'); } echo $total;?> </p> </body> </html></nowiki>

-- Mar 10 In-Class Exercise
StoreGold.php: <!DOCTYPE html> <html>
  <body>
    <?php
    $total = 0;
    $file_name = 'gold.txt';
    if (file_exists($file_name)){
        $total = file_get_contents($file_name);
    }
    if (isset($_REQUEST['num'])) {
        $total = $_REQUEST['num'] + $total;
        file_put_contents($file_name, $total);
    }
    if(isset($_POST['submit'])){
		header("Location:SafeStorage.php");
    }
    ?>
    <form action="" method="post">
    <label for = "num">Amount of Gold: </label>
    <input type="number" id="num" name="num" min="0">
    <input type="submit" name="submit" value="Put Somewhere Safe"/>
    </form>
  </body>
</html>
SafeStorage.php: <!DOCTYPE html> <html>
  <body>
    <p>Total Gold Stored: 
	<?php
    $total = 0;
    $file_name = 'gold.txt';
    if (file_exists($file_name)){
        $total = file_get_contents($file_name);
    }
    echo $total;
    ?>
	</p>
  </body>
</html>
'''StoreGold.php:''' <!DOCTYPE html> <html> <body> <?php $total = 0; $file_name = 'gold.txt'; if (file_exists($file_name)){ $total = file_get_contents($file_name); } if (isset($_REQUEST['num'])) { $total = $_REQUEST['num'] + $total; file_put_contents($file_name, $total); } if(isset($_POST['submit'])){ header("Location:SafeStorage.php"); } ?> <form action="" method="post"> <label for = "num">Amount of Gold: </label> <input type="number" id="num" name="num" min="0"> <input type="submit" name="submit" value="Put Somewhere Safe"/> </form> </body> </html> '''SafeStorage.php:''' <!DOCTYPE html> <html> <body> <p>Total Gold Stored: <?php $total = 0; $file_name = 'gold.txt'; if (file_exists($file_name)){ $total = file_get_contents($file_name); } echo $total; ?> </p> </body> </html>

-- Mar 10 In-Class Exercise
StoreGold.php <html> <head> <title>In Class exercise</title> </head>
<body>
    <form action="" method = 'post'>
  		<label >Amount of Gold</label>
 		<input type="number" name="amount">
  		<input type="submit" name='submit' value="Put Somewhere Safe">
	</form>
	<?php
		if (isset($_POST['submit'])) {
			$data= $_POST['amount'];
			file_put_contents("gold.txt",$data);
			header("Location: safestorage.php");
		}
	?>
</body>
</html>
SafeStorage.php <!DOCTYPE html> <html> <head> <title>In Class exercise</title> </head>
<body>
    <?php require_once('storegold.php'); ?>
    <p>  Total Gold Stored  <?php echo readfile("gold.txt"); ?>
</p>
</body>
</html>
StoreGold.php <html> <head> <title>In Class exercise</title> </head> <body> <form action="" method = 'post'> <label >Amount of Gold</label> <input type="number" name="amount"> <input type="submit" name='submit' value="Put Somewhere Safe"> </form> <?php if (isset($_POST['submit'])) { $data= $_POST['amount']; file_put_contents("gold.txt",$data); header("Location: safestorage.php"); } ?> </body> </html> SafeStorage.php <!DOCTYPE html> <html> <head> <title>In Class exercise</title> </head> <body> <?php require_once('storegold.php'); ?> <p> Total Gold Stored <?php echo readfile("gold.txt"); ?> </p> </body> </html>
[ Next ]
X