Chandan Jha
Chandan Jha

Reputation: 45

how to once fetched data in different 2 div?

I want to show fetched data in 2 differnt div. I had fetched 2 random data from database, to show in two div. My 1st div had leftside content and right side image and 2nd div had leftside image and rightside content. So how can i show fetched data in 2 different div.

Below i provide my divs

<div class="new-product">
    <div class="row">
        <div class="col-sm-6">
            <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quia sint minima, ab asperiores dolores, aspernatur tempora veritatis consectetur omnis ea dolor, doloremque impedit assumenda placeat quod eligendi! Error, quos, quidem.</p>
        </div>
        <div class="col-sm-6">
            <img src="" alt="">
        </div>
    </div>
    <div class="row">   
        <div class="col-sm-6">
            <img src="" alt="">
        </div>
        <div class="col-sm-6">
            <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quia sint minima, ab asperiores dolores, aspernatur tempora veritatis consectetur omnis ea dolor, doloremque impedit assumenda placeat quod eligendi! Error, quos, quidem.</p>
        </div>
    </div>
</div>

and this is fetch data query

$result  = $this->connect()->query("SELECT * FROM wm_products ORDER BY RAND() LIMIT 2");
        if($result->num_rows > 0){
            while($row = $result->fetch_assoc()){
            }
        }

below is output- enter image description here

Upvotes: 0

Views: 37

Answers (1)

Lukasz Formela
Lukasz Formela

Reputation: 447

I think you could do it like this:

$result  = $this->connect()->query("SELECT * FROM wm_products ORDER BY RAND() LIMIT 2");

$i = 0;
$html = "";
if($result->num_rows > 0) {
    $html .= "<div class='new-product'>";
    while($row = $result->fetch_assoc()) {
        if($i%2 == 0) {
            $html .= "<div class='row'>
                <div class='col-sm-6'>
                    <p>{$row->text}</p>
                </div>
                <div class='col-sm-6'>
                    <img src='{$row->image}' alt=''>
                </div>
            </div>";
        } else {
            $html .= "<div class='row'>   
                <div class='col-sm-6'>
                    <img src='{$row->image}' alt=''>
                </div>
                <div class='col-sm-6'>
                    <p>{$row->text}</p>
                </div>
           </div>";
        }
        $i++;
    }
    $html .= "</div>";
}

echo $html;

Assuming that your columns in SQL query are named as text and image. Otherwise replace the $row->text and $row->image with respective column names.

Upvotes: 2

Related Questions