I'm following the official PHP manual: http://php.net/manual/es/mysqli-result.fetch-assoc.php
So far I have my code as follows, but I'm not sure if I'm doing the right thing, since I'm using on the same page many "query" and "while", I wondered if the possibility of putting at the beginning of the code all PHP and within the HTML call the variables with an "echo" to not have to go by opening and closing PHP all the time, could help me simplify this code to enterder but as it is right or whether this is "good" within normal continue. Thanks in advance!
<?php
require_once("inc/header.php");
$connect = new mysqli("localhost", "user", "pass", "db_name");
if ($connect->connect_errno) {
printf("Error: %s", $connect->connect_error);
exit();
}
?>
<!-- *************** show products *************** -->
<div class="container">
<div class="row">
<?php
$query = "SELECT * FROM product";
if ($result = $connect->query($query)) {
while ($row = $result->fetch_assoc()) {
?>
<div class="col-xs-6 col-sm-4 col-md-3 col-lg-3">
<div class="thumbnail">
<a href="product.php?id=<?php echo $row['product_id']; ?>"><img src="<?php echo $row['product_img']; ?>" alt="<?php echo $row['product_name']; ?>"></a>
<div class="caption">
<a href="product.php?id=<?php echo $row['product_id']; ?>"><h6 class="truncate"><?php echo $row['product_name']; ?></h6></a>
<h5><?php echo "$".$row['product_price']; ?></h5>
</div>
</div>
</div>
<?php
}
$result->free();
}
?>
</div>
</div>
<!-- *************** Latest 10 products... *************** -->
<div class="container">
<div class="row">
<?php
$query = "SELECT * FROM product ORDER BY product_id DESC LIMIT 10";
if ($result = $connect->query($query)) {
while ($row = $result->fetch_assoc()) {
?>
<div class="col-xs-6 col-sm-4 col-md-3 col-lg-3">
<div class="thumbnail">
<a href="product.php?id=<?php echo $row['product_id']; ?>"><img src="<?php echo $row['product_img']; ?>" alt="<?php echo $row['product_name']; ?>"></a>
<div class="caption">
<a href="product.php?id=<?php echo $row['product_id']; ?>"><h6 class="truncate"><?php echo $row['product_name']; ?></h6></a>
<h5><?php echo "$".$row['product_price']; ?></h5>
</div>
</div>
</div>
<?php
}
$result->free();
}
?>
</div>
</div>
<!-- *************** Show the categories list... *************** -->
<?php
$query = "SELECT * FROM categories";
if ($result = $connect->query($query)) {
while ($row = $result->fetch_assoc()) {
?>
<ul>
<li><a href="categories.php?id=<?php echo $row['categories_id']; ?>"><?php echo $row['categories_name']; ?></a></li>
</ul>
<?php
}
$result->free();
}
$connect->close();
?>
<?php require_once("inc/footer.php");?>
I suggest use a library called meekrodb, once loaded, you can do queries like this
$news = DB::query("SELECT id,title WHERE title = %s","Title you want");
foreach ($news as $new) {
echo $new["title"];
}
It's perfect to do too many queries on one page. You can download legally and free from MeekroDB.com
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments