Using strings in a URL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kdidymus
Forum Contributor
Posts: 196
Joined: Tue May 13, 2008 3:37 am

Using strings in a URL

Post by kdidymus »

Haylp!

I am designing a family tree website. I have produced a database which currently contains one of my ancestors (for test purposes). Each ancestor has a Unique Reference Number (URN).

http://www.didymus.org.uk/display.php

This works fine because the URN of my ancestor is specified in the body of the PHP by using the line:

$urn = "583";

So when you access the above page, the row with URN 583 is called from the MySQL database and displayed.

What I WANT to be able to do is to make this variable and call data from the MySQL DB dependent on the hyperlink clicked by the user

i.e. http://www.didymus.org.uk/display.php?urn=583

But whatever I do I cannot get it to work. I've tried changing the PHP code to:

$urn = "hlurn"

and using the URL http://www.didymus.org.uk/display.php?hlurn=583

But when I try, the page is blank.

What do I need to do to get this function to work?

Many, MANY thanks for saving what little hair I have left!

Kris Didymus.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Using strings in a URL

Post by aceconcepts »

How are you "GETTING" the url variable?

When you pass a variable via the url you need to "GET" it from within your script (display.php)

Code: Select all

 
//url e.g. http://www.didymus.org.uk/display.php?urn=583
 
//To get urn from the above url you need to do:
   $urn=$_GET['urn'];
 
However, you may want to use some error trapping to ensure integrity:

Code: Select all

 
//url e.g. http://www.didymus.org.uk/display.php?urn=583
 
if(isset($_GET['urn']) && !empty($_GET['urn']))
{
   $urn=$_GET['urn'];
} else {
//msg - no urn passed
}
 
 
kdidymus
Forum Contributor
Posts: 196
Joined: Tue May 13, 2008 3:37 am

Re: Using strings in a URL

Post by kdidymus »

You are a star. It works like a dream. I'm using one of the "For Dummies" books and, as you can probably tell, I'm a complete Dummy.

Will be asking lots more questions as my little project grows!

Thanks again.

KD.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Using strings in a URL

Post by aceconcepts »

Everyone has to start somewhere

Glad it works :D
Post Reply