Why this codes works? (Bootstrap)

The codes,


	<?php
	echo "<div class='row'>";
		if(!empty($records)) {
			foreach($records as $record) {
	?> 
				        <div class="span6">
				        	<div id="" align="left">
				        		<?php echo '<h2>'. $record['title'] .'</h2>'; ?>
				        	</div>
				        </div>
	<?php
			} 
		}
	echo "</div>";		
	?>

You can see it here,
http://coder9.com/jazportfolio/

Thanks.

If you try to look at the codes.
There is only one


<div class="span6">
</div>

But as you can see it prints two columns meaning it prints two of these codes below,


<div class="span6">
</div>

Thanks in advanced for explaining why it works?

Welcome to Sitepoint.

I suspect it is printing every record, but ‘the last’ record has no [title] value.
Try this, as a test:


<div class="row">
<?php
if(!empty($records)) {
	foreach($records as $record) {
?> 
        <div class="span6">
        	<div align="left">
        	<h2><?php echo $record['title']; ?></h2><p>The $record looks like this: <?php var_dump($record); ?></p>
        	</div>
        </div>
<?php
} 
	}
?>
</div>

Also, you may notice the ‘literal’ strings do not need to be enclosed in PHP tags and 'echo’ed.

I hope that is helpful. And the result may allow someone else to better assist you.
My PHP skills are very rusty.

I think I should rephrase my explanation.
It prints all the records along with these codes below,


<div class="span6">
</div>

The only odd is It has only one of this codes,


<div class='row'>
</div>

The good thing here is it doesn’t show any error message.
The bad things is, I guess it is against the rule of Bootstrap?

Thank you very much in advanced, If you give shed to this matter.

That is correct; based on the way it is coded. There is only one

<div class='row'>
</div>

and it is outside (not inside) the loop. That means it will appear exactly as you placed it (as if there is no other PHP code).
If you move it within the loop, it will be printed for each iteration of the loop.

If your intention is to style each row based on the number you can do this (although I would restructure the loop to use a counter and not “foreach”):


<?php
$odd = true;
if(!empty($records)) {
	foreach($records as $record) {
?> 
   <div class="<?php echo ($odd) ? 'odd' : 'even'; ?>">
        <div class="span6">
        	<div align="left">
        	<h2><?php echo $record['title']; ?></h2>
        	</div>
        </div>
</div>
<?php
    $odd = !$odd;
    } 
}
?>