Jump to content

PHP Loop to Echo HTML and some PHP code


blmg2009

Recommended Posts

I'm currently learning PHP so I'm a noob; 

 

I'm looking at looping form 1 to 64 and on each loop echoing out html to build a table. This I can do easily. 

 

However I also want to add in some PHP in the output of each loop; However I'm not sure how I would do this correctly.

 

 

 
 for ($i = 1; $i <= 64; $i++)
{
 
echo 
 
<div class="col-xs-3 team_box top-team-border"><?php echo $results->get_team('64', '<?php echo $i; ?>', 'A', 'main'); ?> <span class="badge"><?php echo $results->get_score('64', '<?php echo $i; ?>', 'A', 'main'); ?></span></div>
 
}
 
Link to comment
https://forums.phpfreaks.com/topic/296795-php-loop-to-echo-html-and-some-php-code/
Share on other sites

Is a few ways actually, you have to echo html in quotes if within php or break in and out of php using <?php ?> tags

<?php
for ($i = 1; $i <= 64; $i++)
{
?> 
 
<div class="col-xs-3 team_box top-team-border"><?php echo $results->get_team('64', '<?php echo $i; ?>', 'A', 'main'); ?> <span class="badge"><?php echo $results->get_score('64', '<?php echo $i; ?>', 'A', 'main'); ?></span></div>

<?php 
}
?>

or

<?php
for ($i = 1; $i <= 64; $i++)
{

 
echo "<div class='col-xs-3 team_box top-team-border'>".$results->get_team('64', '".$i."', 'A', 'main')."<span class='badge'>".$results->get_score('64', '".$i."', 'A', 'main')."</span></div>";


}
?>

You dont use PHP tags within a echo statement, You need to use the concatenation operator when outputting multiple things within your string

for ($i = 1; $i <= 64; $i++)
{
    echo '
    <div class="col-xs-3 team_box top-team-border">' . 
        $results->get_team('64', $i, 'A', 'main') . 
        '<span class="badge">' . $results->get_score('64', $i, 'A', 'main') . '</span>
    </div>';
}

// Or assign the team and score to variables and use them in your string

for ($i = 1; $i <= 64; $i++)
{
    $team = $results->get_team('64', $i, 'A', 'main');
    $score = $results->get_score('64', $i, 'A', 'main');

    echo "
    <div class=\"col-xs-3 team_box top-team-border\">
        $team
        <span class=\"badge\">$score</span>
    </div>";
}


Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.