简体   繁体   中英

How can i replace a string in php url

i made a search box which use $_GET method. 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?

Use

<?php echo $_SERVER['PHP_SELF'];?>

Instead of

<?php echo $_SERVER['REQUEST_URI'];?>

REQUEST_URI is adding query strings to your request.

Where as PHP_SELF gives plain file name eg index.php .

You have to use http_build_query function to build your URL from the associative array of parameters. 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:

<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() :

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.

You should call a function in you "href" tag. Inside, use http_build_query($array) to reconstruct the 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM