this is a bit odd question and i never faced this before but here we are:
I have created a simple PHP/MYSQL shop. everything works etc
BUT when I add a product and there is a space
OR /
OR -
in the category name and when i click on the category name in the front end it doesn't show the product(s) that are in that category and when i look at the URL in the address bar it will show it like this:
category_list.php?category=tooth-white%20range
as you can see it has converted the tooth-white range
to tooth-white%20range
if i write the category name like toothwhiterange
it will work as it should!!
Here is how I get the categories to show up:
<?php
// Run a select query to get my letest 6 items
// Connect to the MySQL database
include "config/connect.php";
$dynamicList2 = "";
$sql = "SELECT DISTINCT category FROM products";
$query = mysqli_query($db_conx, $sql);
$productCount = mysqli_num_rows($query); // count the output amount
if ($productCount > 0) {
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$category = $row["category"];
$dynamicList2 .= '<li>
<a href="category_list.php?category=' . $category . '">' . $category . '</a>
</li>';
}
} else {
$dynamicList2 = "NO Cats Yet!";
}
?>
could someone please help me with this?
Thanks
EDIT:
sorry guys I really don't know how to explain this but here we go again:
the problem is when i put a space
in the name of the category like name of the category
, the name of the category will show up on the front end but when i click on it there wont be any products under that category even though there are some products under that category in the database.
However when i remove the spaces or any special characters like -
or /
from the category name and write it like nameofthecategory
it works just fine and it will show the products under that category!
SECOND EDIT:
Here is how i get/display the products under each category depending on what category is selected:
<?php
if (isset($_GET['category'])) {
// Run a select query to get my letest 6 items
// Connect to the MySQL database
include "config/connect.php";
$category = preg_replace('~[^a-zA-Z0-9]+~', '', $_GET['category']);
$cList2 = "";
$sql = "SELECT * FROM products WHERE category='".$category."' LIMIT 35" ;
$query = mysqli_query($db_conx, $sql);
$productCount = mysqli_num_rows($query); // count the output amount
if ($productCount > 0) {
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$id = $row["id"];
$product_name = $row["product_name"];
$price = $row["price"];
$category = $row["category"];
$stock = $row["stock"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
$cList2 .= '<table style="float:left; margin-top:10px;" width="25%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center"><a href="product.php?id=' . $id . '"><img style="border:solid 1px #999;" src="inventory_images/' . $id . 'Image1.jpg" width="124" height="124" /></a></td>
</tr>
<tr>
<td id="nameHolder" class="nameHolder" style="text-align:left; padding-left:5px;" rowspan="" height="34"><a href="product.php?id=' . $id . '">' . $product_name . '</a></td>
</tr>
<tr>
<td style="text-align:left; padding-left:5px; color:#C33; font-size:17px;" height="30"><strong style="color:#999;">Price:</strong> £' . $price . '</td>
</tr>
<tr>
<td style="text-align:left; padding-left:5px; color:#C33; font-size:17px;" height="30">' . $stock . '</td>
</tr>
<tr>
<td style="text-align:left; padding-left:5px;" height="15"><a href="product.php?id=' . $id . '">view</a></td>
</tr>
<tr>
<td height="50" align="left"><form method="post" action="product.php?id=' . $id . '"><input type="submit" class="button" name="button" id="button" value="Add to cart" /></form></td>
</tr>
</table>';
}
}
} else {
$cList2 = "We have no products listed in our store yet";
}
?>