View Full Version : multiple category select

03-25-2009, 12:22 PM

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,

03-25-2009, 01:40 PM
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:

// 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:

$cat1 = $_POST['cat1'];
$cat2 = $_POST['cat2'];
$cat3 = $_POST['cat3'];

Access value from $links array using selected categories ad redirect user

$link = $links[$cat1][$cat2][$cat3];

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

03-25-2009, 02:05 PM
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 :-)

03-25-2009, 02:25 PM
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:

// 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}");

<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 name="cat2">
<option value="1">Cat2 Choice1</option>
<option value="2">Cat2 Choice2</option>
<option value="3">Cat2 Choice3</option>
<select name="cat3">
<option value="1">Cat3 Choice1</option>
<option value="2">Cat3 Choice2</option>
<option value="3">Cat3 Choice3</option>
<input type="submit" name="submit" value="Submit"/>

03-25-2009, 02:56 PM
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 :thumbsup:

03-25-2009, 03:08 PM
In that case you should use radio buttons, not check-boxes, they allow only one selection per group. Just replace:

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


<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

03-25-2009, 03:23 PM
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.