PHP MySqli Multiple query & while in one page - help to simplifying code

GePraxa

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");?>
sloaxleak

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

multiple mysqli delete codes on one PHP page

From Dev

Creating multiple while loops for one array in php and mysqli

From Dev

Simplifying multiple Or Regex query

From Dev

Simplifying a query in PHP

From Dev

Need help simplifying a method in my code

From Dev

simplifying multiple variable assignment code

From Dev

PHP Mysqli trigger a query one after other

From Dev

Simplifying multiple boolean checks into a single one

From Dev

pointer int in C, need help understanding and simplifying code snippet

From Dev

Only first Mysqli Insert query with PHP working while others fail

From Dev

Only first Mysqli Insert query with PHP working while others fail

From Dev

PHP while loop returning only one row when SUM added to multiple JOIN query

From Dev

How to update two select boxes using one mysqli query in php

From Dev

mysqli query returns the column name as one row in php

From Dev

SQL Query Writing Help - Multiple Lookups in One Record

From Dev

Executing multiple mysql queries in consectuive mysqli_query php

From Dev

Query was empty error in PHP with INSERT Multiple statement in mysqli

From Dev

Uploading multiple images one to each row PHP and Mysqli db

From Dev

PHP MySQLi - update multiple data with some formula in one process

From Dev

Need help in solving the class not found error in PHP while refactoring the code

From Dev

PHP if mysqli query statement

From Dev

php mysqli update query

From Dev

MySQLi Query Error in PHP

From Dev

Php run MySqli query

From Dev

php mysqli update query

From Dev

MySQLi Query Error in PHP

From Dev

Php Ajax - Multiple calls in one page

From Dev

Having issues with multiple forms in one page in php

From Dev

How to run multiple queries in one php page?

Related Related

  1. 1

    multiple mysqli delete codes on one PHP page

  2. 2

    Creating multiple while loops for one array in php and mysqli

  3. 3

    Simplifying multiple Or Regex query

  4. 4

    Simplifying a query in PHP

  5. 5

    Need help simplifying a method in my code

  6. 6

    simplifying multiple variable assignment code

  7. 7

    PHP Mysqli trigger a query one after other

  8. 8

    Simplifying multiple boolean checks into a single one

  9. 9

    pointer int in C, need help understanding and simplifying code snippet

  10. 10

    Only first Mysqli Insert query with PHP working while others fail

  11. 11

    Only first Mysqli Insert query with PHP working while others fail

  12. 12

    PHP while loop returning only one row when SUM added to multiple JOIN query

  13. 13

    How to update two select boxes using one mysqli query in php

  14. 14

    mysqli query returns the column name as one row in php

  15. 15

    SQL Query Writing Help - Multiple Lookups in One Record

  16. 16

    Executing multiple mysql queries in consectuive mysqli_query php

  17. 17

    Query was empty error in PHP with INSERT Multiple statement in mysqli

  18. 18

    Uploading multiple images one to each row PHP and Mysqli db

  19. 19

    PHP MySQLi - update multiple data with some formula in one process

  20. 20

    Need help in solving the class not found error in PHP while refactoring the code

  21. 21

    PHP if mysqli query statement

  22. 22

    php mysqli update query

  23. 23

    MySQLi Query Error in PHP

  24. 24

    Php run MySqli query

  25. 25

    php mysqli update query

  26. 26

    MySQLi Query Error in PHP

  27. 27

    Php Ajax - Multiple calls in one page

  28. 28

    Having issues with multiple forms in one page in php

  29. 29

    How to run multiple queries in one php page?

HotTag

Archive