[英]How can i replace a string in php url
i made a search box which use $_GET method. 我做了一个使用$ _GET方法的搜索框。 I search for category and size.
我搜索类别和大小。
When the category search submitted my url becomes: 提交类别搜索后,我的网址变为:
index.php?category=1
through: 通过:
<li class=""><a href="index.php?category=<?php echo $category['id']; ?>"><?php echo $category['name']; ?></a></li>
When size submitted my url is: 提交尺寸后,我的网址是:
index.php?category=1&size=1
through: 通过:
<li id="">
<a href="<?php echo $_SERVER['REQUEST_URI'].'&size='; ?><?php echo $size['id']; ?>"><?php echo $size['name']; ?></a>
</li>
When i search for another size i have: 当我搜索其他尺寸时,我有:
index.php?category=1&size=1&size=2
How can i just replace the size instead of adding it again in my url? 我该如何替换大小而不是在URL中再次添加大小?
Use 采用
<?php echo $_SERVER['PHP_SELF'];?>
Instead of 代替
<?php echo $_SERVER['REQUEST_URI'];?>
REQUEST_URI
is adding query strings to your request. REQUEST_URI
正在向您的请求添加查询字符串。
Where as PHP_SELF
gives plain file name eg index.php
. 其中
PHP_SELF
给出纯文件名,例如index.php
。
You have to use http_build_query
function to build your URL from the associative array of parameters. 您必须使用
http_build_query
函数从关联的参数数组中构建URL。 And try to avoid doing it directly in a template. 并尝试避免直接在模板中执行此操作。 Move this code at least to the controller level.
将此代码至少移到控制器级别。
// Somewhere at the controller
$category = ...;
$size = ...;
$searchParams = array();
if (!empty($category)) {
$name = $category['name'];
$searchParams['category'] = $category['id'];
}
if (!empty($size)) {
$name = $size['name'];
$searchParams['size'] = $size['id'];
}
$searchQuery = http_build_query($searchParams);
// Somehow pass it to the template
$this->addViewVar('searchUrl', $_SERVER['REQUEST_URI'] . $searchQuery);
$this->addViewVar('searchName', $name);
And in the template just use searchUrl
variable: 在模板中,只需使用
searchUrl
变量:
<li class=""><a href="<?php echo $searchUrl; ?>"></a><?php echo $searchName; ?></li>
Another way to accomplish this task, if you don't have a separate controllers level, it's to create special functions build_search_url()
and search_name()
: 完成此任务的另一种方法是,如果没有单独的控制器级别,则可以创建特殊功能
build_search_url()
和search_name()
:
function build_search_url($path, $category = null, $size = null) {
$searchParams = array();
if (!empty($category)) {
$searchParams['category'] = $category['id'];
}
if (!empty($size)) {
$searchParams['size'] = $size['id'];
}
$searchQuery = http_build_query($searchParams);
return $path . $searchQuery;
}
And then use it like this: 然后像这样使用它:
<li class="">
<a href="<?php echo build_search_url($_SERVER['REQUEST_URI'], $category, $size); ?>">
<?php echo search_name($category, $size); ?>
</a>
</li>
Easiest way IMHO: 恕我直言最简单的方法:
Set the new value for the index in $_GET (or a copy, if you don't want to manipulate the original array) – and then use http_build_query to create a new query string from that. 在$ _GET中设置索引的新值(如果您不想操纵原始数组,则为副本)–然后使用http_build_query从中创建新的查询字符串。
You should call a function in you "href" tag. 您应该在“ href”标签中调用一个函数。 Inside, use http_build_query($array) to reconstruct the array.
在内部,使用http_build_query($ array)重建数组。
$data = array('category' => $_GET['category']);
$data['size'] = $_GET['size'] ? $_GET['size'] : NEW_SIZE;
function getQuery($data) {
return http_build_query($data);
}
You can also use $_SERVER['REQUEST_URI'], but I'm not sure that this function will replace your item value instead of adding it to the end of the query. 您也可以使用$ _SERVER ['REQUEST_URI'],但是我不确定此函数将替换您的项目值,而不是将其添加到查询的末尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.