Combination of field search using PHP & MYSQL

后端 未结 3 841
长情又很酷
长情又很酷 2020-12-22 06:39

I am working on an assignment using PHP & MYSQL.

one of the tasks is to search on any combination of the fields. That includes Dropdown boxes populated from the

相关标签:
3条回答
  • 2020-12-22 06:50

    I've done something similar in the past where I checked the value from different fields and then added them to a series of arrays. I created an array for select, from, where, order. You can do similar for other sets like group or limit. Then I ran 'array_unique', imploded them and put them into the SQL string.

    $array_select = array('users.Id'); // SET SOME DEFAULTS SO THE QUERY WILL ALWAYS RUN
    $array_from = array('users');
    $array_where = array();
    $array_order = array();
    
    if (isset($first_name)) {
        $array_select[] = 'First_Name';
        $array_from[] = 'users';
    }
    
    if (isset($city)) {
        $array_select[] = 'City';
        $array_from[] = 'user_contact';
        $array_where[] = 'users.Id = user_contact.City';
    }
    
    if ($array_select) {
        $array_select = array_unique($array_select);
        $string_select = implode(', ', $array_select);
    }
    if ($array_where) {
        $array_where = array_unique($array_where);
        $string_where = 'WHERE '.implode(' AND ', $array_where);
    }
    // REPEAT FOR OTHERS ...
    
    
    
    // BUILD THE QUERY OUT
    $sql = 'SELECT '.$string_select.' FROM '.$string_from.' '.$string_where.' ...
    
    0 讨论(0)
  • 2020-12-22 06:55

    Why not evaluate your string with each column (this is a guide only, I'm not building your PHP code there:

    SELECT 
      * 
    FROM 
      table
    WHERE 
      (ID = $id OR $id = 'showAll')
      AND (SPORT = $sport OR $sport = 'showAll')
      AND (COUNTRY = $country OR $country = 'showAll')
      AND (GENDER = $gender OR $gender = 'showAll')
      AND (FIRSTNAME = $firstname OR $firstname = 'showAll')
    

    Just need to make sure you NVL the variables to an appropriate value (whether it be int or string)

    0 讨论(0)
  • 2020-12-22 06:57

    Instead of all those conditionals about whether to add AND when concatenating to the query, use an array and implode.

    $fields = array('sport' => 'sport',
                    'gender' => 'gender', 
                    'fname' => 'firstName',
                    'lname' => 'lastName',
                    'country' => 'country');
    $wheres = array();
    foreach ($fields as $postfield => $dbfield) {
        if ($_POST[$postfield] != 'showAll') {
            $wheres[] = "$dbfield = '" . mysql_real_escape_string($_POST[$postfield]) . "'";
        }
    }
    $selectString = "SELECT t2ath.lastName, t2ath.firstName, t2ath.image, t2ath.sport, t2ath.gender, t2ath.country, t2country.flag 
                     FROM t2ath LEFT JOIN t2country
                     ON t2ath.country = t2country.name";
    if (count($wheres) > 0) {
        $selectString .= " WHERE " . implode(" AND ", $wheres);
    }
    $result = mysql_query($selectString);
    

    To see how to do it similarly using PDO prepared statements, see my answer here: What code approach would let users apply three optional variables in PHP/MySQL?

    0 讨论(0)
提交回复
热议问题