Logo  

CS479/579 - Web Programming II

Displaying exercises/e9/solution/store.php

<?php
$myconn = new mysqli("localhost", "sbaker", "sbaker", "sbaker");
function getvendor($vid) {
  global $myconn;
  $st = $myconn->prepare("SELECT name FROM vendor WHERE vid=?");
  $st->bind_param("i", $vid); $st->bind_result($name);
  $st->execute(); $st->fetch(); $st->close();
  return $name;
}
?>
<!DOCTYPE html>
<html>
<head>
 <title> Store front </title>
 <meta charset='utf-8'>
 <link rel=stylesheet type='text/css' href='style.css'>
</head>
<body>
<table id='ctbl'>
<tr>
  <td colspan=2 id='top'><a href='store.php'>Very Generic Super Computer Store</a><br>&nbsp;
<tr>
<td id='sidelist'>
<ul>Components
<?php
$st = $myconn->prepare("SELECT cid, name FROM component ORDER BY name ASC");
$st->bind_result($cid, $name);
$st->execute();
while($st->fetch()) {
  echo "<li><a href='component.php?cid=$cid'> $name </a>\n";
}
$st->close();
?>
</ul>
<td id='items'>
<h1> Daily deals! </h1>
<table style='margin: 0 auto;'>
<?php
function show($item) {
  $vendor = getvendor($item['vid']);
  $price = '$'.$item['price'];
  echo <<<DESC
<a href='item.php?iid={$item['iid']}'>
<table class='item'>
<tr>
 <td class='pic'> {$item['description'][0]}
 <td class='desc'> <div class='vendor'>$vendor</div> {$item['description']}<br>
   <div class='price'>{$price}</div>
</table>
</a>
DESC;
}
$items = [];
$st = $myconn->query("SELECT * FROM inventory WHERE amount > 1000");
while($row = $st->fetch_assoc()) {
  $items[] = $row;
}
$num = count($items);
for($r = 0; $r < 5; $r++) {
  echo "<tr>";
  for($c = 0; $c < 2; $c++) {
    if ($c) echo "<td style='width:50px;'>";
    echo "<td>";
    do {
      $i = rand(0, $num);
    } while (isset($items[$i]['picked']));
    $items[$i]['picked'] = true;
    show($items[$i]);
  }
}

?>
</table>
</table>
</body>
</html>