Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

What is Wrong With My PHP Code?

Hey guys,

So I'm building a site for a local touring company and I have hit a bit of a snag with some PHP code that I just can't seem to get working correctly.

I'm pretty much using the same code that Randy Hoyt used for the "Build a Simple PHP Application" project. I believe that I am using an associative array. I will try and best describe the issue that I am having, as I am still trying to get my head around PHP.

The Problem

So I have 4 grid columns for some quick links, each column has a button and a image directly below below the button when I implement the below code the layout of each column will break and button will be spewed all over it's containing grid column div and the image will display the error icon. Also the footer area removes it's margin and adds a lot of height.

[alt text](imgur.com/H6rTptA "Title")

The Code

Here is the PHP code that I am trying to get to work

index.php

<?php 
    $quickLinks = array();

    $quickLinks["quickLinkA"] = array(
        "title"     =>  "Tours",
        "img"           =>  "images/info-tours.png",
        "link"      =>  "tourism/tours.php"
    );
    $quickLinks["quickLinkB"] = array(
        "title"     =>  "Camping",
        "img"           =>  "images/info-camping.png",
        "link"      =>  "tourism/camping.php"
    );
    $quickLinks["quickLinkC"] = array(
        "title"     =>  "School Groups",
        "img"           =>  "images/info-school",
        "link"      =>  "tourism/school-groups.php"
    );
    $quickLinks["quickLinkD"] = array(
        "title"     =>  "Bird Watching Tours",
        "img"           =>  "images/info-birds.php"
        "link"      =>  "birds/tours.php"
    );
?>

<div class="col-sm-3 info-column">
    <?php foreach ($quickLinks as $quickLink) {
        echo "<p>";
    echo '<a class="btn btn-warning href="' . $quickLink["link"]; '">"';
    echo "</p>";
    echo "<p>";
    echo '<img src="' . $quickLink["img"]; '"alt="' . $quickLink["title"]; '">"';
    echo "</p>";
    } ?>
</div>

I am aware that I have only declared one column, but this is to purely save on using up heaps of space. For Your Information also, I am using Twitter's Bootstrap Framework for this project.

I would really appreciate some advice on how this can be fixed.

Thanking you in advanced,

Stu :)

3 Answers

Chris Malcolm
Chris Malcolm
2,909 Points
  echo '<a class="btn btn-warning href="' . $quickLink["link"]; '">"';

this will result in a parse error or html mistake due to a few reasons..the following occurs multiple times in your code, perhaps try fixing this will help.

  • a semicolon instead of a period.

html syntax is also messed up

  • double quotes are used in places they shouldn't be.
  • A tag is also left open.

ex to change to..

  echo '<a class="btn btn-warning" href="' . $quickLink["link"]. '">SOme link</a>';

ANOTHER SUGGESTION

it seems like your confusing yourself with syntax of html and php. for these kinds of things i like to write plain html and insert php with <?= ?>.

<?php
/* Your php before html */
?>

<!-- start plain html with php embeds -->
<p>
<a class="btn btn-warning" href="<?=$quickLink["link"]?>">Some link</a>
</p>
<p>
<img src="<?=$quickLink["img"]?>" alt="<?=$quickLink["title"]?>">
</p>
<!-- END plain html with php embeds -->

<?php
/* we're back to php */
?>
David Omar
David Omar
5,676 Points

You are missing a comma on your quickLinkD array on the img value.

<?php
$quickLinks["quickLinkD"] = array(
        "title"     =>  "Bird Watching Tours",
        "img"           =>  "images/info-birds.php",
        "link"      =>  "birds/tours.php"
    );
?>

Hey Chris, this works perfectly, I do have the problem of messing up my commas, your instructions were perfectly written out without confusing the hell of me. Absolute champ, cannot thank you enough :)

Stu :)