Forum Moderators: coopster

Message Too Old, No Replies

Need Help On a Comparison Tool

multiple checkbox selection

         

hairycoo

2:03 pm on Dec 12, 2006 (gmt 0)

10+ Year Member



My knowledge of PHP is limited to making slight changes to existing scripts. I now have the ambition to do a comparison tool as follows.

I want to compare apples, oranges, bananas, grapes and pears. Each option would have a checkbox.

When someone checks apples, oranges and bananas, they'd be taken to static page comp_aob.html, when someone checks apples and bananas, they'd be taken to comp_ab.html.

Could someone help me get started? I'd like this to be as simple as possible but I have no idea where to start.

Thanks!

eelixduppy

9:05 pm on Dec 12, 2006 (gmt 0)



If you take the comparison approach it can get messy. Here's something real quick that works, but it is very limited and there is probably a better solution out there. But this is just to get you started :)


<?php
if(isset($_POST['fruit'])) {
$num = array('o','a','b','g','p');
$count = count($_POST['fruit']);
$url = 'comp_';
for($i = 0; $i < $count; $i++) {
$url .= $num[$_POST['fruit'][$i]];
}
$url .= '.html';
echo $url;
}

?>
<br/><br/>
<form action="index.php" method="post">
Orange<input type="checkbox" name="fruit[]" value="0" /><br />
Apple<input type="checkbox" name="fruit[]" value="1" /><br />
Banana<input type="checkbox" name="fruit[]" value="2" /><br />
Grape<input type="checkbox" name="fruit[]" value="3" /><br/>
Pear<input type="checkbox" name="fruit[]" value="4" /><br />
<input type="submit" value="Click me!" />
</form>

One problem with this is that if you do not have the check boxes in the correct order, the page's url will be wrong.

Best of luck!

eelixduppy

9:59 pm on Dec 12, 2006 (gmt 0)



I feel kind of silly, but instead of integers you can have the value of the check box equal to the letter.

That way you don't have to pull the letter from the array.

tsk tsk tsk...I'm getting worse every day ;)

mcibor

10:29 pm on Dec 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



True ;)

instead of using for loop (which I dislike) you can:

<?php
if(isset($_POST['fruit'])) {
$fruits = $_POST['fruit'];
sort($fruits);
$url = 'comp_'.implode("", $fruits).'html';
echo $url;
}

?>
<br/><br/>
<form action="index.php" method="post">
Orange<input type="checkbox" name="fruit[]" value="o" /><br />
Apple<input type="checkbox" name="fruit[]" value="a" /><br />
Banana<input type="checkbox" name="fruit[]" value="b" /><br />
Grape<input type="checkbox" name="fruit[]" value="g" /><br/>
Pear<input type="checkbox" name="fruit[]" value="p" /><br />
<input type="submit" value="Click me!" />
</form>


with sort [php.net] you won't have any problems with mixed urls
and I just prefer to implode [php.net] array than to link it manually ;)

We are all getting older Eelix
Regards HairyCoo
Michal

eelixduppy

10:38 pm on Dec 12, 2006 (gmt 0)



>>>and I just prefer to implode array than to link it manually

implode is the best solution in this case, but I first used the loop because I wasn't thinking and taking the values from an array. Anyway, your solution is better :)

>>>We are all getting older Eelix

haha...yup...Except I haven't even passed 20 yet ;)

P.S. Nice idea with the sort.