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
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>";


}
?>
Link to comment
Share on other sites

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>";
}


Edited by Ch0cu3r
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.