I'm trying to make a system where I can input multiple values, and get all matches with a 15% tolerance from a mysql table. I've made in HTML and CSS the layout and I've managed to achieve it, but the problem is, I need to get the best match on top!
My table has 4 columns Modelo, L, A, H (Model, Length, width, height) Here's my code: HTML
<div class="battrac">
<label for="largo">L</label>
<input id="largo" name="largo" onkeyup="buscar()" required></input>
<label for="ancho">A</label>
<input id="ancho" name="ancho" onkeyup="buscar()" required></input>
<label for="alto">H</label>
<input id="alto" name="alto" onkeyup="buscar()" required></input>
</div>
JS
function buscar() {
// if (str=="") {
// document.getElementById("result-trac").innerHTML="";
// }
var l = document.getElementById("largo").value;
var a = document.getElementById("ancho").value;
var h = document.getElementById("alto").value;
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
if (this.readyState==4 && this.status==200) {
document.getElementById("result-trac").innerHTML=this.responseText;
}
}
xmlhttp.open("GET","busc.php?t="+tipo+"&l="+l+"&a="+a+"&h="+h,true);
xmlhttp.send();
}
busc.php
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$link = mysqli_connect("localhost", "root", "", "test");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$l = $_GET['l'];
$a = $_GET['a'];
$h = $_GET['h'];
if($l=="" ||$a=="" ||$h==""){
echo("Por favor compelte todos los campos");
exit;
}
$sql = "SELECT * FROM test WHERE L>=($l * .85) AND L<=($l * 1.15) AND A>=($a * .85) AND A<=($a * 1.15) AND H>=($h * .85) AND H<=($h * 1.15)";
$result = $link->query($sql);
if ($result->num_rows > 0) {
echo('<table style="width:100%">
<tr>
<th>Modelo</th>
<th>L</th>
<th>A</th>
<th>H</th>
</tr>');
while($row = $result->fetch_assoc()) {
echo("
<tr>
<td>".$row['Producto']."</td>
<td>".$row['L']."</td>
<td>".$row['A']."</td>
<td>".$row['H']."</td>
</tr>
");
}
echo('</table>');
}else{echo($link->connect_error);}
?>
In this picture, the nearest match is BAT 2, but BAT 1 is showing up first.
Some IDs and Classes are in Spanish, but if you need any other information, let me know! EDIT: What I need to do, is to get the nearest match on top. See in the picture BAT 2 has more near numbers than BAT 1 dora's, but BAT 1 is on top. I want BAT 2 to appear on top Here's a fiddle with 4 rows http://sqlfiddle.com/#!9/6e705f So if I need, for example, a product that has L=95, A=95 and H=95, I would like to have BAT 3 show up first, and then BAT 2 and BAT 1. But not BAT 4 because is not in the 15% tolerance in all values