I am trying to create a search function in PHP. I am using following functions:
In show.php:
<form class="form-inline" action="?controller=address&action=searchAll" method="post">
<div class="form-group row">
<div class="col-xs-4">
<input class="form-control" id="terms" name="terms" type="text" placeholder="Search terms">
</div>
</div>
<div class="form-group row">
<div class="col-xs-4">
<button type="submit" class="btn btn-primary" ><span class="glyphicon glyphicon-search"> </span> Search</button>
</div>
</div>
</form>
This form sends a POST call to an controller: addressController.php
function searchAll(){
if (!empty($_POST['terms'])) {
$terms=$_POST['terms'];
$address=Address::searchByTerms($terms);
$listaAddresses[]=$address;
//var_dump($id);
//die();
require_once('Views/Address/show.php');
} else {
$listaAddresses=Address::all();
require_once('Views/Address/show.php');
}
}
And the controller should call the action searchByTerms to the model file Address.php and then open show.php again showing only the filtered items :
public static function searchByTerms($terms){
$db=Db::getConnect();
$select=$db->prepare('SELECT * FROM tb_direcciones WHERE fn LIKE "%:fn%"');
$select->bindValue('fn',$terms);
$select->execute();
$addressDb=$select->fetch();
$address = new Address ($addressDb['id_address'],$addressDb['cia'], $addressDb['fn'], $addressDb['ln'], $addressDb['type'], $addressDb['phone'], $addressDb['fromto'], $addressDb['direccion'], $addressDb['latitud'], $addressDb['longitud']);
//var_dump($address);
//die();
return $address;
}
But something is wrong in my code then the error function is thrown.