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

Tom Schinler
Tom Schinler
21,052 Points

Please help, building resume site having difficulty accessing array.

$jobs = array();

$jobs[0] = array(  
        "Company" => "JP Morgan Chase Bank N.A.",  
        "Dates" => "October, 2009 to Present",
        "Job Title" => "Personal Banker",
        "Duties" => "Sales and Service of Client Accounts and Products",
                    "Cash Handeling and Teller Drawer Balancing",            

);    


$jobs[1] = array(    
        "Company" => "Best Buy",
        "Dates" => "October, 2005 - October, 2008",
        "Job Title" => "Full Time Sales, Department Supervisor",
        "Duties" => "Sales and Service of Client Accounts and Products",
                    "Cash Handeling and Teller Drawer Balancing",            

);    

$jobs[2] = array(    
        "Company" => "RJ's Eatery",
        "Dates" => "October, 2005 to October, 2008",
        "Job Title" => "Shift Manager, Server, Cook, Delivery Driver",
        "Duties" => "Sales and Service of Client Accounts and Products",
                    "Cash Handeling and Teller Drawer Balancing",            

);    

$jobs[3] = array(    
        "Company" => "Pizza Hut",
        "Dates" => "July, 2003 - October, 2005",
        "Job Title" => "Shift Manager, Delivery Driver, Server",
       "Duties" => "Sales and Service of Client Accounts and Products",
                    "Cash Handeling and Teller Drawer Balancing",            

);

This is my array

  <section id="resume" class="grid_6">
            <!-- includes array of companies worked for that can be clicked to bring up light box with job info -->
                <ul class="jobsUl">
                    <?php foreach ($jobs as $job) { ?>
                    <li>
                        <a class="popup-with-zoom-anim" href="#small-dialog"><p> <?php echo $job["Company"]; ?> </p></a>
                    </li>
                    <?php  } ?>
                </ul>
                <div id="small-dialog" class="zoom-anim-dialog mfp-hide">
                    <h1>The Details</h1>
                    <?php foreach ( $jobs as $job ) { ?>
                    <?php echo '<dl>';
                         foreach ( $job as $key => $value ) {
                         echo "<dt>$key</dt><dd>$value</dd>";
                        }
                        echo '</dl>';
                    } ?>
                </div>

            </section>

And this is my HTML calling my array. The UL appears, but when I click a link and the lightbox pops up, It shows data for all companies instead of just the linked one. Where am I going wrong? Would a While loop be better?

1 Answer

Kevin Korte
Kevin Korte
28,149 Points

The problem is that all of this php is running before it even gets to the browser. If you inspect the html code before clicking anything, your small dialog for loop should already be filled with all of your work again.

If I were to do it, I think I would use jQuery to get a value or data-attribute of which link was clicked on. I'd make an ajax call and send that value back to the server. Than I'd use some conditionals to run the correct for each statement only for the array that matches the value it was passed. Than return this html back to the browser with that ajax call we made.

Otherwise, the only other way I can think of doing this is still getting a value from the link click, and using jQuery to hide all the job data that doesn't match.

I much prefer the ajax way though.

Hey Kevin, I've always wondered - would ajax be considered obtrusive javascript in this scenario? (Even if the requested information appeared on a new page and not a modal popup?)