JeBu Posted July 25, 2007 Share Posted July 25, 2007 How it is done? I mean...hm.. for example, i have following HTML code <a href="index.php?option=login">Log in</a> ..and a PHP code to switch pages <?php switch($_GET['option']) { case "login": { include("login.php"); break; default: { echo "Error 404"; break; } } ?> Do I just type a variable in the URL and switch it with php or what?..pff.. Any kind of help/link/article is welcome And articles about PHP headers are also welcome Quote Link to comment Share on other sites More sharing options...
plutomed Posted July 25, 2007 Share Posted July 25, 2007 You can type anything in the address line but you will have to have something in the php code for it to do anything Quote Link to comment Share on other sites More sharing options...
JeBu Posted July 25, 2007 Author Share Posted July 25, 2007 Yes I know, but how are the variables set/how they can be accessed (only by $_GET[]?) and I would like to know more, articles are welcomed Quote Link to comment Share on other sites More sharing options...
plutomed Posted July 25, 2007 Share Posted July 25, 2007 To get a variable out of the address bar you have to put $_GET['var'] http://w3schools.com/php/php_get.asp Quote Link to comment Share on other sites More sharing options...
Fadion Posted July 25, 2007 Share Posted July 25, 2007 The idea of url variables is to have a url like: index.php?page=login and then use php to catch the variable and its value with $_GET. Example: if(isset($_GET['page'])){ echo $_GET['page']; //it will print the page variable, in this case 'login' } else{ echo 'You have not set a url variable'; } In this way you can pass variables between different pages or just different parts of your php script. Quote Link to comment Share on other sites More sharing options...
trq Posted July 25, 2007 Share Posted July 25, 2007 The urls are usually generated dynamically from data contained within a database. eg; list_users.php <?php // connect to db if ($result = mysql_query("SELECT id,uname FROM users")) { if (mysql_num_rows($result)) { while($row = mysql_fetch_assoc($result)) { echo "<a href='profile.php?id={$row['id']}'>{$row['uname']}</a><br />"; } } } ?> profile.php <?php if (isset($_GET['id'])) { $id = mysql_real_escape_string($_GET['id']); if (mysql_query("SELECT uname,age FROM users WHERE id = '$id' LIMIT 1")) { if (mysql_num_rows($result)) { $row = mysql_fetch_assoc($result); echo "This is the profile of {$row['uname']}, who is {$row['age']} years old"; } } } ?> list_users.php list all users names as links, passing each users id through the url. When you click on these links you are taken to profile.php which uses this id to run a query and gather more information about the given user. Hope this helps. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.