Je cherche à filtrer des produits (par catégorie(s), marque(s), couleur(s), et autres critères... en ajax et à base de requêtes préparées.
Ex : SELECT * FROM products WHERE published = 'Yes' AND WHERE catégory IN = 'jupes, robes, pantalons' AND WHERE couleur IN = 'jaune, rose' AND marque IN "Chanel, Dior, Lidl"...........
Je sais faire ça avec mysqli_query, mais je n'ai pas trouvé d'exemple lors de requête préparée.
J'ai trouvé ces bouts de code, mais qui ne correspondent qu'à 1 seul WHERE IN, alors que j'ai besoin de chercher dans plusieurs WHERE IN.
$published='Y';
$sort_products = $con -> prepare ( "select * from products where published = ? ");
if ( isset ($_POST['product_category']))
{
$array= $_POST['product_category'];
$product_category = implode(',', array_fill(0, count($array), '?')); // creates a string containing ?,?,?
$array[] = $this->getTime();
$types = str_repeat('i', count($array));
$params_category = array($types);
foreach ($array as $key => $value)
{ $params_category[] = &$array[$key];
}
$sort_products .= "AND p_cat_id IN (". $product_category." ) ";
call_user_func_array(array($sort_products, 'bind_param'), $params_category);
}
if ( isset ($_POST['product_brand']))
{ $array= $_POST['product_brand'];
$product_brand = implode(',', array_fill(0, count($array), '?')); // creates a string containing ?,?,?
$array[] = $this->getTime();
$types = str_repeat('i', count($array));
$params_brand = array($types);
foreach ($array as $key => $value)
{ $params_brand[] = &$array[$key];
}
$sort_products .= "AND product_brand IN (". $product_brand." ) ";
call_user_func_array(array($sort_products, 'bind_param'), $params_brand);
}
// + autres if isset
if ( !$sort_products->execute()) { trigger_error('Invalid query : ' . $con->error); }
$res = $sort_products->get_result();
$count = $res -> num_rows;
$output="";
Et echo $sort_products me retourne une erreur d'objet. echo $sort_products->fullQuery; pareil
print_r($sort_products->__toString()); pareil
Comment faire un echo d'une requête préparée ? Merci