404 Not Found
404 Not Found

Reputation: 1223

$id is not going with the link

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.

Upvotes: 0

Views: 164

Answers (3)

Amal Murali
Amal Murali

Reputation: 76666

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']);

Upvotes: 2

Slowwie
Slowwie

Reputation: 1246

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.

Upvotes: 1

Paul Denisevich
Paul Denisevich

Reputation: 2414

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/>';

Upvotes: 2

Related Questions