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
Derrick Lee
7,743 Points[PHP] I need help with retrieving values in arrays
Hi guys!
I have an array called services, in the services array, it holds an array of keys and values, for example title, overview etc. In one of the key called sub-services, I have an array of sub-services such as "Logos, "Business Cards".
The way i display my services in my page is through rows and columns. I am using Foundation 3 by Zurb.
I want to make a function to dynamically display my services from the array.
- I am able to specify how many columns I want the portfolio to be.
Foundation 3 works on 12 columns, If i want to have 3 service per row, each service has to be 4 columns. How do i make the loop so that if I specified to have a 3-column layout, It will count the total of sub-services of the service, for the first 3 services, put them in the first row, for the next 3 services, put them in the next row and the next few services in the new row, and so on.
Also, how do i make it so that, if the remainder does not equate to 3, then i will have to give it a class of "end" at the last service so that it doesn't float all the way to the right?
1 Answer
Randy Hoyt
Treehouse Guest TeacherYou want to use the modulus operator, a percent sign (%):
echo 1 % 3; // 1
echo 2 % 3; // 2
echo 3 % 3; // 0
echo 4 % 3; // 1
You want something like this:
if (($i % 3) == 0) {
echo "end";
}
Here's a link to the PHP manual with that operator: http://php.net/manual/en/language.operators.arithmetic.php
Does that help?