jonboy
jonboy

Reputation: 2749

add php variable in url link

I am printing out some variables within a table using a for each loop, I would like to pass one of these variables "car" to a URL. The URL should look as follows;

http://www.google.co.uk/search?q=[VALUE_OF_CARS_HERE]&btnG=Search+Books&tbm=bks&tbo=1&gws_rd=ssl

My for each successfully prints the results and looks as follows;

foreach ($this->books as $book) {
echo '<td>'.$book->id.'</td>';
echo '<td>'.$book->title.'</td>';
}

I have tried the following;

echo '<td>http://www.google.co.uk/search?q='.$book->title.'&btnG=Search+Books&tbm=bks&tbo=1&gws_rd=ssl</td>';

This simply prints out the text but it's not 'clickable';

http://www.google.co.uk/search?q=Cars&btnG=Search+Books&tbm=bks&tbo=1&gws_rd=ssl

Where am I going wrong?

Upvotes: 0

Views: 1630

Answers (4)

Brett
Brett

Reputation: 20049

As per others answers you need to put it inside an anchor tag such as:

$link = 'http://www.google.co.uk/search?q='.$book->title.'&btnG=Search+Books&tbm=bks&tbo=1&gws_rd=ssl';
echo '<td><a href="' . $link .'">' . $book->title . '</a></td>';

However, I would also recommend using urlencode on your book title within the URL, so..

$link = 'http://www.google.co.uk/search?q=' . urlencode($book->title) . '&btnG=Search+Books&tbm=bks&tbo=1&gws_rd=ssl';
echo '<td><a href="' . $link .'">' . $book->title . '</a></td>';

Upvotes: 1

Gauthier
Gauthier

Reputation: 557

The problem doesn't come from the php side of your code but come from a simple forgotten tag. To make a link clickable, you need to use the tag like :

<a href="your link">your text</a>

Try :

echo '<td><a href="http://www.google.co.uk/search?q='.$book->title.'&btnG=Search+Books&tbm=bks&tbo=1&gws_rd=ssl">link text</a></td>';

Upvotes: 2

Marco Mura
Marco Mura

Reputation: 582

Sorry but the url need the a tag.

Based on

echo '<td>http://www.google.co.uk/search?q='.$book->title.'&btnG=Search+Books&tbm=bks&tbo=1&gws_rd=ssl</td>';

you can do this:

echo '<td><a href="http://www.google.co.uk/search?q='.$book->title.'&btnG=Search+Books&tbm=bks&tbo=1&gws_rd=ssl">http://www.google.co.uk/search?q='.$book->title.'&btnG=Search+Books&tbm=bks&tbo=1&gws_rd=ssl</td></a>';

or this:

echo '<td><a href="http://www.google.co.uk/search?q='.$book->title.'&btnG=Search+Books&tbm=bks&tbo=1&gws_rd=ssl">My Link Title</td></a>';

Upvotes: 0

blue112
blue112

Reputation: 56432

You need to put an a (anchor) tag to make it clickable.

$url = 'http://www.google.co.uk/search?q='.$book->title.'&btnG=Search+Books&tbm=bks&tbo=1&gws_rd=ssl';
echo '<td><a href="'.$url.'">'.$url.'</a></td>';

Upvotes: 2

Related Questions