SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Wizard
    Join Date
    Mar 2008
    Location
    United Kingdom
    Posts
    1,285
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Two checkboxes - PHP Query

    Hi,

    I have 2 check boxes on a form on my web site.

    <input name="permanent" id="permanent" value="1" />
    <input name="temp" id="temp" value="2" />

    Now I'm basically using the values so I can query my database for any permanent or temp jobs (WHERE type = '1').

    However, I'm a little unsure. What if the user selects both or none?

    Would this mean using an IF statement, and how would I then use this with query in my db.

    So I've got 4 instances.

    Only selected temp
    Only selected permanent
    Selected both
    Selected none


    Any takers?


    Many thanks

  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)
    Quick stab before lunch.
    PHP Code:
    <?php
    $sSQL 
    'SELECT * FROM jobs WHERE 1 = 1';

    switch(
    count($_POST['contract_type']))
    {
        case 
    2:
            
    $sSQL .= " AND contract_type = 'permanent' AND contract_type = 'temporary'";
        break;
        
        case 
    1:
            
    $sSQL .= sprintf(
                
    " AND contract_type = '%s'",
                
    array_key_exists('permanent'$_POST['contract_type']) ? 'permanent' 'temporary'
            
    );
        break;

        default:
            
    #nowt.
        
    break;
    }
    ?>
    Not very flexible though.

    Or, maybe...

    PHP Code:
    <?php
    $sSQL 
    'SELECT * FROM jobs WHERE 1 = 1';
    if(
    is_array($_POST['contract_type']))
    {
        foreach(
    array_keys($_POST['contract_type']) as $sCondition)
        {
            
    $sSQL .= sprintf(
                
    " AND contract_type = '%s'",
                
    mysql_real_escape_string($sCondition)
            );
        }
    }
    ?>
    @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 Wizard
    Join Date
    Mar 2008
    Location
    United Kingdom
    Posts
    1,285
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Anthony,

    Thanks for this.

    Will give it a crack and get back.

    Off for lunch in a mo.

  4. #4
    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)
    Additionally, I guess the 'AND' could also be an 'OR' dependent on your requirements of course.

    Enjoy.
    @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.

  5. #5
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    996
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    I'd store the job type as a number, that way you can do better checks against it:

    E.g.

    1 = Temp
    2 = Permenent

    Now use your checkboxes like this:

    Code:
    <input name="type[]" id="permanent" value="1" />
    <input name="type[]" id="temp" value="2" />
    And in PHP bitwise OR the numbers:

    PHP Code:

    $type 
    0;
    foreach (
    $_POST['type'] as $currentType) {
        
    $type |= $currentType;

    Now your query is simplified to:

    PHP Code:
    $sql 'SELECT * FROM jobs WHERE contract_type & ' $type
    Your data will look like

    Job1 1
    Job2 2

  6. #6
    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)
    Hold on, whats this then?
    PHP Code:
     |= 
    @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.

  7. #7

  8. #8
    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)
    Quote Originally Posted by TomB View Post
    Bitwise or

    http://us3.php.net/manual/en/languag...rs.bitwise.php

    |= works the same as += or .= but with |
    Thanks!
    @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.

  9. #9
    SitePoint Guru
    Join Date
    Jul 2005
    Location
    Orlando
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can also store the values in a comma delimited string and take advantage of MySQL's FIND_IN_SET function to find just the value you're looking for.

    The bit stuff works great and I use it often, but it does have a limit to the number of options you can have depending on the maximum integer size in both MySQL and PHP.. it's usually 32 options.

  10. #10
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    996
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    Yeah, but for 2 checkboxes the 32 item limitation isn't going to be an issue

    Even if it were, it's easy enough to add more possible options by using multiple columns.

  11. #11
    One website at a time mmj's Avatar
    Join Date
    Feb 2001
    Location
    Melbourne Australia
    Posts
    6,282
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    In SilverBulletUK's example, the AND must be changed to OR, or selecting both won't return anything.

    This may be simpler:

    PHP Code:
    $options = array();
    if (!empty(
    $_POST['permanent']))
      
    $options[] = "contract_type='permanent'";
    if (!empty(
    $_POST['temp']))
      
    $options[] = "contract_type='temp'";

    if (empty(
    $options))
      
    trigger_error('Must specify at least one of \'permanent\', \'temp\''E_USER_ERROR);

    $sql "SELECT * FROM jobs WHERE " implode(' OR '$options); 
    [mmj] My magic jigsaw
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The Bit Depth Blog Twitter Contact me
    Neon Javascript Framework Jokes Android stuff

  12. #12
    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)
    Quote Originally Posted by mmj View Post
    In SilverBulletUK's example, the AND must be changed to OR, or selecting both won't return anything.
    Thanks mmj.
    @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.

  13. #13
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    \n is the newline character used throughout all software.More precisely, it should be put \r\n depending on the platform, it could be \r or \n which is the character for a new line.

  14. #14
    SitePoint Wizard
    Join Date
    Mar 2008
    Location
    United Kingdom
    Posts
    1,285
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many thanks for all the replies.
    I managed to do it myself with a fairly brutal bit of ugly code. Will post soon

    Thanks again.


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
  •