SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Jul 2006
    Location
    Fionnphort, Isle of Mull, Scotland
    Posts
    337
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Counting SOME of the key/value pairs in a GET string

    I'm converting a simple accommodation listing web site to be database driven. The visitor is to be able to select accommodation by six types and six locations, plus some other criteria. I did this with 'Select' boxes, but have been asked to change this to checkboxes so the visitor can select more than one accommodation type.

    I've named the six accommodation type checkboxes 'ac0', ac1', etc. (ac0 = all types). I can handle this so long as the number remains six, using a 'for' loop:

    Code:
    $where = "WHERE "; 
    if (isset($_GET) [other conditions]) {
    //	If ac0 is set, no need to filter on accn_type
    	for ($i = 1; $i < 6; $i++) {
    		if (isset($_GET['ac'.$i])) {
    			$where .= "(accn_type = '" . $_GET['ac'.$i] . "' or ";
    		}
    	}
    //	Remove trailing 'or' and excess leading brackets from where clause
    	$where = substr($where, 0, -4) . ")";
    	$where = str_replace("or (", "or ", $where);
    //	echo $where;
    //	exit();
    }
    This gives me a valid 'where' clause for MySQL.

    Now here's my problem:
    If I were to add or remove an accommodation type I'd have to change the '$i < 6' in the 'for' loop, and similar fixed integers further on in the script. It would be better to have '$i < count(something)'.

    I'm trying to find a way to count the number of parameters in the $_GET with key 'ac?' where '?' is an integer (currently 0-5).

    I could run another 'for' loop, but I was hoping to find something smarter. I'll have to do the same for the locations too.

    Any suggestions, please ?
    Tim Dawson
    Isle of Mull, Scotland

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    There are a couple of ways, I prefer the latter, use array notation for your checkbox names then iterate the array as normal.

    Both examples allow for unlimited boxes.
    PHP Code:
    <?php
    #?ac_0=A&ac_1=B&ac_2=C&ac_3=D&ac_4=E&ac_5=F
    $i 0;
    while(
    true === isset($_GET["ac_{$i}"]))
    {
        echo 
    $_GET["ac_{$i}"];
        
    $i++;
    }
    #ABCDE

    #?ac[0]=A&ac[1]=B&ac[2]=C&ac[3]=D&ac[4]=E&ac[5]=F
    foreach($_GET['ac'] as $key => $value)
    {
        echo 
    $value;
    }
    #ABCDEF
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Addict
    Join Date
    Jul 2006
    Location
    Fionnphort, Isle of Mull, Scotland
    Posts
    337
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thank you. I'll work on this.

    I think it may also solve a problem I realised only after I'd posted my question: with checkboxes you only get a parameter in the GET string if they are checked, so counting the occurrences of 'ac?' in the GET isn't any help in my original method.
    Tim Dawson
    Isle of Mull, Scotland


Bookmarks

Posting Permissions

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