Need Help On a Comparison Tool (multiple checkbox selection)

Discussion in 'PHP' started by bluecorr, Dec 12, 2006.

  1. #1
    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!
     
    bluecorr, Dec 12, 2006 IP
  2. kkibak

    kkibak Peon

    Messages:
    1,083
    Likes Received:
    78
    Best Answers:
    0
    Trophy Points:
    0
    #2
    This is pretty easy to do with PHP--that's the good news :)

    Basically you have 5 checkboxes and there are two possible combinations that would forward to another URL.

    it would go something like this (leaving out the html):

    
    if(!(empty($_POST['apples'])) AND !(empty($_POST['oranges'])) AND !(empty($_POST['bananas'])) AND empty($_POST['grapes']) AND empty($_POST['pears'])) {
      header( 'Location: http://www.URL.com/comp_aob.html' );
    }
    if(!(empty($_POST['apples'])) AND !(empty($_POST['bananas'])) AND empty($_POST['oranges']) AND empty($_POST['grapes']) AND empty($_POST['pears'])) {
      header( 'Location: http://www.URL.com/comp_ab.html' );
    }
    
    Code (markup):
    This isn't a very eloquent way of doing, but with only two possibilities that's probably okay. The example I gave assumes you named the checkboxes by their fruit name.
     
    kkibak, Dec 12, 2006 IP
  3. Krownet

    Krownet Peon

    Messages:
    42
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Are you looking for something more complicated? Such as multiple options depending on what they chose. So they could choose aob, or ob, or ab, or ....

    If you are the process is roughly the same number of lines, and I can provide you with this code. Just let me know.

    Cheers,
     
    Krownet, Dec 12, 2006 IP
  4. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Just extending the answer of kkibak, I'd do something like:

    
    $url = 'http://www.URL.com/comp_';
    
    if ( ! empty( $_POST[ 'apples' ] ) ) {
        $url .= 'a';
    }
    
    if ( ! empty( $_POST[ 'oranges' ] ) ) {
        $url .= 'o';
    }
    
    if ( ! empty( $_POST[ 'bananas' ] ) ) {
        $url .= 'b';
    }
    
    header( 'Location: ' . $url . '.html' );
    
    PHP:
     
    TwistMyArm, Dec 13, 2006 IP
  5. Krownet

    Krownet Peon

    Messages:
    42
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Or you could just use this.

    This is your checkboxes:

    
    <input type="checkbox" name="c[0]" value="a" />
    <input type="checkbox" name="c[1]" value="o" />
    <input type="checkbox" name="c[2]" value="b" />
    <input type="checkbox" name="c[3]" value="g" />
    <input type="checkbox" name="c[4]" value="p" />
    
    Code (markup):
    then your PHP code after they submit:

    
    $url = '';
    if (isset($_POST['c'])) {
    	foreach ($_POST['c'] as $key => $value) {
    		$url .= $value;
    	}
    }
    header("Location: comp_" . $url . ".html");
    
    PHP:
    Or is that a little more complicated than needed?

    Cheers,
     
    Krownet, Dec 13, 2006 IP