user248305
user248305

Reputation: 65

PHP wrapping html in between loop

Firstly I am a complete PHP novice so please be patient. I am trying to wrap a div around elements such as the "h4" tag without disrupting the for loop . is this possible? Thanks

<?php 
$con=mysqli_connect("localhost","root","XXXXXXXXXXXXXXXXXXXXXX","XXXXXXXXXXXXXXXXXXXXXXXX");
// Check connection
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM caseStudies");
$row = mysqli_fetch_array($result);
for($i = 0; $i < 1; $i++) {
    echo "<div id='my-div-$i'></div>";
    echo'<h4>' . $row['caseName'] . '</h4>';
}

what i want to happen:

<?php
$con=mysqli_connect("localhost","root","xxx","xxx");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM caseStudies");
$row = mysqli_fetch_array($result);
for($i = 0; $i < 1; $i++) {
echo "<div id='my-div-$i'>";
echo '<div class="header">';
echo '<h4>' . $row['caseName'] . '</h4>';
echo '</div>';
echo '</div>';
}?>

Upvotes: 0

Views: 95

Answers (1)

Ethan Brouwer
Ethan Brouwer

Reputation: 1005

Try this. You can always split up the opening and closing divs before and after the h4 tag. (Apologies about before. I worded that wrong although I made the code to put the div around the h4 tags)

$con=mysqli_connect("localhost","root","XXXXXXXXXXXXXXXXXXXXXX","XXXXXXXXXXXXXXXXXXXXXXXX");
// Check connection
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM caseStudies");
$row = mysqli_fetch_array($result);
for($i = 0; $i < 1; $i++) {
    echo "<div id='my-div-$i'>";
    echo '<h4>' . $row['caseName'] . '</h4>';
    echo "</div>"
}


<div class="header">
echo '<h4>' . $row['caseName'] . '</h4>';
</div>

Upvotes: 2

Related Questions