1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Checkbox count

Discussion in 'PHP' started by Omzy, Dec 28, 2008.

  1. #1
    Basically I've got a list of checkboxes on my page, one of the checkboxes is labelled 'Other' and has a value of 'other'.

    Each checkbox name is "selection[]" and this is read in by the PHP script. The PHP script does the following:

    if($_POST['selection'] == null || count($_POST['selection']) > 3)
    Code (markup):
    and outputs an error if there are no checkboxes ticked or if there are more than 3 checkboxes ticked.

    I need the script to do the following:

    If any of the 'normal' checkboxes are ticked + the 'Other' checkbox is ticked, then the count() mustn't add the 'Other' checkbox to the count().

    If no 'normal' checkboxes are ticked but the 'Other' checkbox is ticked then the count() functions as normal and has a value of 1.

    The catch however is that the 'Other' checkbox cannot have a different name to the rest, it must be "selection[]".
     
    Omzy, Dec 28, 2008 IP
  2. hackfanatic

    hackfanatic Peon

    Messages:
    29
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try and give this a shot! i think this is what u need

    
    $arraystuff = $_POST['selection'];
    
    $counter = count($arraystuff);
    
    if ( array_search( "other", $arraystuff ) != false){
    		if ( ( $counter - 1 ) > 1 ) $counter-= 1;
    }
    
    echo $counter;
    
    Code (markup):
     
    hackfanatic, Dec 28, 2008 IP
  3. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Cheers mate, I think that's working now. The only problem is that now it gives the following error when NO checkboxes have been ticked:

    "Warning: array_search() [function.array-search]: Wrong datatype for second argument"

    I know why it's saying that but do you know how I can fix this?
     
    Omzy, Dec 28, 2008 IP
  4. hackfanatic

    hackfanatic Peon

    Messages:
    29
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The variable $_POST['selection'] doesn't exist if no checkboxes have been selected so we could check for that

    
    <?
    if( isset($_POST['selection']) ) {
    	$arraystuff = $_POST['selection'];
    	$counter = count($arraystuff);
    
    	if ( array_search( "other", $arraystuff ) != false){
    			if ( ( $counter - 1 ) >= 1 ) $counter-= 1;
    	}
    }else $counter = 0;
    
    echo $counter;
    ?>
    
    Code (markup):
    also, please change the condition to "( $counter - 1 ) >= 1" small logical error..! that would still display count of both, if "other" and just one "normal" would be selected..

    Cheers, :)
     
    hackfanatic, Dec 28, 2008 IP