ChrRaz

Untitled

Jan 14th, 2016
348
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.60 KB | None | 0 0
  1. <!doctype html>
  2. <html lang="en">
  3.     <head>
  4.         <meta charset="UTF-8">
  5.         <title>Varer</title>
  6.         <link rel="stylesheet" href="styles.css"/>
  7.     </head>
  8.     <body>
  9.         <?php require('komponenter/header.php'); ?>
  10.  
  11.         <h1>Varer</h1>
  12.         <?php
  13.         $db = mysqli_connect('localhost', 'root', 'toor', 'webshop_it');
  14.         //var_dump(['db' => $db, 'error' => mysqli_connect_error()]);
  15.  
  16.         switch ($_SERVER['REQUEST_METHOD'])
  17.         {
  18.             case 'GET':
  19.                 echo '<form action="" method="post">';
  20.                 echo '<table>';
  21.  
  22.                 $result = $db->query('SELECT * FROM varer');
  23.  
  24.                 $headers = array_reduce($result->fetch_fields(), function ($carry, $field)
  25.                 {
  26.                     if ($field->name != 'id')
  27.                         return $carry . "<th>" . $field->name . "</th>";
  28.                 });
  29.                 echo "<thead><tr>$headers</tr></thead>";
  30.  
  31.                 $rows = '';
  32.                 while ($row = $result->fetch_assoc())
  33.                 {
  34.                     /*$table_row = array_reduce($row,function($carry, $column)
  35.                     {
  36.                         return $carry . "<td>" . $column . "</td>";
  37.                     });
  38.  
  39.                     $rows .= "<tr>$table_row<td><button type='submit' name='id' value='${row['id']}'>Bestil</button></td></tr>";*/
  40.  
  41.                     $rows .=
  42.                         "<tr>" .
  43.                         "<td>${row['navn']}</td>" .
  44.                         "<td><img src='/billeder/${row['billede']}' width='100px'/></td>" .
  45.                         "<td>${row['pris']}</td>" .
  46.                         "<td>${row['beskrivelse']}</td>" .
  47.                         "<td><input type='number' name='amount[${row['id']}]'></td>" .
  48.                         "<td><button type='submit' name='id' value='${row['id']}'>Bestil</button></td>" .
  49.                         "</tr>";
  50.                 }
  51.  
  52.  
  53.                 echo "<tbody>$rows</tbody>";
  54.  
  55.                 echo '</table>';
  56.                 echo '</form>';
  57.                 break;
  58.  
  59.             case 'POST':
  60.                 echo "<a href='varer.php'>Tilbage</a>";
  61.  
  62.                 $stmt = $db->prepare("SELECT * FROM varer WHERE id=?");
  63.                 $stmt->bind_param('i', $_POST['id']);
  64.                 $stmt->execute();
  65.  
  66.                 $result = $stmt->get_result()->fetch_assoc();
  67.  
  68.                 echo "<p>Du har bestilt en ${result['beskrivelse']}</p>";
  69.  
  70.                 session_start();
  71.  
  72.                 $found = false;
  73.  
  74.                 $amount = (int) $_POST['amount'][$result['id']];
  75.                 foreach ($_SESSION['kurv'] as &$item)
  76.                 {
  77.                     if ($item['id'] == $result['id'])
  78.                     {
  79.                         $item['amount'] += $amount;
  80.                         $found = true;
  81.                         break;
  82.                     }
  83.                 }
  84.  
  85.                 if(!$found)
  86.                     $_SESSION['kurv'][] = [
  87.                         'id'     => $result['id'],
  88.                         'amount' => $amount
  89.                     ];
  90.                 break;
  91.         }
  92.         ?><!doctype html>
  93. <html lang="en">
  94.     <head>
  95.         <meta charset="UTF-8">
  96.         <title>Varer</title>
  97.         <link rel="stylesheet" href="styles.css"/>
  98.     </head>
  99.     <body>
  100.         <?php require('komponenter/header.php'); ?>
  101.  
  102.         <h1>Varer</h1>
  103.         <?php
  104.         $db = mysqli_connect('localhost', 'root', 'toor', 'webshop_it');
  105.         //var_dump(['db' => $db, 'error' => mysqli_connect_error()]);
  106.  
  107.         switch ($_SERVER['REQUEST_METHOD'])
  108.         {
  109.             case 'GET':
  110.                 echo '<form action="" method="post">';
  111.                 echo '<table>';
  112.  
  113.                 $result = $db->query('SELECT * FROM varer');
  114.  
  115.                 $headers = array_reduce($result->fetch_fields(), function ($carry, $field)
  116.                 {
  117.                     if ($field->name != 'id')
  118.                         return $carry . "<th>" . $field->name . "</th>";
  119.                 });
  120.                 echo "<thead><tr>$headers</tr></thead>";
  121.  
  122.                 $rows = '';
  123.                 while ($row = $result->fetch_assoc())
  124.                 {
  125.                     /*$table_row = array_reduce($row,function($carry, $column)
  126.                     {
  127.                         return $carry . "<td>" . $column . "</td>";
  128.                     });
  129.  
  130.                     $rows .= "<tr>$table_row<td><button type='submit' name='id' value='${row['id']}'>Bestil</button></td></tr>";*/
  131.  
  132.                     $rows .=
  133.                         "<tr>" .
  134.                         "<td>${row['navn']}</td>" .
  135.                         "<td><img src='/billeder/${row['billede']}' width='100px'/></td>" .
  136.                         "<td>${row['pris']}</td>" .
  137.                         "<td>${row['beskrivelse']}</td>" .
  138.                         "<td><input type='number' name='amount[${row['id']}]'></td>" .
  139.                         "<td><button type='submit' name='id' value='${row['id']}'>Bestil</button></td>" .
  140.                         "</tr>";
  141.                 }
  142.  
  143.  
  144.                 echo "<tbody>$rows</tbody>";
  145.  
  146.                 echo '</table>';
  147.                 echo '</form>';
  148.                 break;
  149.  
  150.             case 'POST':
  151.                 echo "<a href='varer.php'>Tilbage</a>";
  152.  
  153.                 $stmt = $db->prepare("SELECT * FROM varer WHERE id=?");
  154.                 $stmt->bind_param('i', $_POST['id']);
  155.                 $stmt->execute();
  156.  
  157.                 $result = $stmt->get_result()->fetch_assoc();
  158.  
  159.                 echo "<p>Du har bestilt en ${result['beskrivelse']}</p>";
  160.  
  161.                 session_start();
  162.  
  163.                 $found = false;
  164.  
  165.                 $amount = (int) $_POST['amount'][$result['id']];
  166.                 foreach ($_SESSION['kurv'] as &$item)
  167.                 {
  168.                     if ($item['id'] == $result['id'])
  169.                     {
  170.                         $item['amount'] += $amount;
  171.                         $found = true;
  172.                         break;
  173.                     }
  174.                 }
  175.  
  176.                 if(!$found)
  177.                     $_SESSION['kurv'][] = [
  178.                         'id'     => $result['id'],
  179.                         'amount' => $amount
  180.                     ];
  181.                 break;
  182.         }
  183.         ?>
  184.     </body>
  185. </html>
  186.     </body>
  187. </html>
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment