HTML de nuestro buscador
A continuación puedes ver el código HTML de nuestro formulario avanzado con todos sus inputs.
<form name="frmSearch" method="post" action="index.php">
<input type="hidden" id="advance_search_submit" name="advance_search_submit" value="<?php echo $advance_search_submit; ?>">
<div class="search-box">
<label class="search-label">With Any One of the Words:</label>
<div>
<input type="text" name="search[with_any_one_of]" class="demoInputBox" value="<?php echo $with_any_one_of; ?>" />
<span id="advance_search_link" onClick="showHideAdvanceSearch()">Advance Search</span>
</div>
<div id="advanced-search-box" <?php if(empty($advance_search_submit)) { ?>style="display:none;"<?php } ?>>
<label class="search-label">With the Exact String:</label>
<div>
<input type="text" name="search[with_the_exact_of]" id="with_the_exact_of" class="demoInputBox" value="<?php echo $with_the_exact_of; ?>" />
</div>
<label class="search-label">Without:</label>
<div>
<input type="text" name="search[without]" id="without" class="demoInputBox" value="<?php echo $without; ?>" />
</div>
<label class="search-label">Starts With:</label>
<div>
<input type="text" name="search[starts_with]" id="starts_with" class="demoInputBox" value="<?php echo $starts_with; ?>" />
</div>
<label class="search-label">Search Keywords in:</label>
<div>
<select name="search[search_in]" id="search_in" class="demoInputBox">
<option value="">Select Column</option>
<option value="title" <?php if($search_in=="title") { echo "selected"; } ?>>Title</option>
<option value="description" <?php if($search_in=="description") { echo "selected"; } ?>>Description</option>
</select>
</div>
</div>
<div>
<input type="submit" name="go" class="btnSearch" value="Search">
</div>
</div>
</form>
PHP para gestionar las consultas
Este código recibe la información de los inputs de nuestro formulario y la transforma en una query que trasladaremos a nuestra base de datos. Si el usuario ha seleccionado una columna de la base de datos en concreto, aplicaremos la consulta a dicha columna. Si el usuario no ha seleccionado ninguna columna, buscaremos en todas las columnas. El código sería...
<php
$conn = mysqli_connect("localhost", "root", "", "pruebas");
$with_any_one_of = "";
$with_the_exact_of = "";
$without = "";
$starts_with = "";
$search_in = "";
$advance_search_submit = "";
$queryCondition = "";
if(!empty($_POST["search"])) {
$advance_search_submit = $_POST["advance_search_submit"];
foreach($_POST["search"] as $k=>$v){
if(!empty($v)) {
$queryCases = array("with_any_one_of","with_the_exact_of","without","starts_with");
if(in_array($k,$queryCases)) {
if(!empty($queryCondition)) {
$queryCondition .= " AND ";
} else {
$queryCondition .= " WHERE ";
}
}
switch($k) {
case "with_any_one_of":
$with_any_one_of = $v;
$wordsAry = explode(" ", $v);
$wordsCount = count($wordsAry);
for($i=0;$i<$wordsCount;$i++) {
if(!empty($_POST["search"]["search_in"])) {
$queryCondition .= $_POST["search"]["search_in"] . " LIKE '%" . $wordsAry[$i] . "%'";
} else {
$queryCondition .= "title LIKE '" . $wordsAry[$i] . "%' OR description LIKE '" . $wordsAry[$i] . "%'";
}
if($i!=$wordsCount-1) {
$queryCondition .= " OR ";
}
}
break;
case "with_the_exact_of":
$with_the_exact_of = $v;
if(!empty($_POST["search"]["search_in"])) {
$queryCondition .= $_POST["search"]["search_in"] . " LIKE '%" . $v . "%'";
} else {
$queryCondition .= "title LIKE '%" . $v . "%' OR description LIKE '%" . $v . "%'";
}
break;
case "without":
$without = $v;
if(!empty($_POST["search"]["search_in"])) {
$queryCondition .= $_POST["search"]["search_in"] . " NOT LIKE '%" . $v . "%'";
} else {
$queryCondition .= "title NOT LIKE '%" . $v . "%' AND description NOT LIKE '%" . $v . "%'";
}
break;
case "starts_with":
$starts_with = $v;
if(!empty($_POST["search"]["search_in"])) {
$queryCondition .= $_POST["search"]["search_in"] . " LIKE '" . $v . "%'";
} else {
$queryCondition .= "title LIKE '" . $v . "%' OR description LIKE '" . $v . "%'";
}
break;
case "search_in":
$search_in = $_POST["search"]["search_in"];
break;
}
}
}
}
$orderby = " ORDER BY id desc";
$sql = "SELECT * FROM links " . $queryCondition;
$result = mysqli_query($conn,$sql);
?>
0 Comentarios