Hi I have some products on a website I'm building that I want to be able to filter by the product range that they come from. I have managed to make a filter for specific brands using a drop down menu, but would like to be able to filter the range when clicking on a label/selecting a radio button. I am using php and jquery to try and achieve this, can anybody tell me where I'm going wrong and what I need to do to fix this?
This is my php/html which shows the options of product ranges.
<!-- Product Range -->
<div id="product_range">
<?php
$sql = "SELECT * FROM `product_range`";
$result = $conn->query($sql);
if ($result->num_rows > 0){
// Output data of each rowt
while($row = $result->fetch_assoc()){
//echo "Brand : " . $row["brand"] . " - Product : " . $row["name"] . " - " . $row["description"] . " - Price : " . $row["price"] . "<br>";
echo "<div class='col-lg-6 col-md-4 col-sm-5 col-xs-12 product_range " . $row['brandName'] . " all' id='products'>
<div class='hovereffect'>
<img class='img-responsive product_range_img' src='" . $row['img'] . "' alt=''>
<div class='overlay1'>
<h2> " . $row['name'] . "</h2>
<p>
" . $row['title'] . "
<br>
<br>
<form>
<input type='radio' class='radio' name='selector' value='" . $row['product_range'] . "' />
<label for='" . $row['product_range'] . "' id='labelSelect'>
View Products
</label>
</form>
</p>
</div>
</div>
</div>";
}
} else {
echo "0 results";
}
?>
</div>
I then have this part which is the products i wish to display on click.
<!-- Product packs -->
<div id="product_group">
<?php
$sql = "SELECT * FROM `product_packs`";
$result = $conn->query($sql);
if ($result->num_rows > 0){
// Output data of each rowt
while($row = $result->fetch_assoc()){
//echo "Brand : " . $row["brand"] . " - Product : " . $row["name"] . " - " . $row["description"] . " - Price : " . $row["price"] . "<br>";
echo "<div class='col-lg-2.4 col-md-3.4 col-sm-4 col-xs-12 products " . $row['brandName'] . " all " . $row['product_range'] . "' id='products'>
<div class='hovereffect'>
<img class='img-responsive productimg' src='" . $row['img'] . "' alt=''>
<div class='overlay1'>
<h2> " . $row['name'] . "</h2>
<p>
" . $row['title'] . "
<br>
" . $row['header'] . "
<br>
<br>
" . $row['price'] . "
<br>
<a href='#'>
Add To Cart
</a>
</p>
</div>
<div class='overlay2'>
<div class='overlay3'>
<p>
This is the description
</p>
</div>
</div>
</div>
</div>";
}
} else {
echo "0 results";
}
?>
</div>
<!-- Single products -->
<div id="product">
<?php
$sql = "SELECT * FROM `products`";
$result = $conn->query($sql);
if ($result->num_rows > 0){
// Output data of each rowt
while($row = $result->fetch_assoc()){
//echo "Brand : " . $row["brand"] . " - Product : " . $row["name"] . " - " . $row["description"] . " - Price : " . $row["price"] . "<br>";
echo "<div class='col-lg-2.4 col-md-3.4 col-sm-4 col-xs-12 products " . $row['brandName'] . " all " . $row['product_range'] . "' id='products'>
<div class='hovereffect'>
<img class='img-responsive productimg' src='" . $row['img'] . "' alt=''>
<div class='overlay1'>
<h2> " . $row['name'] . "</h2>
<p>
" . $row['title'] . "
<br>
<br>
" . $row['price'] . "
<br>
<a href='#'>
Add To Cart
</a>
</p>
</div>
<div class='overlay2'>
<div class='overlay3'>
<p>
" . $row['description'] . "
</p>
</div>
</div>
</div>
</div>";
}
} else {
echo "0 results";
}
?>
</div>
this is the jquery i have at the moment, but it only displays the products, not yet filter them.
$(document).ready(function(){
$("#labelSelect").click(function(){
$("#product_range").hide();
$("#product_group").show();
$("#product").show();
$("." + $("input[type='radio'][name='selector']:checked")).show();
$("products:not(." + $("input[type='radio'][name='selector']:checked") + ")").hide();
});
});
Thanks in advance for your help.
Related
The following code works fine and it gives the required output:
<a target="_blank" href="' . $property_post_link . '" style="color:#f8953a;font-size:18px;margin-left:0px;text-decoration:none"><b>' . $property_name . '</b></a>
<div style="color:#f8953a;font-style:italic">
<!--- <span style="font-weight:bold;">Transaction Type: </span>' . $transaction_type . ' --->
<!--- <span style="font-weight:bold;padding-left:5px;">Category: </span>' . $newprop_category . ' --->
</div>
<div style="color:#f8953a;font-style:italic">
<span style="font-weight:bold">Property Type: </span>' . $proper_type . '
<span style="font-weight:bold;padding-right:5px;">BHK: </span>' . $bhk . '
<span style="font-weight:bold;padding-left:5px;">Status: </span>' . $status . '
</div>
<div style="color:#f8953a;font-style:italic">
<span style="font-weight:bold">Location: </span>' . $loc . '
<span style="font-weight:bold;padding-right:5px;">Building Name: </span>' . $buildingname . '
<span style="font-weight:bold;padding-left:5px;">No. of Floor: </span>' . $no_floor . '
</div>
<div style="color:#f8953a;font-style:italic">
<span style="font-weight:bold">Carpet Area: </span>' . $carpet_area . '
<span style="font-weight:bold;padding-right:5px;">Built up Area: </span>' . $buildup_area . '
<span style="font-weight:bold;padding-left:5px;">Price: </span>' . $price_options_select . '
</div>
<div style="color:#f8953a;font-style:italic">
<span style="font-weight:bold">Contact Person: </span>' . $contactperson . '
</div>
But I need to get this in a table. I am using the following code:
echo "<table border='1'>
<tr><th>Building</th><th>Location</th><th>BHKs</th><th>Built up Area</th><th>Furnishing</th><th>Quote</th><th>Advance Rent</th><th>Deposit</th><th>Parking</th></tr>";
foreach ($the_rows as $the_row) {
echo "<tr><td>" . $the_row->buildingname . "</td>";
echo "<td>" . $the_row->loc . "</td>";
echo "<td>" . $the_row->bhk . "</td>";
echo "<td>" . $the_row->buildup_area . "</td>";
echo "<td>" . $the_row->interior_options_select . "</td>";
echo "<td>" . $the_row->price . "</td>";
echo "<td>" . $the_row->advrent . "</td>";
echo "<td>" . $the_row->deposit . "</td>";
echo "<td>" . $the_row->car_parking . "</td></tr>";
}
echo "</table>";
But this is not fetching the results from the database. Any help is appreciated.
If you are using simple mysql_query() and mysql_fetch_array() then use code below.
echo "<table border='1'>
<tr><th>Building</th><th>Location</th><th>BHKs</th><th>Built up Area</th><th>Furnishing</th><th>Quote</th><th>Advance Rent</th><th>Deposit</th> <th>Parking</th></tr>";
foreach ($the_rows as $the_row) {
echo "<tr><td>" . $the_row['buildingname'] . "</td>";
echo "<td>" . $the_row['loc'] . "</td>";
echo "<td>" . $the_row['bhk'] . "</td>";
echo "<td>" . $the_row['buildup_area'] . "</td>";
echo "<td>" . $the_row['interior_options_select'] . "</td>";
echo "<td>" . $the_row['price']. "</td>";
echo "<td>" . $the_row['advrent'] . "</td>";
echo "<td>" . $the_row['deposit'] . "</td>";
echo "<td>" . $the_row['car_parking'] . "</td></tr>";
}
echo "</table>";
if not than please deiscribe you database code too,
I have a query I'm trying to run in my php code and it is only returning one result, but if i run the same query in phpmyadmin it works. Can anyone tell me where I'm going wrong?
<?php
$sql = "SELECT * FROM `product_packs` WHERE `name` IN('" . implode("', '", $_SESSION['cart_items']) . "')";
$result = $conn->query($sql);
if ($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo "<div class='col-xs-6 col-sm-4 col-md-2 col-lg-2'>
<div class='products " . $row['brandName'] . " all " . $row['product_range'] . "' id='products'>
<div class='hovereffect'>
<img class='img-responsive productimg' src='" . $row['img'] . "' alt=''>
<div class='overlay1'>
<h2> " . $row['name'] . "</h2>
<p>
" . $row['title'] . "
<br>
<br>
" . $row['price'] . "
<br>
<a href='remove_from_cart.php?name=" . $row['name'] . "&price=" . $row['price'] . "'>
Remove From Cart
</a>
</p>
</div>
</div>
</div>";
}
?>
I have printed the query to make sure the result of the implode is correct and it seems to be as i can run the result in phpmyadmin and it works fine.
Any help would be appreciated.
Try the following (NOT tested as far as I have not sufficent data provided form you):
UPDATED:
<?php
$sql = "SELECT * FROM `product_packs` WHERE `name` IN('" . implode("', '", $_SESSION['cart_items']) . "')";
// dump the query send to the database
$var_dump($sql);
$result = $conn->query($sql);
if ($result->num_rows > 0){
$resultset_count = $result->num_rows;
// dump the number of resultsets in the query
var_dump($resultset_count);
while($row = $result->fetch_assoc()){
echo "<div class='col-xs-6 col-sm-4 col-md-2 col-lg-2'>
<div class='products " . $row['brandName'] . " all " . $row['product_range'] . "' id='products'>
<div class='hovereffect'>
<img class='img-responsive productimg' src='" . $row['img'] . "' alt=''>
<div class='overlay1'>
<h2> " . $row['name'] . "</h2>
<p>
" . $row['title'] . "
<br>
<br>
" . $row['price'] . "
<br>
<a href='remove_from_cart.php?name=" . $row['name'] . "&price=" . $row['price'] . "'>Remove From Cart </a>
</p>
</div>
</div>
</div>
</div>";
}
?>
Why don't you use FIND_IN_SET? It will work for you, I don't know your database structure but I have created an example query that could be of help for you
why don't use FIND_IN_SET? it will work for you i don't know your database structure still i have created query for you it might help you
SELECT * FROM product_packs
WHERE (
FIND_IN_SET(cart_items, (SELECT cart_items TABLENAME
WHERE cart_items = '$_SESSION['cart_items']')
)
)
ORDER BY `product_id` ASC
Hi guys I have a problem with my PHP, What I am trying to achieve is this:
<li>Product name from DB
<strong>£499<img src="images/thumb.png" alt="Product name from DB"/></strong>
</li>
and here is my code with php:
<ul id="items">
<?php while($product_data = mysql_fetch_array($query_product_result))
{
$num_rows_products = $num_rows_products - 1;
Print "<li><a href = 'main.php?prodid=" . $product_data["product_id"] . " <strong> Name: " . $product_data["title"] . "</strong></a>";
Print "<strong>Price: £" . $product_data["price"] . "'><img src='images/" . $product_data["mainImageThumbnail"] . "' alt='Product image' /></a></strong></li>";
if($num_rows_products > 0)
Print '<p> nu products ? wtf</p>';
}
?>
For some reason all I get is an image with a hyperlink, no title and no price, any chance you can spot the mistake?
Your not closing the <a> tag properly. See the code below. Notice the missing ">" in your code?
Print "<li><a href = 'main.php?prodid=" . $product_data["product_id"] . "'> <strong> Name: " . $product_data["title"] . "</strong></a>";
Print "<strong>Price: £" . $product_data["price"] . "<img src='images/" . $product_data["mainImageThumbnail"] . "' alt='Product image' /></a></strong></li>";
Use Echo instead. Print won't displays concatenated string.
<ul id="items">
<?php while($product_data = mysql_fetch_array($query_product_result))
{
$num_rows_products = $num_rows_products - 1;
echo "<li><a href = 'main.php?prodid=" . $product_data["product_id"] . " <strong> Name: " . $product_data["title"] . "</strong></a>";
echo "<strong>Price: £" . $product_data["price"] . "'><img src='images/" . $product_data["mainImageThumbnail"] . "' alt='Product image' /></a></strong></li>";
if($num_rows_products > 0)
echo '<p> nu products ? wtf</p>';
}
?>
I'm trying to do a condition within an if statement.
I have a current if statement which determines odd and even for css styling.
Within this I want to add another if statement for the reference number. (outlined as Ref in the code).
I'm not sure how to go about this but I want to add something along the lines of
if ref == 1 then echo ref, I've tried putting an if within an if and I just keep getting syntax errors. Can someone point me in the right direction please?
include "db_connect.php";
$result1 = mysql_query("SELECT * FROM updates ORDER BY id DESC limit 5");
$result2 = mysql_num_rows($result1);
$x = $result2;
while ($row1 = mysql_fetch_array($result1)) {
if (++$x % 2) {
echo "<blockquote class='example-right'>" . $row1['update'] .
"<div class='ref'>Ref: " . $row1['ref'] .
"</div><div class='date-right'> from " . $row1['date'] .
" to " . $row1['todate'] .
"</div> </blockquote> <p>" . $row1['username'] .
"</p> </td>";
echo "</p>";
} else {
echo "<blockquote class='example-obtuse'>" . $row1['update'] .
"<div class='ref'>Ref: " . $row1['ref'] .
"</div><div class='date-right'> from " . $row1['date'] .
" to " . $row1['todate'] .
"</div> </blockquote> <p>" . $row1['username'] .
"</p> </td>";
}
}
the bit I tried to do with 2nd if statement:
include "db_connect.php";
$result1 = mysql_query("SELECT * FROM updates1 ORDER BY id DESC limit 5");
$result2 = mysql_num_rows($result1);
$x = $result2;
while ($row1 = mysql_fetch_array($result1)) {
if (++$x % 2) {
$sch = $row1['schuedled'];
else if ($sch == '1') {
echo "<blockquote class='example-right'>" . $row1['update'] .
"<div class='ref'>Ref: " .$row1['ref'] .
"</div><div class='date-right'> from " . $row1['date'] .
" to " . $row1['todate'] .
"</div> </blockquote> <p>" . $row1['username'] .
"</p> </td>";
else {
echo "<blockquote class='example-right'>" . $row1['update'] .
"</div><div class='date-right'> from " . $row1['date'] .
" to " . $row1['todate'] .
"</div> </blockquote> <p>" . $row1['username'] .
"</p> </td>";
}
}
echo "</p>";
} else {
echo "<blockquote class='example-obtuse'>" . $row1['update'] .
"<div class='assyst-ref'>Ref: " . $row1['ref'] .
"</div><div class='date-right'> from " . $row1['date'] .
" to " . $row1['todate'] .
"</div> </blockquote> <p>" . $row1['username'] .
"</p> </td>";
}
}
Try something like this:
while($row1 = mysql_fetch_array($result1)){
$html = '';
if (++$x % 2 ){
$html .= "<blockquote class='example-right'>";
} else {
$html .= "<blockquote class='example-obtuse'>";
}
$html .= $row1['update'];
if($row1['schuedled'] == '1'){
$html .= "<div class='ref'>Ref: " .$row1['ref'] . "</div>";
}
$html .= "more html";
$html .= "more html";
$html .= "more html";
$html .= "</blockquote>";
echo $html;
}
This my code:
<?php
$lijstDoelmannen = mysql_query("SELECT * FROM Speler WHERE positie = 'Doelman' ORDER BY familienaam, voornaam");
$teller = 1;
while($rij = mysql_fetch_array($lijstDoelmannen))
{
if($teller < 5){
echo "<td><a href='spelerDetail.php?spelerId='" . $rij['id'] . "><img src='images/spelers/unknown.png' alt='' width='50' />
<br /><br />" . $rij["id"] . " " . $rij['familienaam'] . " " . $rij['voornaam'] . "</a></td>";
}
}
?>
The problem is that in the hyperlink the parameter spelerId = spaces (not filled in). If I echo $rij["id"], it gives me the right value.
You have a ' in the wrong spot in your href.
"...<a href='spelerDetail.php?spelerId='" . $rij['id'] . ">..."
This should be:
"...<a href='spelerDetail.php?spelerId=" . $rij['id'] . "'>..."
<a href='spelerDetail.php?spelerId='" . $rij['id'] . ">
You need to move the apostrophe:
<a href='spelerDetail.php?spelerId=" . $rij['id'] . "'>
It's currently ending the link, before the variable is added.
You can also do:
echo "<td><a href='spelerDetail.php?spelerId={$rij['id']}'
while($rij = mysql_fetch_array($lijstDoelmannen))
{
if($teller < 5){
echo "<td><a href='spelerDetail.php?spelerId='" . $rij['id'] . "><img src='images/spelers/unknown.png' alt='' width='50' />
<br /><br />" . $rij["id"] . " " . $rij['familienaam'] . " " . $rij['voornaam'] . "</a></td>";
}
}
?>
I prefer writing the above code this way to avid these types of issues:
while($rij = mysql_fetch_array($lijstDoelmannen)){
if($teller < 5){ ?>
<td><a href="spelerDetail.php?spelerId=<?php echo $rij['id'] ?>">
<img src="images/spelers/unknown.png" alt="" width="50" />
<br /><br /><?php echo $rij['id'] . " " . $rij['familienaam'] . " " . $rij['voornaam'] ?></a></td>
<?php }} ?>