I am creating a hyperlink in foreach loop. It is working fine. When I am passing $id in URL parameter then it is not working. my link is showing http://****/test/index.php/test/view?id=**. i don't what i am doing wrong here.
foreach($list as $item)
{
$rs[]=$item['uname'];
$id=$item['uid'];
//var_dump($id); here it's printing $id value...
echo '<b> <a href="/test/index.php/test/view?id="'.$id.'">'.$item['uname'].'</a><br/>';
}
I want to pass $id value with hyperlink. Please suggest me.
It's of course getting printed -- your browser is just not displaying it to you since it's not being correctly parsed as HTML due to the extra "
around the $id
variable.
Set your header as follows:
header('Content-Type: text/plain');
and you'll see that it returns something like:
<b> <a href="/test/index.php/test/view?id="55">FOOBAR</a><br/>
^ ? ^
As you can see, the issue is the extra double-quote before 55.
Change your code to:
echo '<b> <a href="/test/index.php/test/view?id=' . $id .'">'.
$item['uname'] . '</a><br/>';
Alternatively, you could also use double-quotes and enclose your variables inside {}
, like so:
echo "<b> <a href=\"/test/index.php/test/view?id=$id\">{$item['uname']}
</a><br/>";
I'd use sprintf
as it's cleaner.
echo sprintf('<b> <a href="%u">%s</a><br/>', $id, $item['uname']);
You have another ".
Change this:
echo '<b> <a href="/test/index.php/test/view?id="'.$id.'">'.$item['uname'].'</a><br/>';
To this:
echo '<b> <a href="/test/index.php/test/view?id='.$id.'">'.$item['uname'].'</a><br/>';
Try this:
echo "<b><a href='/test/index.php/test/view?id=$id'>$item</a></b><br/>";
This works!
And is the easiest and cleanest option. Inside of the double escaping, the simple is used for the html and through the double escaping all variables are written inside :) . Very simple.