Searching a Database for and input aswell as array values in one search PHP, MYSQL, AJAX -


i having issue can not search exact data want. can search database fine , return values in format want. want add in ability search company's name , display results in same table. companys names not stored in databse in array session pulled api. store id each company in database set keys array.

i want know if there way results given form array database search. know can foreach loop around sql select statement, issue if there no match in array not run search, making rest of search limited searching ip address 1 of main reasons this. way think possible adding foreach loop sql statement don't see how possible nor can find documentation on online. please help!

here code runs based on input search box sent ajax.

// start sessions, connected db set customer array  session_start(); $conn = connect(); $customers = $_session['array'];  // if ajax posted search input if($_post){     $search = mysqli_real_escape_string($conn, $_post['search']);      //set second input checking against customers array , format give results     $input = $search     $input = strtolower($input);     $input = "'$input'";     $result = preg_grep($input, $customers);     $arrayk = array_keys($result);  //select search mysql query $matchingcompanies = "";   foreach($arrayk $id) { $matchingcompanies .= "'" . $id . "' or "; } $matchingcompanies = substr($matchingcompanies,0,strlen($matchingcompanies)-4);  $query1 = "select * ipv4_linked `companyid` = (" . $matchingcompanies . ") or concat(ipv4address, ipv4subnet, hostname, owner, sitename) '%" . $search . "%'";       $view4 = mysqli_query($conn, $query1);     //table title row     echo "<div><table name ='ipv4' class='tableparent'><td class='tabled'>ip address</td><td class='table'>subnet</td><td class='table'>pc name</td><td class='table'>owner</td><td class='table'>customer</td><td class='table'>customer site</td></tr>";  //loops through search results , echo table     while ($row = mysqli_fetch_array($view4)){          $id = $row['companyid'];         $company = $customers[$id];          echo "<tr><td class='tabled'>". $row['ipv4address']."</td><td class='table'>". $row['ipv4subnet']."</td><td class='table'>".$row['hostname']."</td><td class='table'>". $row['owner']."</td><td class='table'>".$company."</td><td class='table'>".$row['sitename']."</td></tr>";     }         echo"</table></div>"; }  ?> 

solved issue adding in matchingcompanies field before , setting exceeding long string lots of or's, limit search start after 3 characters inputed string return not large.

//select search mysql query $matchingcompanies = "";   foreach($arrayk $id) { $matchingcompanies .= "'" . $id . "' or "; } $matchingcompanies = substr($matchingcompanies,0,strlen($matchingcompanies)-4);  $query1 = "select * ipv4_linked `companyid` = (" . $matchingcompanies . ") or concat(ipv4address, ipv4subnet, hostname, owner, sitename) '%" . $search . "%'";       $view4 = mysqli_query($conn, $query1); 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -