user773440
user773440

Reputation: 868

how to build query string in magento

Hi want to build the query string in magento. I tried

<?php 
echo $this->getUrl("catalog/category/view",
  array(
    "_use_rewrite"=>false,
    "category"=>$_category->getId(),
    "product"=>$_product->getId()
  )
);
?>

i want the url: http://www.localhost.com/hungermunch/fujigrill/catalog/category/view?category=11&product=1 but im getting

http://www.localhost.com/hungermunch/fujigrill/catalog/category/view/category/11/product/1/

how can i get the required url . Is it possible

Upvotes: 3

Views: 8410

Answers (2)

Nathan Stokes
Nathan Stokes

Reputation: 116

You can also append url querystring params in Magento like this:

$params = array(
    '_query' => array(
        'category' => $_category->getId(),
        'product'  => $_product->getId(),
    )
);

echo Mage::getUrl('catalog/category/view', $params);

Here's a reference for the getUrl() method:

http://www.magentocommerce.com/wiki/5_-_modules_and_development/reference/geturl_function_parameters

Upvotes: 4

Andrew
Andrew

Reputation: 12809

If you want to add on a query string to the end you could use this method:

$this->getUrl("catalog/category/view") . "?" .
http_build_query(
    "category" => $_category->getId(),
    "product"  => $_product->getId()
);

Although I see no reason not to use your first method, and then get the values from Magento as you need them, for example inside a controller you can do this:

$productId = $this->getRequest()->getParam('product');
$categoryId = $this->getRequest()->getParam('category');

Magento will then get those values for you from the url generated by your code.

Upvotes: 1

Related Questions