Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Himanshu_wkd's avatar

PHP

Hey, I am having one concern related to php :

I want to write a loop for displaying 4 different div on a page, using the database values. Can someone help?

0 likes
7 replies
Ashraam's avatar

Can you be more explicit ? like you us your code...

A loop is a basic php feature maybe take a look at this

Himanshu_wkd's avatar

@ashraam @dipakrataniagile

Here is my code :

<section class="case__wrap">
 
<?php
         

$testData=$CommonClass->ResultWithArrayData("SELECT * FROM portfolio WHERE status=1 ORDER BY id DESC ");

?>

  <div>          
  <div class="custom_container">
    <div class="custom_row">
      <div class="case__wrap_inner_text">
        <h2>Case <strong>Studies</strong></h2>
      </div>
      <div class="case__wrap_inner" >
      
       <div class="image__wrapper_content">         
       <img src="admin/uploads/portfolio/<?php echo $testData[3]['image'] ?>">
       <!-- <img src="assets/img/protfolio/vitalizeart-thumnail.png"> -->
       </div>
      
       <div class="content__wrapper_of_case_Study">
        <div class="top___Sec">
       
      
          <div class="middle__ineeer_sec_For_grid">
              <div class="middle__Sec">
              <img src="admin/uploads/portfolio/<?php echo $testData[3]['logo'] ?>">
                  <!-- <img src="assets/img/protfolio/logo/vitalizeart_logo.png"> -->
               <h1><?php echo $testData[3]['heading'] ?></h1>
               <p><?php echo $testData[3]['description'] ?> </p>
               <a href="case-study-detail.php?pid=<?php echo $testData[3]['id']?>">View  Detail</a>
             </div>
          </div>
        </div>
      </div>
        </div>

        <div class="casee_in_grid">
          <div class="case_in_grid__inner">
            <div class="img__Sec_gird">
            <img src="admin/uploads/portfolio/<?php echo $testData[2]['image'] ?>">
            </div>
            <h1><?php echo $testData[2]['heading'] ?></h1>
               <p><?php echo $testData[2]['description'] ?> </p>
              <a href="case-study-detail.php?pid=<?php echo $testData[2]['id']?>">View  Detail</a>
          </div>
          <div class="case_in_grid__inner">
              <div class="img__Sec_gird">
              <img src="admin/uploads/portfolio/<?php echo $testData[1]['image'] ?>">
              </div>
              <h1><?php echo $testData[1]['heading'] ?></h1>
               <p><?php echo $testData[1]['description'] ?> </p>
              <a href="case-study-detail.php?pid=<?php echo $testData[1]['id']?>">View  Detail</a>
            </div>
        </div>
        </div>


        <div class="case__wrap_inner">
            <div class="image__wrapper_content">
            <img src="admin/uploads/portfolio/<?php echo $testData[0]['image'] ?>">
            <!-- <img src="assets/img/protfolio/gsceenc-thumbnail.PNG"> -->
            </div>
            <div class="content__wrapper_of_case_Study">
             <div class="top___Sec">
            
           
               <div class="middle__ineeer_sec_For_grid">
                   <div class="middle__Sec">
                   <img src="admin/uploads/portfolio/<?php echo $testData[0]['logo'] ?>">
                   <h1><?php echo $testData[0]['heading'] ?></h1>
               <p><?php echo $testData[0]['description'] ?> </p>
               <a href="case-study-detail.php?pid=<?php echo $testData[0]['id']?>">View  Detail</a>
                  </div>
               </div>
             </div>
           </div>
             </div>
         
      </div>
    </div>
  </div>
</section>

Now I want to execute a loop which will display data in this format. (1-2-1) I hope it gives the understanding top you. Thanks

Tray2's avatar

Are you using blade or vanilla PHP?

Blade

@foreach($somethings as $something)
    {{ $something->field }}
@endforeach

Vanilla

<?php
 foreach($somethings as $something) {
    echo $something['field'];
}
?>
automica's avatar

You should be doing this $testData=$CommonClass->ResultWithArrayData("SELECT * FROM portfolio WHERE status=1 ORDER BY id DESC "); in your controller and then pushing the $testData into your view.

Be wary of how big your portfolio results are. Its fine when you've got a few, but your page size and db performance will suffer when your portfolio grows to a significant size.

Please or to participate in this conversation.