Skip to content

Instantly share code, notes, and snippets.

@B-uny
Last active February 11, 2020 15:08
Show Gist options
  • Save B-uny/eb70a0a1c705a0eb32b32abcb134da71 to your computer and use it in GitHub Desktop.
Save B-uny/eb70a0a1c705a0eb32b32abcb134da71 to your computer and use it in GitHub Desktop.
Useful for querying & filtering multiple tables in php. Note that you can run multi-query instead of individual calls (I prefer to separate my calls line by line).
<?php
//on ajax POST of user defined var
if(isset($_POST['msg'])){
//include the SQL configuration and request
include('configFiles.php');
//var sent with POST to be queried
$keywords = $_POST['keywords'];
//send queries
$query = "SELECT dataList as Column1, ID as Column2 FROM myTable WHERE dataList LIKE '%".$keywords."%' UNION SELECT dataList, as Column1, ID as Column2 FROM myTableTwo WHERE dataList LIKE '%".$keywords."%' ORDER BY column2 ASC";
//do not close if statement yet
?>
<!--Begin HTML formatting-->
<br>
<table id="myTable">
<th>ID</th><th>Data</th>
<?php
//check for matches
if($result = $dbConfig->query($query)){
//get matches
while($row = $query->fetch_assoc()){
//get the datalist and explode it on your selected delimiter
$myData = "$row[Column1]";
$array = explode(',', $numbers);
//filter that checks for value in exploded array items
foreach ($array as $key => $value) {
if (stripos($array[$key], $keywords) != false){
echo "<tr><td>".$row['Column2']."</td><td>".$array[$key]."</td></tr>";
}
};
};
};
} //close statement
?>
</table>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment