<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Varer</title>
<link rel="stylesheet" href="styles.css"/>
</head>
<body>
<?php require('komponenter/header.php'); ?>
<h1>Varer</h1>
<?php
$db = mysqli_connect('localhost', 'root', 'toor', 'webshop_it');
//var_dump(['db' => $db, 'error' => mysqli_connect_error()]);
switch ($_SERVER['REQUEST_METHOD'])
{
case 'GET':
echo '<form action="" method="post">';
echo '<table>';
$result = $db->query('SELECT * FROM varer');
$headers = array_reduce($result->fetch_fields(), function ($carry, $field)
{
if ($field->name != 'id')
return $carry . "<th>" . $field->name . "</th>";
});
echo "<thead><tr>$headers</tr></thead>";
$rows = '';
while ($row = $result->fetch_assoc())
{
/*$table_row = array_reduce($row,function($carry, $column)
{
return $carry . "<td>" . $column . "</td>";
});
$rows .= "<tr>$table_row<td><button type='submit' name='id' value='${row['id']}'>Bestil</button></td></tr>";*/
$rows .=
"<tr>" .
"<td>${row['navn']}</td>" .
"<td><img src='/billeder/${row['billede']}' width='100px'/></td>" .
"<td>${row['pris']}</td>" .
"<td>${row['beskrivelse']}</td>" .
"<td><input type='number' name='amount[${row['id']}]'></td>" .
"<td><button type='submit' name='id' value='${row['id']}'>Bestil</button></td>" .
"</tr>";
}
echo "<tbody>$rows</tbody>";
echo '</table>';
echo '</form>';
break;
case 'POST':
echo "<a href='varer.php'>Tilbage</a>";
$stmt = $db->prepare("SELECT * FROM varer WHERE id=?");
$stmt->bind_param('i', $_POST['id']);
$stmt->execute();
$result = $stmt->get_result()->fetch_assoc();
echo "<p>Du har bestilt en ${result['beskrivelse']}</p>";
session_start();
$found = false;
$amount = (int) $_POST['amount'][$result['id']];
foreach ($_SESSION['kurv'] as &$item)
{
if ($item['id'] == $result['id'])
{
$item['amount'] += $amount;
$found = true;
break;
}
}
if(!$found)
$_SESSION['kurv'][] = [
'id' => $result['id'],
'amount' => $amount
];
break;
}
?><!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Varer</title>
<link rel="stylesheet" href="styles.css"/>
</head>
<body>
<?php require('komponenter/header.php'); ?>
<h1>Varer</h1>
<?php
$db = mysqli_connect('localhost', 'root', 'toor', 'webshop_it');
//var_dump(['db' => $db, 'error' => mysqli_connect_error()]);
switch ($_SERVER['REQUEST_METHOD'])
{
case 'GET':
echo '<form action="" method="post">';
echo '<table>';
$result = $db->query('SELECT * FROM varer');
$headers = array_reduce($result->fetch_fields(), function ($carry, $field)
{
if ($field->name != 'id')
return $carry . "<th>" . $field->name . "</th>";
});
echo "<thead><tr>$headers</tr></thead>";
$rows = '';
while ($row = $result->fetch_assoc())
{
/*$table_row = array_reduce($row,function($carry, $column)
{
return $carry . "<td>" . $column . "</td>";
});
$rows .= "<tr>$table_row<td><button type='submit' name='id' value='${row['id']}'>Bestil</button></td></tr>";*/
$rows .=
"<tr>" .
"<td>${row['navn']}</td>" .
"<td><img src='/billeder/${row['billede']}' width='100px'/></td>" .
"<td>${row['pris']}</td>" .
"<td>${row['beskrivelse']}</td>" .
"<td><input type='number' name='amount[${row['id']}]'></td>" .
"<td><button type='submit' name='id' value='${row['id']}'>Bestil</button></td>" .
"</tr>";
}
echo "<tbody>$rows</tbody>";
echo '</table>';
echo '</form>';
break;
case 'POST':
echo "<a href='varer.php'>Tilbage</a>";
$stmt = $db->prepare("SELECT * FROM varer WHERE id=?");
$stmt->bind_param('i', $_POST['id']);
$stmt->execute();
$result = $stmt->get_result()->fetch_assoc();
echo "<p>Du har bestilt en ${result['beskrivelse']}</p>";
session_start();
$found = false;
$amount = (int) $_POST['amount'][$result['id']];
foreach ($_SESSION['kurv'] as &$item)
{
if ($item['id'] == $result['id'])
{
$item['amount'] += $amount;
$found = true;
break;
}
}
if(!$found)
$_SESSION['kurv'][] = [
'id' => $result['id'],
'amount' => $amount
];
break;
}
?>
</body>
</html>
</body>
</html>
Comments
0 B
|👍
/👎