duanchan9354 2014-04-29 12:05
浏览 38
已采纳

PHP - 在数组中构建URL和参数值

URL parameters are being built (into the $url variable) based on user input values, like this:

$query = trim($_GET["Query"]);
if (!empty($query)) {
$url = "results.php?";
$url .= "&Query=$query";
}
$store = $_GET["Store"];
if (!empty($store)) {
$url .= "&Store=$store";
}

These URL parameters (built into the $url variable) will be used to parse an XML document. Then ultimately search results will be displayed.

<product category="Tablet">
<name>ipad-1</name>
<store>Best Buy</store> // problem will later occur when store element has two words
</product>

<product category="Tablet">
<name>ipad-2</name>
<store>Amazon</store>  
</product>

Multiple pages of search results are produced, and the page number links are created here.

for($i=0; $i<$searchResultsFound; $i++) {
if ($i == $a_condition) {
//This array below is where the probably likely is
$pageNumberLinks[] = "<a href=".$url."&firstSearchResult=".$firstSearchResultValue."&lastSearchResult=".$lastSearchResultValue.">".$pageNumberLink_i . "</a>";
}}

The page number links are displayed using this foreach loop.

foreach ($pageNumberLinks as $PageNumberLinks) {
echo $PageNumberLinks . " ";
}

The page number links are working fine when the store parameter (pulled from the XML element named 'store') have only one word (IE Amazon), and the URL in the address bar looks like this:

results.php?&Query=ipad&store=Amazon&firstSearchResult=10&lastSearchResult=20

However, when the store parameter has two words (IE Best Buy), the URL only displays the first word 'Best' and cuts off the remainder of the URL in the address bar, and looks like this:

results.php?&Query=ipad&store=Best

And the source code looks like this:

<a href=results.php?&Query=ipad&store=Best&firstSearchResult=10&lastSearchResult=20>2</a>

My guess is that the page number links are not coded correctly when inserted into the array in the for loop above. Does anyone notice anything coded incorrectly here:

$pageNumberLinks[] = "<a href=".$url."&firstSearchResult=".$firstSearchResultValue."&lastSearchResult=".$lastSearchResultValue.">".$pageNumberLink_i . "</a>";

As a side note, the URL works fine when coded like this:

<a href="<?php echo $url; ?>&Store=<?php echo $store; ?>">Anchor text</a>
  • 写回答

1条回答 默认 最新

  • dtqqq24248 2014-04-29 14:53
    关注

    You need to use urlencode or http_build_query to make life easier. Still do all your checking of what parameters are in the correct format but build the actual query using one of these functions

    Check the manual pages for both of these functions and decide which one better suits your needs

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?