始める前に、PHP をより理解しやすくしてくれたこのサイトに感謝したいと思います。私はPHPが初めてで、フォームを扱うのはこれが初めてです。次のコードの目標は、ユーザーが数量を選択して [送信] をクリックしたときに、選択した数量と合計を示す請求書の表を開くようにすることです。私はたくさんの YouTube や読書を行ってきましたが、自分のページでそれを正確に行う方法をまだ理解できていません。心に留めておいてください、私はそれを空想的にしようとしているのではなく、非常に理解しやすいだけです. これが私のコードです:)
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<form action= "<= $_server['PHP_SELF'] ?>" method="$_POST">
<?php // **INSERT NAME HERE This code will allow the user to purchase whatever quantity of certain comic books they want.
$productImage = array('spiderman1.jpg', 'spiderman2.jpg', 'spiderman3.jpg', 'spiderman4.jpg', 'hulk1.jpg'); // I'm stil lhaving a hard time with multidimensional arrays.
$description = array('Amazing Spiderman #1', 'Amazing Spiderman #15', 'Amazing Spiderman #52', 'Amazing Spiderman #107', 'Hulk #181');
$price = array('400', '350', '150', '300', '90');
//the product image array gave me a hard time, in the sense that I'm still having a hard time trying to output an image that saves onto my desktop locally. So I chose the option of grabbing the links fromt the web. Don't worry though, I won't do this for my Assignment #1. UPDATE: Problem has been fixed!
echo '<table border="1" align="center" cellpadding="10">'; // I'm very horrible with tables, I finally figured out how to center the text within the table.
echo "<tr align='center'>
<td><b>Product</b></td>
<td><b>Description</b></td>
<td><b>Price (each)</b></td>
<td><b>Quantity</b></td>
</tr>";
foreach ($productImage as $key=>$display) // I used $key to represent the value to display my images via $display. If you erase the $display code it would look very nice, but output a bunch of errors.
{
echo "<tr align='center'>";
echo '<td>';
echo "<img src='".$productImage[$key]."' width='200' height='300' align='center'>";
echo '</td>';
echo '<td>';
echo $description[$key];// Description loop from the foreach()
echo '</td>';
echo '<td>';
printf('$%', $price); // for some reason if I wanted the price to be $400.00 with '$%.2f', it would print as 1.00400. So I just kept it as whole numbers swapmeet style.
echo $price[$key]; // This loops the price via the foreach() function.
echo '</td>';
echo '<td>';
echo '<select name="service_type"><br>'; // my quantity select box, since these are very rare comics
echo '<option>0</option>'; // started off with 0 because if I gave this first value a 1, then all default comic quantity would be one. Users will be mad :(
echo '<option>1</option>';
echo '<option>2</option>';
echo '<option>3</option>';
echo '</td>';
}
echo '<tr>';
echo '<td>';
echo 'Add to cart '; // letting the user know what to do after they picked the quantity
echo '<input type="submit" value="submit" name="submit" send="invoice.php">'; // my submit button
echo '</td>';
echo '</tr>';
echo '</table>';
//The images under the description are linked to the websites, if the images are not shown it is due to the image being removed or renamed on the linked site (a disclaimer I borrowed from the assignment webpage.)
if (array_key_exists('submit', $_POST)) // what I want to do is, if the user clicks submit, it will bring them to their invoice page.
{
echo "This is your total" . $_POST['submit'];
}
?>
</form>
</body>
</html>