0

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?

1
  • maybe put it inside a href? <a href="http://www.google...">Text</a> Commented Dec 18, 2014 at 10:40

4 Answers 4

2

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>';
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.