SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    May 2005
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Radio buttons and dynamic querys

    Hello

    I have a search page with two text field and one radio button group (three buttons). You must fill in at least one value - none of the radio buttons is checked as default.

    The form fields are named: productname, productgroup (radio btn) and keyword.

    On the result page I have this code calling a method in class Inc_images:

    PHP Code:
    $productname $_POST['productname'];
            if (!isset(
    $_POST['productgroup'])) {
                
    $productgroup "";
            } else {
            
    $productgroup $_POST['productgroup'];
            }
            
    $keyword $_POST['keyword'];
            
            
    $result $image->findImage($productname$productgroup$keyword);
            if (
    count($result) <= 0) {
                echo 
    'Finns inga bilder som matchar din sökning';
            } else {
                foreach (
    $result as $item) {
                    echo 
    $item->pNamn;
                    echo 
    '<br>';
                    echo 
    $item->pGrupp;
                    echo 
    '<br>';
                    echo 
    $item->keywords;
                    echo 
    '<br>';
                    echo 
    '<br>';
                }
            } 
    My class method looks like this:
    PHP Code:
    public function findImage($productname$productgroup$keyword)
        {
            try {
                
    $productname "%$productname%";
                
    $productgroup $productgroup;
                
    $keyword "%$keyword%";
                
    $sqlwhere "1 = 1 ";
                if (!empty(
    $productname)) {
                    
    $sqlwhere .= " AND pNamn LIKE :productname";
                }
                
                if (isset(
    $productgroup) || (!empty($productgroup))) {
                    
    $sqlwhere .= " AND pGrupp = :productgroup";
                }
                
                if (!empty(
    $keyword)) {
                    
    $sqlwhere .= " AND keywords LIKE :keyword";
                }
                
                
    $sql "SELECT * FROM images WHERE " $sqlwhere;
                
    $stmt $this->pdoLink->pdoConnectLink->prepare($sql);
                
    $stmt->bindParam(':productname'$productnamePDO::PARAM_STR);
                
    $stmt->bindParam(':productgroup'$productgroupPDO::PARAM_STR);
                
    $stmt->bindParam(':keyword'$keywordPDO::PARAM_STR);
                
    $stmt->execute();
                
    $result = array();
                while (
    $row $stmt->fetchObject()) {
                
    $result[] = $row;
            }
            return 
    $result;
        }
        catch (
    PDOException $e)
        {
            echo 
    'Ett fel inträffade: ' $e->getMessage();
        }
        } 
    For some reason the radio buttons are not ignored if none of them are selected. To have the query to return any rows I must select one of the radio buttons – which I don't want to be necessary.

    Any tips on how to fix this?

    Regards, Magnus

  2. #2
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is your problem:

    Quote Originally Posted by brake6 View Post
    PHP Code:
    if (isset($productgroup) || (!empty($productgroup))) { 
    Earlier you set $productgroup to empty string if it's not set so at this stage it will always be set and the condition will always be met.

    As a sidenote: you're checking if $productgroup is set or not empty. That doesn't make sense. If it's not empty then it also is set so you should ask if it's set and not empty.

    However, in this case $productgroup is always set at this stage so just check if it's not empty:

    PHP Code:
    if ($productgroup != '') {
        
    $sqlwhere .= " And Pgrupp = :productgroup";

    I'm not using empty() here because it returns true for a string '0' rendering itself useless in most cases.

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2005
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks decowski,

    I changed according to your suggestion, but I get this error (which has haunted me a lot):
    Code:
    SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of token
    For some reason bindParam get a empty(zero, null, ?) value if productgroup is not set – and she does't like it

    Can you have a if-conditional around the second $stmt->bindParam... ?

    A quick way to solve this is to have one of the radio buttons checked as default.

    Regards, Magnus

  4. #4
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Brake6 View Post
    Can you have a if-conditional around the second $stmt->bindParam...
    Yes, just use the same if expression.

  5. #5
    SitePoint Enthusiast
    Join Date
    May 2005
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many thanks

    Worked perfect.

    Regards, Magnus


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
  •