Support

Account

Home Forums General Issues Use selected checkbox values in a URL

Solved

Use selected checkbox values in a URL

    • lerq

    • November 1, 2013 at 6:39 am

    Hi there,

    I’m probably going about this all the wrong way, so I’m looking for a bit of assistance!

    Essentially, I’ve got a list of areas that people work in set up as a checkbox list. I’ve also got separate pages created for each area.

    What I’m trying to do is loop round the array to pull out the values of each selected area and append them to a partial URL to complete it to match the page URL.

    I’ve gone round and round, but so far have got this!:

    <?php
         $values = get_field('regions-listed-in');
         if($values)
             {
             foreach($values as $value)
                {
                   <a href="http://test/inhouse/$value">$value</a>
                }
             }
    ?>

    Any help will be much appreciated.

    Cheers,
    Lerq

    • Elliot

    • November 3, 2013 at 8:21 pm

    Hi @lerq

    Can you give some examples of expected results for both 1 value checked and 3 values checked?

    Will there ever be multiple values checked? Does each checked value create a new link? Or does it add to the 1 link url?

    Perhaps all you are missing is the correct PHP syntax:

    
    echo ' <a href="http://test/inhouse/' . $value . '">$value</a>';
    
    • lerq

    • November 5, 2013 at 1:04 am

    Hi Elliot,

    Thanks for the response.

    Yup, there will often be multiple values selected, and I’d like them to point to different URLs.

    For example, if there are 5 regions available in the checkbox list (a – e), and Joe works in 3 of them (a, b and d) I’d like the output to be:

    “Joe is available in: a b d”

    with the a b and d text linking to the respective area pages (http://test/inhouse/a, http://test/inhouse/b and http://test/inhouse/d)

    If Jane only works in area c, then the output would be:

    “Jane is available in: c”

    with the c linking to http://test/inhouse/c

    Cheers,
    Lerq

    • Elliot

    • November 5, 2013 at 2:51 pm

    Hi @lerq

    Thanks for the info.

    Yep, your code should look like your origional loop, but use my ‘echo’ code within the loop to render the link.

    Easy

    Thanks
    E

    • lerq

    • November 5, 2013 at 6:44 pm

    Hi Elliot,

    Brilliant! Thank you for your help on this, works a treat!

    Although, I had to wrap the second $value (that displays the text for the link) in the single quotes and full stops to get it to render the text correctly:

    <?php
        $values = get_field('regions-listed-in');
        if($values)
            {
                foreach($values as $value)
                    {
                     echo ' <a href="http://test/inhouse1/explore/' . $value . '">' . $value . '</a>';
                     }
             }
    ?>
Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.