I have two pages made in html an index.html call where the user types a product name (eg Lapis) and the result is displayed on a different page and the result of the query can be very large depending on the query there should be a pagination of results. The problem in question is that I can not put query result within the view page nor create a suitable paging.
Code index.html responsible for the insertion of the term to be consulted:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Index</title>
</head>
<body>
<form action="ValidateForm.php" method="get">
<label class="radio-inline"><input type="radio" name="result_type" value="produtos" checked=""/>Produtos</label>
<input class="form-control" type="text" id="search_input" name="search_input" placeholder="Search"/>
<button type="submit" name="search_form_submit" value="Search">Search</button>
</form>
</body>
Display.html code where paged query results should be displayed:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Exibe produtos</title>
</head>
<body>
<div style="background-color:red">
<div id="conteudo" style="background-color:yellow;width:400px">
</div>
</div>
<footer id="paginacao">
</footer>
</body>
Code ValidateForm.php responsible for handling the form sent in index.html and send the terms already validated to the .php answerable by the query.
<?php
include_once "QueryInDB.php";
class ValidadeForm{
static function validate(){
if(isset($_GET['search_form_submit'])){
ValidadeForm::validateSearch();
}
}
static function validateSearch(){
$erro = false;
if(!isset($_GET) || empty($_GET)){
$erro = 'Nada foi postado.';
}
$values = array();
foreach($_GET as $chave => $valor){
// Remove todas as tags HTML
$values[$chave] = strip_tags($valor);
// Verifica se tem algum valor nulo
if(empty($valor)){
$erro = 'Existem campos em branco.';
}
}
if(!isset($values['search_input']) && !$erro){
$erro = 'Consulta Nula';
}
if($erro){
echo $erro;
}else{
array_pop($values);//Tira o nome do Button submit
print_r(queryInDB::search($values));
}
}
}
ValidadeForm::validate();
?>
Code QueryInDB.php code responsible for conducting the query and returning the result to ValidityForm (which in theory should do the insertion of the results inside the display.html)
<?php
class queryInDB{
static function search($values){
$conn = queryInDB::openConnection();
$stmt = queryInDB::queryFactory($conn,$values);
$result = queryInDB::queryExecute($stmt);
return $result;
}
static function openConnection(){
try{
$conn = new PDO("mysql:host=localhost;port=3306;dbname=loja", 'teste', 'teste');
return $conn;
}catch(PDOException $e){
print 'Error: ' . $e->getMessage() . '<br />';
}
}
static function queryFactory($conn,$values){
$colunm = $values['result_type'];
array_shift($values);//Tira o Seletor Radio Button ()
$values = (string)$values['search_input'];//Pega o input do usuario e transforma em string
$values = explode(' ',$values);
$keywords = array();
foreach($values as $value){
$value = mysql_real_escape_string($value);
$value = '%' . $value . '%';
$keywords[] = $value;
}
$keys = 'nome LIKE ' . str_repeat('? OR nome LIKE ', count($keywords) - 1) . '?';
$stmt['stmt'] = $conn->prepare('SELECT id, nome, descricao, valor FROM ' . $colunm . ' WHERE ' . $keys);
$stmt['params'] = $keywords;
return $stmt;
}
static function queryExecute($stmt){
$stmt['stmt']->execute($stmt['params']);
$result = $stmt['stmt']->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
}
?>
I could not do the injection of the results inside the display.html and nor inject the proper paging, it is important not to insert php code inside the display.html