Witam, mam switcha z kategoriami porduktów, ale chcę go przekształcić na ifa, aby móc dodawać kategorie z poziomu interfejsu, bez grzebania w kodzie.
mam takiego switcha
switch($_POST['category'])
{
case'coins':
$_SESSION['category'] = $_POST['category'];
break;
case'testy':
$_SESSION['category'] = $_POST['category'];
break;
case'wszystko':
$_SESSION['category'] = null;
break;
default:
break;
}
i próbując go przekształcić na if'a wyszło mi coś takiego, ale nie działa.
if(isset($_POST['category']));
{
$_SESSION['category'] = $_POST['category'];
}
if($_SESSION['category'] == "wszystko")
{
$_SESSION['category'] = null;
}
A tu mamy cały kod tej funkcji:
function search_product()
{
$limit = 8;
$page = $_GET['p'];
if($page==0 || $page==1)
{
$strona= 0;
}else{
$strona = ($page*$limit)-$limit;
}
switch($_POST['category'])
{
case'coins':
$_SESSION['category'] = $_POST['category'];
break;
case'testy':
$_SESSION['category'] = $_POST['category'];
break;
case'wszystko':
$_SESSION['category'] = null;
break;
default:
break;
}
switch($_POST['filter'])
{
case'najmniejsza':
if(isset($_SESSION['category']))
{
$phpro_category = filter_var($_SESSION['category'], FILTER_SANITIZE_STRING);
$phpro_search_name = filter_var($_SESSION['product_search'], FILTER_SANITIZE_STRING);
$stmt = getDBH()->prepare("SELECT * FROM products WHERE name LIKE :search_name AND category=:category ORDER BY price ASC LIMIT $strona,8");
$stmt->bindValue(':search_name','%'.$phpro_search_name.'%');
$stmt->bindParam(':category', $phpro_category, PDO::PARAM_STR);
$stmt->execute();
}else{
$phpro_search_name = filter_var($_SESSION['product_search'], FILTER_SANITIZE_STRING);
$stmt = getDBH()->prepare("SELECT * FROM products WHERE name LIKE :search_name ORDER BY price LIMIT $strona,8");
$stmt->bindValue(':search_name','%'.$phpro_search_name.'%');
$stmt->execute();
}
break;
case'najwieksza':
if(isset($_SESSION['category']))
{
$phpro_category = filter_var($_SESSION['category'], FILTER_SANITIZE_STRING);
$phpro_search_name = filter_var($_SESSION['product_search'], FILTER_SANITIZE_STRING);
$stmt = getDBH()->prepare("SELECT * FROM products WHERE name LIKE :search_name AND category=:category ORDER BY price DESC LIMIT $strona,8");
$stmt->bindValue(':search_name','%'.$phpro_search_name.'%');
$stmt->bindParam(':category', $phpro_category, PDO::PARAM_STR);
$stmt->execute();
}else{
$phpro_search_name = filter_var($_SESSION['product_search'], FILTER_SANITIZE_STRING);
$stmt = getDBH()->prepare("SELECT * FROM products WHERE name LIKE :search_name ORDER BY price DESC LIMIT $strona,8");
$stmt->bindValue(':search_name','%'.$phpro_search_name.'%');
$stmt->execute();
}
break;
default:
if(isset($_SESSION['category']))
{
$phpro_category = filter_var($_SESSION['category'], FILTER_SANITIZE_STRING);
$phpro_search_name = filter_var($_SESSION['product_search'], FILTER_SANITIZE_STRING);
$stmt = getDBH()->prepare("SELECT * FROM products WHERE name LIKE :search_name AND category=:category ORDER BY price ASC LIMIT $strona,8");
$stmt->bindValue(':search_name','%'.$phpro_search_name.'%');
$stmt->bindParam(':category', $phpro_category, PDO::PARAM_STR);
$stmt->execute();
}else{
$phpro_search_name = filter_var($_SESSION['product_search'], FILTER_SANITIZE_STRING);
$stmt = getDBH()->prepare("SELECT * FROM products WHERE name LIKE :search_name ORDER BY price LIMIT $strona,8");
$stmt->bindValue(':search_name','%'.$phpro_search_name.'%');
$stmt->execute();
}
break;
}
echo '<div class="text5">Wyniki wyszukiwania dla: '.$_SESSION['product_search'].'</div>';
while ($r = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo'
<div class="col-sm-6 col-md-3">
<input type="hidden" name="id" value="'.$r['id'].'">
<input type="hidden" name="price" value="'.$r['price'].'">
<input type="hidden" name="count" value="'.$r['name'].'">
<div class="thumbnail">
<img src="../graph/'.$r['picture'].'" alt="" class="img-responsive">
<div class="caption">
<h3 class="text-left">'.$r['name'].'</h3>
<p class="text-left">Cena: '.$r['price'].'zł</p>
<div class="btn-toolbar text-center">
<button type="submit" name="buy_product" class="btn btn-primary pull-right">Szczegoły</button>
</div>
</div>
</div>
</div>';
}
echo '</div>';
}
function change_steamid()
{
$stmt = getDBH()->prepare("UPDATE users SET steamid = :steamid WHERE login = :id");
$stmt->bindParam(':steamid', $_SESSION['steamid'], PDO::PARAM_STR);
$stmt->bindParam(':id', $_SESSION['id'], PDO::PARAM_STR);
$stmt->execute();