Home > front end >  Dynamically Create divs based on mysql query
Dynamically Create divs based on mysql query

Time:03-28

I am developing the cart page for an ecommerce website using HTML, PHP, and mySQL.

How can I generate and place data inside divs n times based on the result of the mySQL query.

For example, if the mySQL returns 3 rows, I want 3 divs to be created dynamically and have data place inside them.

Thanks for your help.

I am guessing this is done using a for loop, but I am struggling to put it into code

<?php
  $conn  = mysqli_connect($servername, $username, $password, $database);
  $query = "SELECT * FROM `product` ORDER BY RAND() LIMIT 3";
  $result = $conn->query($query);
  while($rows = fetch_assoc($query)){ ?>
        <article >
            <div >
                <figure >
                    <figcaption >
                    <a href="#" ><?php echo $rows['name'] ?></a>
                    </figcaption>
                </figure>
            </div>
        </article>  
    <?php }
?>

CodePudding user response:

You are using the wrong function in $rows = fetch_assoc($query)

Rewrite to $rows = mysqli_fetch_assoc($query)

See details mysqli_fetch_assoc()

CodePudding user response:

ok it will be something like this

$result = mysql_query("SELECT * FROM Form");
while($row = mysql_fetch_array($result))

  {
  echo "<div>";
  echo $row['Id'] ."|". $row['name']."|". $row['Mobile']."|" $row['email'];

  echo "</div>";

  }
  • Related