John Davidson

mysql - How to insert PHP loop into a div that was created by PHP?

0 comments
Message:


I have looped through the $elements 6 times to create 6 identical containers for each service a company offers. I'm trying to put the "if while" loop at the bottom of the php inside the $element4 'overlay' div. The "if while" loop calls the service title from the MySQL database and displays it on the webpage. I have tried pasting the php code directly between the div tags, I have tried leaving it where it is, I have tried putting the "if while" loop in its own file and putting the include statement between the 'overlay' div tags. Nothing is working. With previous attempts, I have been able to get the h3 tags to populate in the wrong spots in the DOM, but I can't get them where I want them for some reason. Can you please tell me how I can get the output of the "if while" loop to display in the "overlay" div?`


  $element = "<div class='service'>";
$element2= "<div class='img_container'>";
$element3= "<img class='service_img'/></div>";
$element4= "<div class='overlay'></div>";
$element5= "<div class='service_details'></div></div>";
$count = 6;
foreach( range(1,$count) as $item){
echo $element, $element2, $element3, $element4, $element5;

}

?>

<?php

if($resultCheck > 0){
while($row = $result->fetch_assoc()){
echo "<h3 class='service_title'>" . $row['service_title'] . "<br>" . "</h3>";
}}

?>



My Answer:
To insert the output of the "if while" loop into the "overlay" div, you can modify your code as follows:

php
$element = "
";
$element2= "
";
$element3= "
";
$element4= "
";
$element5= "
";
$count = 6;
foreach( range(1,$count) as $item){
echo $element, $element2, $element3, $element4, $element5;

if($resultCheck > 0){
while($row = $result->fetch_assoc()){
echo "

" . $row['service_title'] . "

";
}
}
}


By placing the "if while" loop inside the foreach loop, the service titles fetched from the database will be displayed within each "overlay" div that is created for each service. This way, the service titles will be displayed in the correct location within the DOM structure.

Rate this post

3 of 5 based on 6677 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us