The first argument of the anchor()
function can accept an array or a string, so:
$segments = array(
'product',
'id',
'1',
'?version=12&name=mydoc'
);
# with array
echo anchor($segments, 'click me');
# with string
echo anchor('/product/id/1?version=12&name=mydoc', 'click me');
the difference between the two is that the former will output a trailing slash after the $id
(with value 1) segment:
# version 1
http://localhost/product/id/1/?version=12&name=mydoc
While the latter no:
# version 2
http://daniweb.ci/product/id/1?version=12&name=mydoc
but, in both cases it should work fine, the point is to include the ?
in the segment.
If you want to generate only the second version of the links through an array, then this could be done by extending the url helper, for example by simply exploding the string at the ?
character and removing the right slash. In application/helpers
create the file MY_url_helper.php and paste this:
<?php
if ( ! function_exists('my_anchor'))
{
function my_anchor($uri = '', $title = '', $attributes = '')
{
$title = (string) $title;
if ( ! is_array($uri))
{
$site_url = ( ! preg_match('!^\w+://! i', $uri)) ? site_url($uri) : $uri;
}
else
{
$site_url = site_url($uri);
}
if ($title == '')
{
$title = $site_url;
}
if ($attributes != '')
{
$attributes = _parse_attributes($attributes);
}
# new edit
$querystring = explode('?', $site_url);
$querystring[0] = rtrim($querystring[0], '/');
$site_url = implode('?', $querystring);
# stop edit
return '<a href="'.$site_url.'"'.$attributes.'>'.$title.'</a>';
}
}
Then from the view call the new function my_anchor()
as submit the same arguments of the anchor()
helper:
$segments …