#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    252
    Rep Power
    73

    Advanced search arrays


    The code listed below gives the desired result however I don’t think that it is the best method to do so. I have an advanced search page where the user can input several values at the same time, as most advanced searches would do. The first part of the code where the value is stripped down to the first letter and then given either a matching or non matching search value is where I have the most issue with. I ultimately will have 11 fields to search on and then every letter of the alphabet which at that point is very large. I really want to know if over 2000 lines of code is acceptable and if my logic is okay here.
    I have tried an array but where I get stumped is assigning a searchable sql value if there was a matching value for the array. I included the id value in case there is no matches the entire database would not be displayed.
    Pseudo code:
    $chad = ‘chad’
    firstName => $chad
    $arrValueC = firstName LIKE ‘c%’

    PHP Code:
    $firstNameC strtolower($firstName[0]);
    if (
    $firstNameC == 'c') {
    $fistNameClist '%C%';  
    } else {
    $id 'nothing';
    }
    $lastNameC strtolower($lastName2[0]);
    if (
    $lastNameC == 'c') {
    $lastNameClist '%C%';  
    } else {
    $id 'nothing';
    }
    $arrValueC = array('id' => $id'firstName' => $firstNameClist'lastName' => $lastNameClist);
    $countC 0;
    foreach (
    $arrValueC as $fieldC => $valueC){
        if (!empty(
    $valueC)) {
            if (
    $countC 0) {
                
    $arr_comboC .= ' OR ';
            }
            
    $arr_comboC .= "$fieldC LIKE '$valueC'";
            
    $countC++;
        }
    }    
        
    $result mysql_query($db_select.$arr_comboC
        or die(
    mysql_error()); 
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,113
    Rep Power
    9398
    2000 lines is too much for this. While each field probably needs to be handled specially (not a whole lot you can do about that) you don't need to write nearly identical code for all the individual letters.
    PHP Code:
    if ($firstName != "" && ctype_alpha($firstName[0])) {
        
    $firstNameClist "{$firstName[0]}%";
    } else {
        
    // don't search on the first name

  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    252
    Rep Power
    73
    Originally Posted by requinix
    2000 lines is too much for this. While each field probably needs to be handled specially (not a whole lot you can do about that) you don't need to write nearly identical code for all the individual letters.
    PHP Code:
    if ($firstName != "" && ctype_alpha($firstName[0])) {
        
    $firstNameClist "{$firstName[0]}%";
    } else {
        
    // don't search on the first name

    Okay that makes sense, thank you.
    So is filtering the information through "if" statements before it hits the array the best option?
  6. #4
  7. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,113
    Rep Power
    9398
    Best I know, yes.

    I tend to do this SQL construction using an array of arbitrary conditions.
    PHP Code:
    $match = array();

    // ...

    if ($firstName != "" && ctype_alpha($firstName[0])) {
        
    $match[] = "firstName LIKE '{$firstName[0]}%'";
    }

    // ...

    $query "SELECT fields FROM table";
    if (
    $match) {
        
    $query .= " WHERE " implode(" OR "$match);

  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    252
    Rep Power
    73
    Originally Posted by requinix
    Best I know, yes.

    I tend to do this SQL construction using an array of arbitrary conditions.
    PHP Code:
    $match = array();

    // ...

    if ($firstName != "" && ctype_alpha($firstName[0])) {
        
    $match[] = "firstName LIKE '{$firstName[0]}%'";
    }

    // ...

    $query "SELECT fields FROM table";
    if (
    $match) {
        
    $query .= " WHERE " implode(" OR "$match);

    Okay, great! Yea, that is perfect. Thank you.

IMN logo majestic logo threadwatch logo seochat tools logo