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

CSS

CSS Background image with PHP

Hi,

I am a beginner at PHP, but I have so far managed to solve my problems with some Google searches and hard thinking.

However I need help with this, probably very simple, thing.

I have a background image in an anchor tag that needs to change when hovering. This background-image is different in every anchor tag, so I have been trying to use PHP to do this.

There may be a better way to do this, but I would like to see a solution to this problem so I learn by my mistakes.

You can see my code here: http://codepen.io/antmoritz/pen/DoxGa (That code is in my .php file together with some more code below it)

And you can see the result here: http://antonmoritz.com/look/

So if you look at CodePen at line 32 and 45, there is: $projects[2]["id"]

That is now specifying the value of "id" in the array with index 2.

My question is, how do I make this to not be specific to index 2? I want all my arrays, not just the index 2 one.

Wall of text, I know, but I would really appreciate some help with this.

Thank you!

Best regards, Anton Moritz

6 Answers

Nick Pettit
STAFF
Nick Pettit
Treehouse Teacher

Hi Anton,

You're on the right track building the array and then outputting the data with a concatenated string. To output all the different background images, you'll need to write a loop that goes through each element in the array.

I used to do this using the array_walk() function a few years ago, but I'm not sure if that's still standard practice. Randy Hoyt should be able to tell you what's up. :) He's our PHP teacher.

Nick Pettit Yeah, I used a foreach loop before, but it printed out several lines of "background-image" in the CSS, so they were all overridden by the last one and therefore only showing my last array's logo.

Hopefully Randy Hoyt can help me.

Thanks Nick!

Nick Pettit
STAFF
Nick Pettit
Treehouse Teacher

Hi Anton Moritz,

I should have added, Randy Hoyt is out of the office until Monday, so it might be a little while until he can get to this post. I think Andrew Chalkley and Jim Hoskins have some PHP experience, so they might be able to help out in the meantime.

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Instead of for each try just a for:

for ($i = 0; $i < count($projects); $i++) { 
    #Use $i as your index and place your code in here.
}
Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

You have to think about what you want the final CSS to be and then work backwards to figure out what PHP you want to generate that. (I think by trying to think about the CSS and the PHP at the same time, you've actually gotten confused about the CSS, not the PHP!) You have some CSS that you want to apply to all the project links, and you have other CSS that you want to apply only to links with a specific ID. I think this is what you are trying to generate:

<style>
.projects a {
    background-repeat: no-repeat;
    background-position: center;
    background-size: 50%;   
    transition: all 0.3s ease-in-out;
    -webkit-transition: all 0.3s ease-in-out;
}
.projects a#troy {
    background-image: url('http://antonmoritz.com/img/projects/logo-troy.png');
}
.projects a#venn {
    background-image: url('http://antonmoritz.com/img/projects/logo-venn.png');
}
...
etc.
</style>

Does that look right? I think you might be able to figure it out from there, but I'll carry on just in case.

How many times do you need the first .projects a rule? Only once. Even if you add 100 more projects, you only need that rule one time.

How many times do you need a rule like .projects a#ID? You need one of those for each project. If you add a new project to the array, you want to generate one more rule like that, so that rule can go in a foreach loop.

<style>
.projects a {
    background-repeat: no-repeat;
    background-position: center;
    background-size: 50%;   
    transition: all 0.3s ease-in-out;
    -webkit-transition: all 0.3s ease-in-out;
}
<?php foreach ($projects as $project) { ?>
.projects a#<?php echo $project["id"]; ?> {
    background-image: url('http://antonmoritz.com/img/projects/logo-<?php echo $project["id"]; ?>');
<?php } ?>
</style>

Does that help?

Andrew Chalkley ,

Thank you so much for your help. I almost got it to work with a for loop, but didn't completely.


Randy Hoyt ,

This is why you are the PHP boss! Thank you, thank you, thank you!!!!

I've been struggling with this problem for so long. I can't thank you enough.

You are right, I forgot about how to make the proper CSS for this to work. I focused on the PHP.

Thanks again.

Best regards, Anton Moritz