Skip to content

Instantly share code, notes, and snippets.

@minhphong306
Last active October 8, 2017 15:44
Show Gist options
  • Save minhphong306/c5d794024abde67833161bc7c50d834c to your computer and use it in GitHub Desktop.
Save minhphong306/c5d794024abde67833161bc7c50d834c to your computer and use it in GitHub Desktop.
Giải bài tập PHP
<?php
$host = '192.168.43.99';
$username = 'root';
$passwd = 'root';
$dbname = 'shop';
$conn = new mysqli($host, $username, $passwd, $dbname);
$query = "SELECT * FROM category order by name";
$data = $conn->query($query);
$newline = '<br/>';
echo "<form method='GET'>";
echo "<select id='category_list' name='cat_id' onchange='this.form.submit()'>";
echo "<option>Tất cả</option>";
while ($row = $data->fetch_assoc()) {
$id = $row['Id'];
$name = $row['Name'];
echo "<option value='$id'>";
echo "$name";
echo "</option>";
}
echo "</select>";
echo "</form>";
if (isset($_GET['cat_id'])) {
$cat_id = $_GET['cat_id'];
$query = "SELECT p.id, p.Name, c.Name as Cat_name, p.Description, p.Price, p.Is_active
from product p join category c
on p.Cat_id = c.Id
where p.cat_id = $cat_id
order by p.Name";
} else {
$query = "SELECT p.id, p.Name, c.Name as Cat_name, p.Description, p.Price, p.Is_active
from product p join category c
on p.Cat_id = c.Id
order by p.Name";
}
$data = $conn->query($query);
echo "<table>
<thead>
<tr>
<th>Name</th>
<th>Category</th>
<th>Description</th>
<th>Price</th>
<th>Active</th>
</tr>
</thead>
<tbody>";
while ($row = $data->fetch_assoc()) {
$name = $row['Name'];
$cname = $row['Cat_name'];
$des = $row['Description'];
$price = $row['Price'];
$is_active = $row['Is_active'];
if ($is_active) {
$active = "<input type='checkbox' checked disabled>";
} else {
$active = "<input type='checkbox' disabled>";
}
echo "<tr>
<td>$name</td>
<td>$cname</td>
<td>$des</td>
<td>$price</td>
<td>$active</td>
</tr>";
}
echo "</tbody></table>";
$conn->close();
?>
<style>
table, thead, tbody, tr, th, td{
border: 1px solid black;
}
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment