次の例を見てください。
<!DOCTYPE html>
<head>
<link href="loginmodule.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php
if (count($_POST)) {
for ($i = 0; $i < $_POST["nr_rows"]; $i++) {
echo "<h2>id=".$_POST["goosedown_id"][$i].", qty=".$_POST["qty"][$i]."</h2>\n";
}
die("<pre>".print_r($_POST, true)."</pre>\n");
} else {
// connect to the database
$link = mysqli_init();
// Adjust hostname, username, password and database name before use!
$db = mysqli_real_connect($link, "localhost", "root", "", "test") or die(mysqli_connect_error());
$SQL = "SELECT * FROM 5050goosedown ORDER BY price ASC";
$result = mysqli_query($link, $SQL) or die(mysqli_error($link));
$rows = array();
for ($i = 1; $row = mysqli_fetch_assoc($result); $i++){
$rows[] = array(
'id' => $row['goosedown_id'],
'bgcolor' => ($i % 2) ? '#F5F5F5' : '#E5E5E5',
'name' => htmlentities($row['name']),
'size' => ($row['width'] || $row['height']) ? $row['width'].'/'.$row['height'] : '',
'fill' => $row['normal_fill'].'/'.$row['our_fill'],
'old_price' => $row['old_price'] ? $row['old_price'] : '',
'price' => $row['price']
);
}
}
?>
<form action="<?= $_SERVER['SCRIPT_NAME'] ?>" method="post">
<table border="0" width="600">
<tr>
<th width="30%" colspan="2"><b>50/50 Goose Down:</b></th>
<th width="30%"><i>Normal Fill / Our Fill</i></th>
<th width="12.5%"><i>Old Price</i></th>
<th width="12.5%"><i>Price</i></th>
<th width="12.5%"><i>Quantity</i></th>
</tr>
<?php foreach ($rows as $row): ?>
<tr bgcolor="<?= $row['bgcolor'] ?>">
<td><?= $row['name'] ?></td>
<td><?= $row['size'] ?></td>
<td><?= $row['fill'] ?></td>
<td><?= $row['old_price'] ?></td>
<td><?= $row['price'] ?></td>
<td>
<select name="qty[]">
<?php for ($i = 0; $i <= 8; $i++): ?>
<option value="<?= $i ?>"><?= $i ?></option>
<?php endfor ?>
</select>
<input type="hidden" name="goosedown_id[]" value="<?= $row['id'] ?>" />
</td>
</tr>
<?php endforeach ?>
</table>
<input type="submit" name="myButton" id="myButton" value="Submit" />
<input type="hidden" name="nr_rows" value="<?= count($rows) ?>">
</form>
</body>
</html>