0

So I'm having trouble getting my html to show up in my php script foreach loop. I included my arrays in my header.php page which is included in the webpage that I'm building right now. Right now my website only shows an empty well since I'm using bootstrap.

I've already tried just putting regular html in the div and it works, which means there is something wrong in the php script.

My php arrays

    <?php
    //Teams
    $teams = array(
            "fnatic" => array(
                    title => "Fnatic",
                    teamLocation => "Sweden"
            ),
            "tsm" => array(
                    title => "TSM",
                    teamLocation => "Denmark"
            ),

            "envyus" => array(
                    title => "EnvyUs",
                    teamLocation => "France"
            ),
        );
?>

Here is my html. The middle section where is where I'm trying to have my foreach loop display. I already included my header.php, which has my arrays, and my footer.php, which has all my scripts.

<?php
    define('TITLE', 'PROS | CSGOPLAYER CONFIGS');
    include('includes/header.php');
    include('includes/minicarousel.php');

?>

<div class="container-fluid">
    <div class="row">
        <div class="well">
            <?php foreach ($teams as $team => $item) { ?>
            <h1><a href="team.php?item=<?php echo $team; ?>"><?php echo $item[title]; ?></h1>
            <p><?php echo $item[teamLocation];?></p>
            <?php } ?>

        </div>
    </div>
</div><!--Contect-->


<?php
    include('includes/footer.php');
?>
4
  • $item[title] should be $item['title']. But PHP will convert it for you, although it prints a warning when it does this. Commented Oct 9, 2015 at 22:29
  • You're missing </a> Commented Oct 9, 2015 at 22:30
  • @Barmar but this would not solve the problem. Can you add var_dump($teams); right before your foreach loop Commented Oct 9, 2015 at 22:30
  • @esel I know, that's why I said it will convert it. And the browser will add the missing </a>. Commented Oct 9, 2015 at 22:31

4 Answers 4

2

My suggestion would be to do a var_dump of $teams in the well, and then exit() the script to verify the teams are correctly primed.

Also, I'd advise using the following syntax as I feel it's clearer, but your mileage may of course vary:

<?php
    $well = '';
    foreach ($teams as $team => $item) {
        $well .= <<<WELL
        <h1><a href="team.php?item={$team}">{$item['title']}</a></h1>
        <p>{$item['teamLocation']}</p>
WELL;
// The above line must start at column 1, to work as here-document
    }
    print $well;
?>

Also, the syntax you use should issue lots of warnings of unquoted strings. The fact that you don't see those warnings tells me that you have error_reporting set too low, and possibly error display turned off as well. And this could explain why you see an empty well instead of a helpful error message.

   title => "Fnatic",
         teamLocation => "Sweden"

and so on, should be

   'title' => 'Fnatic',
         'teamLocation' => 'Sweden'

Single or double quote is a matter of choice, but it's best if some quotes are always in place.

All that said, your code ought to be working, so consider the possibility there might be an error elsewhere.

Sign up to request clarification or add additional context in comments.

1 Comment

+1, I posted an answer thinking everyone had missed the fact that the OP had defined the array wrong, then saw your bottom part (>.<).
0

You are missing your a tag closer. Also consider enabling short_open_tags in your PHP and use <? and ?> (more efficient). Try print instead of echo. What happens when you execute print_r($item) within the loop?

<div class="container-fluid">
    <div class="row">
        <div class="well">
            <?php
                foreach ($teams as $team => $item) { 
                    ?>
                        <h1>
                           <a href="team.php?item=<?php echo $team; ?>">
                               <?php 
                                   print $item[title]; 
                               ?>
                           </a>
                        </h1>
                        <p>
                            <?php 
                                print $item[teamLocation];
                            ?>
                        </p>
                   <?php 
                } 
            ?>
        </div>
    </div>
</div><!--Contect-->

3 Comments

why should the OP try print instead of echo?
it is largely a matter of personal taste, but short tags are usually discouraged (php.net/manual/en/language.basic-syntax.phptags.php). Efficiency increase is negligible, and then you might have to deploy on a server where short_open_tags aren't enabled and cannot be. Been there, done that, got the T-shirt and a serious pissing off myself :-)
Because it's one thing to try. Didn't see anything else wrong with the code, until a second or third look and noticed the array index ticks missing.
0

You just should remove that obsolete comma.

    $teams = array(
        "fnatic" => array(
                title => "Fnatic",
                teamLocation => "Sweden"
        ),
        "tsm" => array(
                title => "TSM",
                teamLocation => "Denmark"
        ),

        "envyus" => array(
                title => "EnvyUs",
                teamLocation => "France"
        )
    );

1 Comment

That shouldn't make a difference - a trailing comma in an array declaration won't generate an error
0

in your header

<?php
$html = array();
foreach($teams as $team => $item){
    $html[] = "<h1><a href='team.php?item=".$team."'>".$item['title']."</a><h1>".<p>".$team['teamLocation']."</p>"; 
}
?>

inside of div.well

<?php echo implode('' , $html); ?>;

also your array keys should be wrapped in qoutes like:

...

'title'=>'Fnatic',

...

Comments

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.