Solved

inserting variable in a link

Posted on 2011-02-14
5
344 Views
Last Modified: 2012-05-11
This should be a very easy question for you php ninjas.

I want to create a variable in my webpage, like this?

<%php i=100; %>

<a href="http://www.test.com/?=<%php i %>" >link 100</a>

What is the exact code to declare the variable and use it in a link so that the resulting link is http://www.test.com/?=100

Thanks!
0
Comment
Question by:keks_
  • 2
  • 2
5 Comments
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
ID: 34888633
Something along these lines assuming that $i = 100

<a href="http://www.test.com/?=<?php echo $i; ?>" >link 100</a>
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34888636
Actually, if you want the URL as the text of the link as well then


<a href="http://www.test.com/?=<?php echo $i; ?>" >http://www.test.com/?=<?php echo $i; ?></a>

0
 
LVL 13

Expert Comment

by:darren-w-
ID: 34888888
or you can create a function:
<?php

function createHyperlink($path, $text) {
    return "<a href = '$path'>$text</a>";
}
?>

<?php echo createHyperlink("http://www.google.co.uk", "A link to google"); ?>

Open in new window

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34889401
Some introductory help with PHP is available here: http://us.php.net/tut.php

Another way of doing this is to use HEREDOC notation.  I like HEREDOC because it helps me keep logic and presentation separated.


// UNTESTED CODE BUT VALID IN PRINCIPLE

// SET VALUE FOR ARGUMENT IN STRING NOTATION
$i = '100';

// ALL URL VARIABLES MUST BE URLENCODED
$url_i = urlencode($i);

// DECLARE THE LINK USING HEREDOC
$link = <<<LINK
<a href="http://www.test.com/?=$url_i" >link 100</a>
LINK;

// SHOW THE LINK
echo $link;

Open in new window

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34889421
Sidebar note.  You might want to try that URL something more like this...
<a href="http://www.test.com?q=$url_i" >

In your test.com/index.php script you will find a variable named $_GET["q"] with a value equal to the contents of $url_i
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question