Hello,
I make my school project - eshop and I need to select data from database with filter. I wrote a code and tried it with XAMPP. Everything was ok so I uploaded it but the same code isn’t working. What is wrong?
Can you help me?
This is my code:
<?php
$connect = new PDO("mysql:host=localhost;dbname=konzole_db", "root", "");
?>
....
<?php
//fetch_data.php
include('database_connection.php');
if(isset($_POST["action"]))
{
$query = "SELECT * FROM konzole WHERE product_status='0'";
if(isset($_POST["minimum_price"], $_POST["maximum_price"]) && !empty($_POST["minimum_price"]) && !empty($_POST["maximum_price"]))
{
$query .= "
AND Cena BETWEEN '".$_POST["minimum_price"]."' AND '".$_POST["maximum_price"]."'
";
}
if(isset($_POST["typ"]))
{
$typ_filter = implode("','", $_POST["typ"]);
$query .= "
AND Typ IN('".$typ_filter."')
";
}
if(isset($_POST["platforma"]))
{
$platforma_filter = implode("','", $_POST["platforma"]);
$query .= "
AND Platforma IN('".$platforma_filter."')
";
}
if(isset($_POST["uloziste"]))
{
$uloziste_filter = implode("','", $_POST["uloziste"]);
$query .= "
AND Uloziste IN('".$ulotiste_filter."')
";
}
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$total_row = $statement->rowCount();
$output = '';
if($total_row > 0)
{
foreach($result as $row)
{
$output .= '
<section>
<div class="ex1" >
<div class="col-sm-4 col-lg-3 col-md-3">
<img src="images/'.$row["Obrazek"].'" /><br />
<h4 class="typ">'.$row["Typ"].'</h4>
<h4 class="v_uloziste">'.$row["Velikost_uloziste"].'</h4>
<h4 class="cena">'.$row["Cena"] .' Kč </h4>
<input type="text" name="mnozstvi" id="mnozstvi' . $row["id"] .'" value="1" />
<input type="hidden" name="hidden_typ" id="name'.$row["id"].'" value="'.$row["Typ"].'" />
<input type="hidden" name="hidden_uloziste" id="space'.$row["id"].'" value="'.$row["Velikost_uloziste"].'" />
<input type="hidden" name="hidden_cena" id="price'.$row["id"].'" value="'.$row["Cena"].'" />
<input type="button" name="pridat_do_kosiku" id="'.$row["id"].'" style="margin-top:5px;" value="Přidat do košíku" />
</div>
</div>
</section>
';
}
}
else
{
$output = '<h1 class="data" >Data nenalezena :( </h1>';
}
echo $output;
}
?>
....
<?php
// katalog.php
include('database_connection.php');
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<script src="js/jquery-1.10.2.min.js"></script>
<script src="js/jquery-ui.js"></script>
<script src="js/bootstrap.min.js"></script>
<link href = "css/jquery-ui.css" rel = "stylesheet">
<link href="css/style.css" rel="stylesheet">
<title>PRO konzole</title>
<link rel="stylesheet" type="text/css" href="css/styl.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<!-- Page Content -->
<div class="container">
<div class="row">
<nav>
<dl>
<img src="images/logo1.png" title="logo" alt="logo" />
<form class="example" action="/action_page.php" style="margin:auto;max-width:300px; float: right">
<input type="text" placeholder="Hledat..." name="hledat">
<button type="submit"><i class="fa fa-search"></i></button>
</form>
<dt><a href="index.php">Domů</a></dt>
<dt><a href="katalog.php">Katalog</a></dt>
<dt2><a href="kosik.php">Košík</a></dt2>
<dt2><a href="prihlasit.php">Přihlásit se</a></dt2>
</dl>
</nav>
<div >
<nav2 class="katalog">
<div class="col-md-3">
<div class="list-group">
<h3>Cena</h3>
<input type="hidden" id="hidden_minimum_price" value="0" />
<input type="hidden" id="hidden_maximum_price" value="35000" />
<p style="margin-left:8px" id="price_show">1000 - 35000</p>
<div class="slider-wrapper" id="price_range"></div>
</div>
<div class="">
<h3>Typ</h3>
<div style="height: 180px; overflow-y: auto; overflow-x: hidden;">
<section>
<?php
$query = "SELECT DISTINCT (typ) FROM konzole ORDER BY id DESC ";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
foreach($result as $row)
{
?></section>
<div class="checkbox">
<label><input type="checkbox" class="common_selector typ" value="<?php echo $row['typ']; ?>" > <?php echo $row['typ']; ?></label>
</div>
<?php
}
?>
</div>
</div>
<div class="list-group">
<h3>Platforma</h3>
<?php
$query = "
SELECT DISTINCT(platforma) FROM konzole
";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
foreach($result as $row)
{
?>
<div class="list-group-item checkbox">
<label><input type="checkbox" class="common_selector platforma" value="<?php echo $row['platforma']; ?>" > <?php echo $row['platforma']; ?></label>
</div>
<?php
}
?>
</div>
<div class="list-group">
<h3>Uložiště</h3>
<?php
$query = "
SELECT DISTINCT(uloziste) FROM konzole ORDER BY uloziste DESC
";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
foreach($result as $row)
{
?>
<div class="list-group-item checkbox">
<label><input type="checkbox" class="common_selector uloziste" value="<?php echo $row['uloziste']; ?>" > <?php echo $row['uloziste']; ?></label>
</div>
<?php
}
?>
</div>
</div>
</nav2>
<div class="col-md-9">
<br />
<div class="row filter_data">
</div>
</div>
</div>
</div>
<script>
$(document).ready(function(){
filter_data();
function filter_data()
{
$('.filter_data').html('<div id="loading" style="" ></div>');
var action = 'fetch_data';
var minimum_price = $('#hidden_minimum_price').val();
var maximum_price = $('#hidden_maximum_price').val();
var typ = get_filter('typ');
var platforma = get_filter('platforma');
var uloziste = get_filter('uloziste');
$.ajax({
url:"fetch_data.php",
method:"POST",
data:{action:action, minimum_price:minimum_price, maximum_price:maximum_price, typ:typ, platforma:platforma, uloziste:uloziste},
success:function(data){
$('.filter_data').html(data);
}
});
}
function get_filter(class_name)
{
var filter = [];
$('.'+class_name+':checked').each(function(){
filter.push($(this).val());
});
return filter;
}
$('.common_selector').click(function(){
filter_data();
});
$('#price_range').slider({
range:true,
min:1000,
max:35000,
values:[1000, 35000],
step:500,
stop:function(event, ui)
{
$('#price_show').html(ui.values[0] + ' - ' + ui.values[1]);
$('#hidden_minimum_price').val(ui.values[0]);
$('#hidden_maximum_price').val(ui.values[1]);
filter_data();
}
});
});
</script>
</body>
</html>