1
I have two pages made in html a call index.html where the user type a product name (eg Lapis) and the result is displayed on a different page and as the result of the query can be very large depending on the query there should be a result pagination. The problem in question is that I cannot place the result of the query inside the exhibition page nor create a suitable pagination.
Code index.html responsible for inserting 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>
Code display.html where the paged results of the query 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 treating the form sent in index.html and sending the already validated terms to the . php responsible for 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 carrying out the query and return the result to Validadeform (which in theory should insert 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 was unable to inject the results into the.html view and inject the appropriate pagination, it is important not to insert php code into the.html view