0

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>';
}?>
3
  • 1
    Can you include the expected result? Commented Jun 2, 2014 at 23:59
  • 1
    The for loop only executes once if you have $i < 1. If this is the case, you don't need a for loop. Commented Jun 3, 2014 at 0:11
  • updated OP on what i was trying to achieve Commented Jun 3, 2014 at 0:20

1 Answer 1

2

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>
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your answer but I haven't worded my question very well. what I mean was to add a div around the h4 tag. see OP

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.