Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Dec 2007
    Location
    on earth
    Posts
    183
    Thanks
    49
    Thanked 2 Times in 2 Posts

    multiple category select

    Hello,

    I am not sure how to get around this: There are 3 categories of options to choose from, and in each category 3 options, of which only one can be chosen. The combination gives a specific url link.
    The ideal would be multiple check boxes to click (3 categories, 3 options per category, 1 option to choose per category), with an on-submit linking action. That would give a total of 9 clicks, of which only 3 can be clicked since 1 per category only. (27 combinations?).
    I dont even know if this is a php thing to do. Does anyone know of a good script or site to get that working? :-)

    Thank you very much,
    Mike
    Last edited by mikacruz; 03-25-2009 at 11:29 AM.

  • #2
    New Coder
    Join Date
    Mar 2009
    Location
    Chicago, IL
    Posts
    69
    Thanks
    0
    Thanked 15 Times in 15 Posts
    If I understand your question correctly, you want to redirect user based on 3 categories each having 3 choices that he selects. If so, you can define your links at the top of submit page using multidimensional array:

    PHP Code:
    // Format: $links[cat1 choice][cat2 choice][cat3 choice] = "link";
    $links[1][1][1] = "http://redirect1.com";
    $links[1][2][3] = "http://redirect2.com";
    $links[3][2][1] = "http://redirect3.com";
    // Etc. 
    After form is submitted, get result for each category:
    PHP Code:
    $cat1 $_POST['cat1'];
    $cat2 $_POST['cat2'];
    $cat3 $_POST['cat3']; 
    Access value from $links array using selected categories ad redirect user
    PHP Code:
    $link $links[$cat1][$cat2][$cat3];

    header("Location: {$link}"); 

  • Users who have thanked steelaz for this post:

    mikacruz (03-25-2009)

  • #3
    Regular Coder
    Join Date
    Dec 2007
    Location
    on earth
    Posts
    183
    Thanks
    49
    Thanked 2 Times in 2 Posts
    Thanks a lot,
    I am going to try understand how to implement this into the page, such as associating this with select boxes under each option...
    To be honest your answer is a bit of a mystery to my miserable knowledge of php :-)

  • #4
    New Coder
    Join Date
    Mar 2009
    Location
    Chicago, IL
    Posts
    69
    Thanks
    0
    Thanked 15 Times in 15 Posts
    Here's very basic example, save the code below as demo.php and try running it. If you leave all drop-downs at choice1 it will redirect to google:

    PHP Code:
    <?php
    // Define your links
    $links[1][1][1] = "http://www.google.com";
    $links[1][2][3] = "http://www.yahoo.com";
    $links[3][2][1] = "http://www.reddit.com";

    // Check if form was submitted
    if (isset($_POST['submit']))
    {
        
    // Get selected options
        
    $cat1 $_POST['cat1'];
        
    $cat2 $_POST['cat2'];
        
    $cat3 $_POST['cat3'];
        
        
    // Get link from array
        
    $link = @$links[$cat1][$cat2][$cat3];
        
        
    // If link was found - redirect
        
    if (!empty($link)) header("Location: {$link}");
    }
    ?>

    <html>
    <body>
        <form method="post" action="demo.php">
            <select name="cat1">
                <option value="1">Cat1 Choice1</option>
                <option value="2">Cat1 Choice2</option>
                <option value="3">Cat1 Choice3</option>
            </select>
            <select name="cat2">
                <option value="1">Cat2 Choice1</option>
                <option value="2">Cat2 Choice2</option>
                <option value="3">Cat2 Choice3</option>
            </select>
            <select name="cat3">
                <option value="1">Cat3 Choice1</option>
                <option value="2">Cat3 Choice2</option>
                <option value="3">Cat3 Choice3</option>
            </select>
            <input type="submit" name="submit" value="Submit"/>
        </form>
    </body>
    </html>

  • #5
    Regular Coder
    Join Date
    Dec 2007
    Location
    on earth
    Posts
    183
    Thanks
    49
    Thanked 2 Times in 2 Posts
    ok i see. the thing is that I need images for each options, hence the need for check boxes that you tick for each category, rather than a scrolling menu...
    Will keep your script though because it will definitely come useful later. Nice one

  • #6
    New Coder
    Join Date
    Mar 2009
    Location
    Chicago, IL
    Posts
    69
    Thanks
    0
    Thanked 15 Times in 15 Posts
    In that case you should use radio buttons, not check-boxes, they allow only one selection per group. Just replace:

    PHP Code:
    <select name="cat1">
        <
    option value="1">Cat1 Choice1</option>
        <
    option value="2">Cat1 Choice2</option>
        <
    option value="3">Cat1 Choice3</option>
    </
    select
    With

    PHP Code:
    <input type="radio" name="cat1" value="1">Cat1 Choice1 
    <input type="radio" name="cat1" value="2">Cat1 Choice2
    <input type="radio" name="cat1" value="3">Cat1 Choice3 

  • Users who have thanked steelaz for this post:

    mikacruz (03-25-2009)

  • #7
    Regular Coder
    Join Date
    Dec 2007
    Location
    on earth
    Posts
    183
    Thanks
    49
    Thanked 2 Times in 2 Posts
    great, that seems perfect. Now I'm going to try to get these categories have their own style... and shall ask the css department if any issues occur at this level :-)
    thanks a lot for putting down the codes, perfectly helpful.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •