Solved

inserting variable in a link

Posted on 2011-02-14
5
337 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 108

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 108

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

707 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now