2021年1月5日星期二

Search data from 2 tables in mysql using php [caveat: make sure to distinguish those tables]

I'm building an autocomplete search algorithm. The problem is that my table doesn't contain everything (as for now). So I created a new php table to kinda 'complete' the big one.

  • table 1: has 5 columns
  • table 2: has 2 columns

I have this working code that manages to do the work for one table (the big one):

if (isset($_POST['city'])) {            $output = "";          $city = $_POST['city'];          $query =  "SELECT * FROM `table_1` WHERE `location` LIKE '%".$city."%'";          $result = $conn->query($query);          $output = '<ul class="list-unstyled">';                 if ($result->num_rows > 0) {              while ($row = $result->fetch_array()) {                  if(in_array( ucwords($row['location']),$arr )){                  $output .= '';                  }else{                  array_push($arr, ucwords($row['location']));                  $item = preg_replace("#(".$city.")#i", "<b style='color:black'>$1</b>",  ucwords($row['location']));                  $output .= '<li style="padding-left:20px;font-size:130%;margin-bottom:8px;margin-top:8px;">'.$item.'</li>';                  }              }          }else{                $output .= '<li>Nothing Found.</li>';          }                    $output .= '</ul>';          echo $output;      }  

I tried to work with the UNION operator, but I don't think that is what I need. Because as I stated in the Question: I would like to distinguish those two tables with different 'layout': (Maybe, with a different color or something)

 if(FIRST_TABLE_QUERY_FOUND_VALUE)      $output .= '<li style="---style_1---">'.$item.'</li>';      else if(TRY_SECOND_QUERY_TABLE)      $output .= '<li style="---style_2---">'.$item.'</li>';      else      $output .= '<li style="---style_3---">'.$item.'</li>';  

So, in this way, if the first query doesn't find anything, It would still find it in the second table;
How would you accomplish this?

https://stackoverflow.com/questions/65589225/search-data-from-2-tables-in-mysql-using-php-caveat-make-sure-to-distinguish-t January 06, 2021 at 10:06AM

没有评论:

发表评论