donglilian0061
donglilian0061
2014-01-25 17:56
浏览 61
已采纳

PHP在URL中显示%20的空格?

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";
}

?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dounie5475
    dounie5475 2014-01-25 18:05
    已采纳

    Its normal that %20 is added, as spaces are not really url friendly and abit discouraged within file naming conventions, the 20 is the hex(base16) representation of a space char.

    If it effect your code you could always use urldecode() to convert it back.

    Why does it have to be encoded?

    Going to the HTTP layer when you make a request to the web your browser will open a socket to the server and do a HTTP request:

    GET category_list.php?category=tooth-white%20range HTTP/1.1 //Good

    GET category_list.php?category=tooth-white range HTTP/1.1 //Bad

    If you have a space this will cause the request to break as the protocol there is a space after the url in the HTTP request.

    点赞 评论
  • duan1989643
    duan1989643 2014-01-25 18:05

    URLs cannot contain spaces. They should be encoded as %20. Either your browser or the server is fixing it before PHP gets hold of it.

    Use urldecode to convert content from a URL to text.

    点赞 评论
  • dongrong7267
    dongrong7267 2014-01-25 18:10

    When a website URL is encoded %20 is the ASCII code for a space and you can only transmit ASCII characters. In PHP all Superglobals ($_POST, $_GET, etc) are urldecoded by default (and further more, you shouldn't try to decode them again). It's just a different representation of your value. In your code where you build the link, you could easily:

    $dynamicList2 .= '<li> <a href="category_list.php?category=' . urlencode($category) . '">' . $category . '</a> </li>';

    This will ensure your link is built in the proper encoded manner. If you are ever referencing superglobals though, do not urldecode them. The warning in the PHP Documentation specificially mentions this.

    http://us2.php.net/manual/en/function.urldecode.php

    点赞 评论

相关推荐