jumpman8947 jumpman8947 - 5 months ago 17
MySQL Question

Populate Multiple variables with php from mysql database

I have data for various products in my, mysql database. Ex

ID Name price detail fact
1 test 20.00 text example
2 testing 30.00 apple orange
3 tester 5.43 plum grape


And i have html code similar to the below and then repeats lets say about 30 different times because there are 30 different product data in the mysql

<!-- This is product 1 code -->
<div class="col-sm-4">
<div class="product-image-wrapper">
<div class="single-products">
<div class="productinfo text-center">

<img src="sample image 1" alt="" />
<h2>"price 1"</h2>
<p>"detail 1"</p>
<a href="#" class="btn btn-default add-to-cart"><i class="fa fa-shopping-cart"></i>Add to cart</a>
</div>
<div class="product-overlay">
<div class="overlay-content">
<h2>"price 1"</h2>
<h2>"ID"</h2>
<p>"detail 1"</p>
<a href="#" class="btn btn-default add-to-cart"><i class="fa fa-shopping-cart"></i>Add to cart</a>
</div>
</div>
</div>
<div class="choose">
<ul class="nav nav-pills nav-justified">
<li><a href=""><i class="fa fa-plus-square"></i>Add to wishlist</a></li>
<li><a href=""><i class="fa fa-plus-square"></i>Add to compare</a></li>
</ul>
</div>
</div>

<!-- This is product 2 code -->
<div class="col-sm-4">
<div class="product-image-wrapper">
<div class="single-products">
<div class="productinfo text-center">

<img src="sample image 2" alt="" />
<h2>"price 2"</h2>
<p>"Detail 2"</p>
<a href="#" class="btn btn-default add-to-cart"><i class="fa fa-shopping-cart"></i>Add to cart</a>
</div>
<div class="product-overlay">
<div class="overlay-content">
<h2>"price 2"</h2>
<p>"ID2"</p>
<p>"Detail 2"</p>
<a href="#" class="btn btn-default add-to-cart"><i class="fa fa-shopping-cart"></i>Add to cart</a>
</div>
</div>
</div>
<div class="choose">
<ul class="nav nav-pills nav-justified">
<li><a href=""><i class="fa fa-plus-square"></i>Add to wishlist</a></li>
<li><a href=""><i class="fa fa-plus-square"></i>Add to compare</a></li>
</ul>
</div>
</div>


What i'm trying to accomplish is for example, in product 1 code enter where "price 1" would be the variable for price in the first row in my data base, "detail 1" would be the 1st row detail from the data base. So on and so fourth. Then when we get to product 2 code skip down to second row of data and insert matching data in its correct field. and repeat this process until all products in the database have been match with the code. Also notice not all fields in the row of mysql data are being used like "fact" for example.

I am not sure how to accomplish this.

Answer Source

You need to obtain the data by querying your MySQL database. Once you do this, you can loop through the result set, row by row, and output.

<?php

// Some code here to obtain MySQL data result set, like...
$result_set = mysqli_query( 'SELECT * FROM products_table_name WHERE 1 ORDER BY name ASC' );

$x = 1;
foreach( $result_set as $row ){

?>
<!-- This is product <?php echo($x); ?> code -->
<div class="col-sm-4">
                        <div class="product-image-wrapper">
                            <div class="single-products">
                                <div class="productinfo text-center">

                                    <img src="sample image 1" alt="" />
                                    <h2><?php echo $row['price']; ?></h2>
                                    <p><?php echo $row['detail']; ?></p>
                                    <a href="#" class="btn btn-default add-to-cart"><i class="fa fa-shopping-cart"></i>Add to cart</a>
                                </div>
                                <div class="product-overlay">
                                    <div class="overlay-content">
                                        <h2><?php echo $row['price']; ?></h2>
                                        <h2><?php echo $row['ID']; ?></h2>
                                        <p><?php echo $row['detail']; ?></p>
                                        <a href="#" class="btn btn-default add-to-cart"><i class="fa fa-shopping-cart"></i>Add to cart</a>
                                    </div>
                                </div>
                            </div>
                            <div class="choose">
                                <ul class="nav nav-pills nav-justified">
                                    <li><a href=""><i class="fa fa-plus-square"></i>Add to wishlist</a></li>
                                    <li><a href=""><i class="fa fa-plus-square"></i>Add to compare</a></li>
                                </ul>
                            </div>
                        </div>
<?php
    $x++;
}