Help - Search - Members - Calendar
Full Version: Php Varible Question
Zymic Webmaster Forums > Web Design & Development > Server Side Scripting > PHP
LadyYepperz
Is it possible to pass a variable through a link?
I need to be able to call a variable after a link has been clicked to be used later in the script. Something like this.

CODE
while($rows = mysql_fetch_array($result)){
     print "<a href='testlink.php?action=author' title='$rows[author]' name='author' value='$rows[author]'>$rows[author]</a>";
}

switch($_GET[action]){
    case "author":
        $sname="$_GET[author]";
        print $sname;
        break;
  
    default:
        include_once("testlink.php");
        break;
}


I know the $GET wont work on the "name" & "value" variables in the link. I only wrote it like this to show you what I want to do. Is there a way to call a variable from a link?
Ed
If you're merely using the values for indexing or querying against, you can use the following:

CODE
link.php?a=1&b=2&c=3


These are then available within link.php as:

CODE
$_GET['a']
$_GET['b']
$_GET['c']


However if you're using these values to be placed in a database etc, you should be using a POST request.

The other option you have is to use a session:

CODE
session_start();

$values = array(
   'a' => 1,
   'b' => 2,
   'c' => 3
);

$_SESSION['foo'] = $values;


Providing you call 'session_start()' at the top of the page you can use retrieve the subsequent values from the session like so:

CODE
session_start();

echo $_SESSION['foo']['a'], ', ', $_SESSION['a'], ' , ', $_SESSION['foo']['b'], ', ', $_SESSION['foo']['c'];


Hope that helps.
LadyYepperz
CODE
link.php?a=1&b=2&c=3

I don't think this will work for me.

My links call a switch statement...
My problem is.. i need to transfer another variable because THAT variable is needed to query the DB.
This is a DB driven site added to often by members. When a new row is added to the DB the links have to update accordingly so, the links are written with a while statement. Certain links will lead to the same switch statement but the query needs to be different for each. Hence why i need to pass a 2nd variable.

This would not be a problem if i could use a while statement to write the inside of my switch statement but PHP is being difficult! Can you amazing people help me around this problem.

Please & Thank You!
swordz
I don't understand exactly what you need. Can you explain it slightly better? Or give a live example?

Why can't you just pass the 2nd variable you also need?

swordz


LadyYepperz
Background: This site is for authors to show off their short stories.

CODE
print "<a href='testlink.php?action=author' title='$rows[author]'>$rows[author]</a>";

These links will lead to a switch statement ("action"), ("case=author") that will show the bio page. My links are written by a while statement to update when a new author joins the community.

I need to query the DB for a specific author when his/her link is clicked on.
LadyYepperz
biggrin.gif biggrin.gif biggrin.gif
I figured out how to pass a variable through a link ... for those who might like to know how I did it.


CODE
print "<a href='testlink.php?action=author' title='$rows[author]'>".$var=$rows[author]."</a>";

Even with the "$var" only the author's name prints to the screen.

Thank you Swordz & Bread for replying to my thread!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2012 Invision Power Services, Inc.