category and Brand both seach at a time in php
i am creating a e-commerce website. Example if i click category as Tv and brand as LG the relevant product should display.but it is not working well.no error diplayed
what I tried so far i have written below.
Form Design
Category
<div align="left">
<li class="list-group-item list-group-item-action active" >
<h4>Category</h4></li>
<li class="list-group-item list-group-item-action" id="categories"></li>
</div>
Brand
<div align="left" >
<li class="list-group-item list-group-item-action active" ><h4>Brand</h4></li>
<li class="list-group-item list-group-item-action" id="brands"></li>
</div>
Products
<div class="panel-heading">
Products
</div>
<div class="panel-body" id="Products" >
</div>
jquery
Category
function getCategory(){
$.ajax({
type: 'GET',
url: 'get_category.php' ,
dataType: 'JSON',
success: function (data)
{
for (var i = 0; i < data.length; i++)
{
var catname = data[i].catname;
var id = data[i].id;
$('#categories').append('<a href="#" cid= '+ id + ' class="list-group-item list-group-item-action">' + '<b>'+ data[i].catname + '<b>' + '</a>');
}
},
error: function (xhr, status, error)
{
console.log(xhr.message)
}
});
}
Brand
function getBrand(){
$.ajax({
type: 'GET',
url: 'all_brand.php' ,
dataType: 'JSON',
success: function (data) {
console.log(data);
for (var i = 0; i < data.length; i++)
{
var id1 = data[i].id;
$('#brands').append('<a href="#" bid= '+ id1 + ' class="list-group-item list-group-item-action">' + '<b>'+ data[i].brand_name + '<b>' + '</li>');
}
},
error: function (xhr, status, error) {
alert(xhr.responseText);
}
});
}
Categroy and brand diplayed succesfully.
Product
if i click category and brand at the time the relevant product will display. this how i wrote query
$(document).ready(function() {
$("#categories a.list-group-item").click(function () {
var cat = $(this).attr('cid');
var brand = $("#brands a.list-group-item").click();
var brand = $(this).attr('bid');
$.ajax({
url: 'get_search.php',
type: 'post',
data: {cat: cat, brand: brand},
// data: {cat: cat},
dataType: 'json',
success: function (data) {
var len = data.length;
console.log(data);
$("#Products").empty();
for (var i = 0; i < data.length; i++) {
var price = data[i].price;
var image = data[i].image;
var description = data[i].description;
$("#Products").append("<div class='col-md-4'> " +
"<div class='panel panel-info' id='Products'>" +
"<div class='card-body'>" +
"<div class='panel-heading'>" + "<h4> " + description + "</h4> " +
"<p class='panel-body'>" + "<h3> " + price + "</h3>" +
"<p class='panel-body'> <img class='card-img-top' style='width:250px' height='250px' id='theImg' src='images/" + image + "' /> </p>" +
" <a href='#' class='btn btn-primary'>View More</a> </div> </div></div> </div>");
}
}
});
});
});
get_search.php
<?php
include("db.php");
$stmt = $conn->prepare("SELECT id,cat_id,brand_id,price,description,image,keywords from products WHERE cat_id = ? AND brand_id = ? ");
$stmt->bind_result($id,$cat_id,$brand_id,$price,$description,$image,$keywords);
$cat_id = $_POST["cat"];
$brand = $_POST['brand'];
$stmt->bind_param("ss", $cat_id,$brand);
$stmt->bind_result($id,$cat_id,$brand_id,$price,$description,$image,$keywords);
if ($stmt->execute()) {
while ( $stmt->fetch() ) {
$output = array ("id"=>$id, "cat_id"=>$cat_id,"brand_id"=>$brand_id,"price"=>$price,"description"=>$description,"image"=>$image,"keywords"=>$keywords);
}
echo json_encode( $output );
}
$stmt->close();
?>
php jquery ajax
|
show 9 more comments
i am creating a e-commerce website. Example if i click category as Tv and brand as LG the relevant product should display.but it is not working well.no error diplayed
what I tried so far i have written below.
Form Design
Category
<div align="left">
<li class="list-group-item list-group-item-action active" >
<h4>Category</h4></li>
<li class="list-group-item list-group-item-action" id="categories"></li>
</div>
Brand
<div align="left" >
<li class="list-group-item list-group-item-action active" ><h4>Brand</h4></li>
<li class="list-group-item list-group-item-action" id="brands"></li>
</div>
Products
<div class="panel-heading">
Products
</div>
<div class="panel-body" id="Products" >
</div>
jquery
Category
function getCategory(){
$.ajax({
type: 'GET',
url: 'get_category.php' ,
dataType: 'JSON',
success: function (data)
{
for (var i = 0; i < data.length; i++)
{
var catname = data[i].catname;
var id = data[i].id;
$('#categories').append('<a href="#" cid= '+ id + ' class="list-group-item list-group-item-action">' + '<b>'+ data[i].catname + '<b>' + '</a>');
}
},
error: function (xhr, status, error)
{
console.log(xhr.message)
}
});
}
Brand
function getBrand(){
$.ajax({
type: 'GET',
url: 'all_brand.php' ,
dataType: 'JSON',
success: function (data) {
console.log(data);
for (var i = 0; i < data.length; i++)
{
var id1 = data[i].id;
$('#brands').append('<a href="#" bid= '+ id1 + ' class="list-group-item list-group-item-action">' + '<b>'+ data[i].brand_name + '<b>' + '</li>');
}
},
error: function (xhr, status, error) {
alert(xhr.responseText);
}
});
}
Categroy and brand diplayed succesfully.
Product
if i click category and brand at the time the relevant product will display. this how i wrote query
$(document).ready(function() {
$("#categories a.list-group-item").click(function () {
var cat = $(this).attr('cid');
var brand = $("#brands a.list-group-item").click();
var brand = $(this).attr('bid');
$.ajax({
url: 'get_search.php',
type: 'post',
data: {cat: cat, brand: brand},
// data: {cat: cat},
dataType: 'json',
success: function (data) {
var len = data.length;
console.log(data);
$("#Products").empty();
for (var i = 0; i < data.length; i++) {
var price = data[i].price;
var image = data[i].image;
var description = data[i].description;
$("#Products").append("<div class='col-md-4'> " +
"<div class='panel panel-info' id='Products'>" +
"<div class='card-body'>" +
"<div class='panel-heading'>" + "<h4> " + description + "</h4> " +
"<p class='panel-body'>" + "<h3> " + price + "</h3>" +
"<p class='panel-body'> <img class='card-img-top' style='width:250px' height='250px' id='theImg' src='images/" + image + "' /> </p>" +
" <a href='#' class='btn btn-primary'>View More</a> </div> </div></div> </div>");
}
}
});
});
});
get_search.php
<?php
include("db.php");
$stmt = $conn->prepare("SELECT id,cat_id,brand_id,price,description,image,keywords from products WHERE cat_id = ? AND brand_id = ? ");
$stmt->bind_result($id,$cat_id,$brand_id,$price,$description,$image,$keywords);
$cat_id = $_POST["cat"];
$brand = $_POST['brand'];
$stmt->bind_param("ss", $cat_id,$brand);
$stmt->bind_result($id,$cat_id,$brand_id,$price,$description,$image,$keywords);
if ($stmt->execute()) {
while ( $stmt->fetch() ) {
$output = array ("id"=>$id, "cat_id"=>$cat_id,"brand_id"=>$brand_id,"price"=>$price,"description"=>$description,"image"=>$image,"keywords"=>$keywords);
}
echo json_encode( $output );
}
$stmt->close();
?>
php jquery ajax
1
What exactly does "it is not working well" mean? Are you not getting the correct results? Are you not getting any results?
– Dave
Nov 15 '18 at 14:28
no sir. i am not getting the correct result sir.
– user3489210
Nov 15 '18 at 14:34
1
What are you getting? What are you expecting?
– Dave
Nov 15 '18 at 14:35
if i click category as Tv and brand as LG relevant product should display. category and brand both search at a time
– user3489210
Nov 15 '18 at 14:37
Your search query is looking for exact matches for both category and brand. If the columns don't have exactly the same values you won't get any results. You may want to consider usingLIKE
instead of=
in your query. Also, check the values that are being posted to make sure they contain the values you think they do.
– Dave
Nov 15 '18 at 14:42
|
show 9 more comments
i am creating a e-commerce website. Example if i click category as Tv and brand as LG the relevant product should display.but it is not working well.no error diplayed
what I tried so far i have written below.
Form Design
Category
<div align="left">
<li class="list-group-item list-group-item-action active" >
<h4>Category</h4></li>
<li class="list-group-item list-group-item-action" id="categories"></li>
</div>
Brand
<div align="left" >
<li class="list-group-item list-group-item-action active" ><h4>Brand</h4></li>
<li class="list-group-item list-group-item-action" id="brands"></li>
</div>
Products
<div class="panel-heading">
Products
</div>
<div class="panel-body" id="Products" >
</div>
jquery
Category
function getCategory(){
$.ajax({
type: 'GET',
url: 'get_category.php' ,
dataType: 'JSON',
success: function (data)
{
for (var i = 0; i < data.length; i++)
{
var catname = data[i].catname;
var id = data[i].id;
$('#categories').append('<a href="#" cid= '+ id + ' class="list-group-item list-group-item-action">' + '<b>'+ data[i].catname + '<b>' + '</a>');
}
},
error: function (xhr, status, error)
{
console.log(xhr.message)
}
});
}
Brand
function getBrand(){
$.ajax({
type: 'GET',
url: 'all_brand.php' ,
dataType: 'JSON',
success: function (data) {
console.log(data);
for (var i = 0; i < data.length; i++)
{
var id1 = data[i].id;
$('#brands').append('<a href="#" bid= '+ id1 + ' class="list-group-item list-group-item-action">' + '<b>'+ data[i].brand_name + '<b>' + '</li>');
}
},
error: function (xhr, status, error) {
alert(xhr.responseText);
}
});
}
Categroy and brand diplayed succesfully.
Product
if i click category and brand at the time the relevant product will display. this how i wrote query
$(document).ready(function() {
$("#categories a.list-group-item").click(function () {
var cat = $(this).attr('cid');
var brand = $("#brands a.list-group-item").click();
var brand = $(this).attr('bid');
$.ajax({
url: 'get_search.php',
type: 'post',
data: {cat: cat, brand: brand},
// data: {cat: cat},
dataType: 'json',
success: function (data) {
var len = data.length;
console.log(data);
$("#Products").empty();
for (var i = 0; i < data.length; i++) {
var price = data[i].price;
var image = data[i].image;
var description = data[i].description;
$("#Products").append("<div class='col-md-4'> " +
"<div class='panel panel-info' id='Products'>" +
"<div class='card-body'>" +
"<div class='panel-heading'>" + "<h4> " + description + "</h4> " +
"<p class='panel-body'>" + "<h3> " + price + "</h3>" +
"<p class='panel-body'> <img class='card-img-top' style='width:250px' height='250px' id='theImg' src='images/" + image + "' /> </p>" +
" <a href='#' class='btn btn-primary'>View More</a> </div> </div></div> </div>");
}
}
});
});
});
get_search.php
<?php
include("db.php");
$stmt = $conn->prepare("SELECT id,cat_id,brand_id,price,description,image,keywords from products WHERE cat_id = ? AND brand_id = ? ");
$stmt->bind_result($id,$cat_id,$brand_id,$price,$description,$image,$keywords);
$cat_id = $_POST["cat"];
$brand = $_POST['brand'];
$stmt->bind_param("ss", $cat_id,$brand);
$stmt->bind_result($id,$cat_id,$brand_id,$price,$description,$image,$keywords);
if ($stmt->execute()) {
while ( $stmt->fetch() ) {
$output = array ("id"=>$id, "cat_id"=>$cat_id,"brand_id"=>$brand_id,"price"=>$price,"description"=>$description,"image"=>$image,"keywords"=>$keywords);
}
echo json_encode( $output );
}
$stmt->close();
?>
php jquery ajax
i am creating a e-commerce website. Example if i click category as Tv and brand as LG the relevant product should display.but it is not working well.no error diplayed
what I tried so far i have written below.
Form Design
Category
<div align="left">
<li class="list-group-item list-group-item-action active" >
<h4>Category</h4></li>
<li class="list-group-item list-group-item-action" id="categories"></li>
</div>
Brand
<div align="left" >
<li class="list-group-item list-group-item-action active" ><h4>Brand</h4></li>
<li class="list-group-item list-group-item-action" id="brands"></li>
</div>
Products
<div class="panel-heading">
Products
</div>
<div class="panel-body" id="Products" >
</div>
jquery
Category
function getCategory(){
$.ajax({
type: 'GET',
url: 'get_category.php' ,
dataType: 'JSON',
success: function (data)
{
for (var i = 0; i < data.length; i++)
{
var catname = data[i].catname;
var id = data[i].id;
$('#categories').append('<a href="#" cid= '+ id + ' class="list-group-item list-group-item-action">' + '<b>'+ data[i].catname + '<b>' + '</a>');
}
},
error: function (xhr, status, error)
{
console.log(xhr.message)
}
});
}
Brand
function getBrand(){
$.ajax({
type: 'GET',
url: 'all_brand.php' ,
dataType: 'JSON',
success: function (data) {
console.log(data);
for (var i = 0; i < data.length; i++)
{
var id1 = data[i].id;
$('#brands').append('<a href="#" bid= '+ id1 + ' class="list-group-item list-group-item-action">' + '<b>'+ data[i].brand_name + '<b>' + '</li>');
}
},
error: function (xhr, status, error) {
alert(xhr.responseText);
}
});
}
Categroy and brand diplayed succesfully.
Product
if i click category and brand at the time the relevant product will display. this how i wrote query
$(document).ready(function() {
$("#categories a.list-group-item").click(function () {
var cat = $(this).attr('cid');
var brand = $("#brands a.list-group-item").click();
var brand = $(this).attr('bid');
$.ajax({
url: 'get_search.php',
type: 'post',
data: {cat: cat, brand: brand},
// data: {cat: cat},
dataType: 'json',
success: function (data) {
var len = data.length;
console.log(data);
$("#Products").empty();
for (var i = 0; i < data.length; i++) {
var price = data[i].price;
var image = data[i].image;
var description = data[i].description;
$("#Products").append("<div class='col-md-4'> " +
"<div class='panel panel-info' id='Products'>" +
"<div class='card-body'>" +
"<div class='panel-heading'>" + "<h4> " + description + "</h4> " +
"<p class='panel-body'>" + "<h3> " + price + "</h3>" +
"<p class='panel-body'> <img class='card-img-top' style='width:250px' height='250px' id='theImg' src='images/" + image + "' /> </p>" +
" <a href='#' class='btn btn-primary'>View More</a> </div> </div></div> </div>");
}
}
});
});
});
get_search.php
<?php
include("db.php");
$stmt = $conn->prepare("SELECT id,cat_id,brand_id,price,description,image,keywords from products WHERE cat_id = ? AND brand_id = ? ");
$stmt->bind_result($id,$cat_id,$brand_id,$price,$description,$image,$keywords);
$cat_id = $_POST["cat"];
$brand = $_POST['brand'];
$stmt->bind_param("ss", $cat_id,$brand);
$stmt->bind_result($id,$cat_id,$brand_id,$price,$description,$image,$keywords);
if ($stmt->execute()) {
while ( $stmt->fetch() ) {
$output = array ("id"=>$id, "cat_id"=>$cat_id,"brand_id"=>$brand_id,"price"=>$price,"description"=>$description,"image"=>$image,"keywords"=>$keywords);
}
echo json_encode( $output );
}
$stmt->close();
?>
php jquery ajax
php jquery ajax
edited Nov 15 '18 at 15:54
user3489210
asked Nov 15 '18 at 13:53
user3489210user3489210
195
195
1
What exactly does "it is not working well" mean? Are you not getting the correct results? Are you not getting any results?
– Dave
Nov 15 '18 at 14:28
no sir. i am not getting the correct result sir.
– user3489210
Nov 15 '18 at 14:34
1
What are you getting? What are you expecting?
– Dave
Nov 15 '18 at 14:35
if i click category as Tv and brand as LG relevant product should display. category and brand both search at a time
– user3489210
Nov 15 '18 at 14:37
Your search query is looking for exact matches for both category and brand. If the columns don't have exactly the same values you won't get any results. You may want to consider usingLIKE
instead of=
in your query. Also, check the values that are being posted to make sure they contain the values you think they do.
– Dave
Nov 15 '18 at 14:42
|
show 9 more comments
1
What exactly does "it is not working well" mean? Are you not getting the correct results? Are you not getting any results?
– Dave
Nov 15 '18 at 14:28
no sir. i am not getting the correct result sir.
– user3489210
Nov 15 '18 at 14:34
1
What are you getting? What are you expecting?
– Dave
Nov 15 '18 at 14:35
if i click category as Tv and brand as LG relevant product should display. category and brand both search at a time
– user3489210
Nov 15 '18 at 14:37
Your search query is looking for exact matches for both category and brand. If the columns don't have exactly the same values you won't get any results. You may want to consider usingLIKE
instead of=
in your query. Also, check the values that are being posted to make sure they contain the values you think they do.
– Dave
Nov 15 '18 at 14:42
1
1
What exactly does "it is not working well" mean? Are you not getting the correct results? Are you not getting any results?
– Dave
Nov 15 '18 at 14:28
What exactly does "it is not working well" mean? Are you not getting the correct results? Are you not getting any results?
– Dave
Nov 15 '18 at 14:28
no sir. i am not getting the correct result sir.
– user3489210
Nov 15 '18 at 14:34
no sir. i am not getting the correct result sir.
– user3489210
Nov 15 '18 at 14:34
1
1
What are you getting? What are you expecting?
– Dave
Nov 15 '18 at 14:35
What are you getting? What are you expecting?
– Dave
Nov 15 '18 at 14:35
if i click category as Tv and brand as LG relevant product should display. category and brand both search at a time
– user3489210
Nov 15 '18 at 14:37
if i click category as Tv and brand as LG relevant product should display. category and brand both search at a time
– user3489210
Nov 15 '18 at 14:37
Your search query is looking for exact matches for both category and brand. If the columns don't have exactly the same values you won't get any results. You may want to consider using
LIKE
instead of =
in your query. Also, check the values that are being posted to make sure they contain the values you think they do.– Dave
Nov 15 '18 at 14:42
Your search query is looking for exact matches for both category and brand. If the columns don't have exactly the same values you won't get any results. You may want to consider using
LIKE
instead of =
in your query. Also, check the values that are being posted to make sure they contain the values you think they do.– Dave
Nov 15 '18 at 14:42
|
show 9 more comments
1 Answer
1
active
oldest
votes
I knew there was a reason I disliked mysqli. Suggest you switch to using PDO and the code below will get you the results you are looking for. Building the array to return in JSON format is left for you to complete.
$conn = new PDO('mysql:host=localhost;dbname=test;charset=utf8mb4','username','password');
$_POST["cat"] = 'TV';
$_POST['brand'] = 'LG';
$stmt = $conn->prepare('SELECT id,description from products
WHERE cat_id LIKE :catid AND brand_id LIKE :brand');
$cat_id = '%' . $_POST["cat"] . '%';
$brand = '%' . $_POST['brand'] . '%';
$stmt->execute([':catid' => '%' . $cat_id . '%',
':brand' => '%' . $brand . '%']);
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
print_r($results);
sir my jquery having a problem cat_id and brand_id not posted to get_search.php page but no error display can you check the query on Product query
– user3489210
Nov 15 '18 at 15:55
1
You need to add the columns you want to the query. I only created 3 of them for testing. And, as I said, you need to create the JSON to be returned. I only got you past your query issues.
– Dave
Nov 15 '18 at 16:05
("#categories a.list-group-item").click(function () { var cat = $(this).attr('cid'); var brand = $("#brands a.list-group-item").click(); var brand = $(this).attr('bid'); is this correct we post the value to get_search page. values are not posted
– user3489210
Nov 15 '18 at 16:13
sir it is working now
– user3489210
Nov 15 '18 at 17:12
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53321004%2fcategory-and-brand-both-seach-at-a-time-in-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I knew there was a reason I disliked mysqli. Suggest you switch to using PDO and the code below will get you the results you are looking for. Building the array to return in JSON format is left for you to complete.
$conn = new PDO('mysql:host=localhost;dbname=test;charset=utf8mb4','username','password');
$_POST["cat"] = 'TV';
$_POST['brand'] = 'LG';
$stmt = $conn->prepare('SELECT id,description from products
WHERE cat_id LIKE :catid AND brand_id LIKE :brand');
$cat_id = '%' . $_POST["cat"] . '%';
$brand = '%' . $_POST['brand'] . '%';
$stmt->execute([':catid' => '%' . $cat_id . '%',
':brand' => '%' . $brand . '%']);
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
print_r($results);
sir my jquery having a problem cat_id and brand_id not posted to get_search.php page but no error display can you check the query on Product query
– user3489210
Nov 15 '18 at 15:55
1
You need to add the columns you want to the query. I only created 3 of them for testing. And, as I said, you need to create the JSON to be returned. I only got you past your query issues.
– Dave
Nov 15 '18 at 16:05
("#categories a.list-group-item").click(function () { var cat = $(this).attr('cid'); var brand = $("#brands a.list-group-item").click(); var brand = $(this).attr('bid'); is this correct we post the value to get_search page. values are not posted
– user3489210
Nov 15 '18 at 16:13
sir it is working now
– user3489210
Nov 15 '18 at 17:12
add a comment |
I knew there was a reason I disliked mysqli. Suggest you switch to using PDO and the code below will get you the results you are looking for. Building the array to return in JSON format is left for you to complete.
$conn = new PDO('mysql:host=localhost;dbname=test;charset=utf8mb4','username','password');
$_POST["cat"] = 'TV';
$_POST['brand'] = 'LG';
$stmt = $conn->prepare('SELECT id,description from products
WHERE cat_id LIKE :catid AND brand_id LIKE :brand');
$cat_id = '%' . $_POST["cat"] . '%';
$brand = '%' . $_POST['brand'] . '%';
$stmt->execute([':catid' => '%' . $cat_id . '%',
':brand' => '%' . $brand . '%']);
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
print_r($results);
sir my jquery having a problem cat_id and brand_id not posted to get_search.php page but no error display can you check the query on Product query
– user3489210
Nov 15 '18 at 15:55
1
You need to add the columns you want to the query. I only created 3 of them for testing. And, as I said, you need to create the JSON to be returned. I only got you past your query issues.
– Dave
Nov 15 '18 at 16:05
("#categories a.list-group-item").click(function () { var cat = $(this).attr('cid'); var brand = $("#brands a.list-group-item").click(); var brand = $(this).attr('bid'); is this correct we post the value to get_search page. values are not posted
– user3489210
Nov 15 '18 at 16:13
sir it is working now
– user3489210
Nov 15 '18 at 17:12
add a comment |
I knew there was a reason I disliked mysqli. Suggest you switch to using PDO and the code below will get you the results you are looking for. Building the array to return in JSON format is left for you to complete.
$conn = new PDO('mysql:host=localhost;dbname=test;charset=utf8mb4','username','password');
$_POST["cat"] = 'TV';
$_POST['brand'] = 'LG';
$stmt = $conn->prepare('SELECT id,description from products
WHERE cat_id LIKE :catid AND brand_id LIKE :brand');
$cat_id = '%' . $_POST["cat"] . '%';
$brand = '%' . $_POST['brand'] . '%';
$stmt->execute([':catid' => '%' . $cat_id . '%',
':brand' => '%' . $brand . '%']);
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
print_r($results);
I knew there was a reason I disliked mysqli. Suggest you switch to using PDO and the code below will get you the results you are looking for. Building the array to return in JSON format is left for you to complete.
$conn = new PDO('mysql:host=localhost;dbname=test;charset=utf8mb4','username','password');
$_POST["cat"] = 'TV';
$_POST['brand'] = 'LG';
$stmt = $conn->prepare('SELECT id,description from products
WHERE cat_id LIKE :catid AND brand_id LIKE :brand');
$cat_id = '%' . $_POST["cat"] . '%';
$brand = '%' . $_POST['brand'] . '%';
$stmt->execute([':catid' => '%' . $cat_id . '%',
':brand' => '%' . $brand . '%']);
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
print_r($results);
answered Nov 15 '18 at 15:42
DaveDave
2,73981729
2,73981729
sir my jquery having a problem cat_id and brand_id not posted to get_search.php page but no error display can you check the query on Product query
– user3489210
Nov 15 '18 at 15:55
1
You need to add the columns you want to the query. I only created 3 of them for testing. And, as I said, you need to create the JSON to be returned. I only got you past your query issues.
– Dave
Nov 15 '18 at 16:05
("#categories a.list-group-item").click(function () { var cat = $(this).attr('cid'); var brand = $("#brands a.list-group-item").click(); var brand = $(this).attr('bid'); is this correct we post the value to get_search page. values are not posted
– user3489210
Nov 15 '18 at 16:13
sir it is working now
– user3489210
Nov 15 '18 at 17:12
add a comment |
sir my jquery having a problem cat_id and brand_id not posted to get_search.php page but no error display can you check the query on Product query
– user3489210
Nov 15 '18 at 15:55
1
You need to add the columns you want to the query. I only created 3 of them for testing. And, as I said, you need to create the JSON to be returned. I only got you past your query issues.
– Dave
Nov 15 '18 at 16:05
("#categories a.list-group-item").click(function () { var cat = $(this).attr('cid'); var brand = $("#brands a.list-group-item").click(); var brand = $(this).attr('bid'); is this correct we post the value to get_search page. values are not posted
– user3489210
Nov 15 '18 at 16:13
sir it is working now
– user3489210
Nov 15 '18 at 17:12
sir my jquery having a problem cat_id and brand_id not posted to get_search.php page but no error display can you check the query on Product query
– user3489210
Nov 15 '18 at 15:55
sir my jquery having a problem cat_id and brand_id not posted to get_search.php page but no error display can you check the query on Product query
– user3489210
Nov 15 '18 at 15:55
1
1
You need to add the columns you want to the query. I only created 3 of them for testing. And, as I said, you need to create the JSON to be returned. I only got you past your query issues.
– Dave
Nov 15 '18 at 16:05
You need to add the columns you want to the query. I only created 3 of them for testing. And, as I said, you need to create the JSON to be returned. I only got you past your query issues.
– Dave
Nov 15 '18 at 16:05
("#categories a.list-group-item").click(function () { var cat = $(this).attr('cid'); var brand = $("#brands a.list-group-item").click(); var brand = $(this).attr('bid'); is this correct we post the value to get_search page. values are not posted
– user3489210
Nov 15 '18 at 16:13
("#categories a.list-group-item").click(function () { var cat = $(this).attr('cid'); var brand = $("#brands a.list-group-item").click(); var brand = $(this).attr('bid'); is this correct we post the value to get_search page. values are not posted
– user3489210
Nov 15 '18 at 16:13
sir it is working now
– user3489210
Nov 15 '18 at 17:12
sir it is working now
– user3489210
Nov 15 '18 at 17:12
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53321004%2fcategory-and-brand-both-seach-at-a-time-in-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
What exactly does "it is not working well" mean? Are you not getting the correct results? Are you not getting any results?
– Dave
Nov 15 '18 at 14:28
no sir. i am not getting the correct result sir.
– user3489210
Nov 15 '18 at 14:34
1
What are you getting? What are you expecting?
– Dave
Nov 15 '18 at 14:35
if i click category as Tv and brand as LG relevant product should display. category and brand both search at a time
– user3489210
Nov 15 '18 at 14:37
Your search query is looking for exact matches for both category and brand. If the columns don't have exactly the same values you won't get any results. You may want to consider using
LIKE
instead of=
in your query. Also, check the values that are being posted to make sure they contain the values you think they do.– Dave
Nov 15 '18 at 14:42