magcr23 Posted June 22, 2015 Share Posted June 22, 2015 Hi guys, i have this code: <ol class="breadcrumb"> <li class="active"> <a href="#">Back</a> </li> </ol> </div> </div> <div class="row"> <?php include("conexao.php"); ?> <?php $utilizador = $_SESSION["user"]; $mensagem=$_GET['mensagem']; $result = mysqli_query($con, "SELECT * FROM mensagens WHERE emissor = '$utilizador' AND id= $mensagem"); $ln = mysqli_fetch_assoc($result); echo 'De: ' . $ln['emissor'] . '<br/>'; echo 'Assunto: ' . $ln['assunto'] . '<br />'; echo 'Mensagem: ' . $ln['mensagem'] . '<br />'; ?> I want to change that: <ol class="breadcrumb"> <li class="active"> <a href="#">Back</a> </li> </ol> I have 3 pages: pag1, pag2, function I want it to do that, if the user was on pag1.php and came to function the link is <a href="pag1.php">Back</a> if the user was on pag2 and came to function the link is <a href="pag2.php">Back</a> How can i do that? Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted June 22, 2015 Share Posted June 22, 2015 There are a couple of options using PHP's super-globals. You can pass the previous page through the url and have your destination pick it up with a $_GET request, you can $_POST the info through using a hidden form field and a submit event, you can access the $_SERVER[] array to get the HTTP_REFERER or HTTP_HOST+HTTP_REQUEST_URI or you can set a $_SESSION variable to carry the information through. If you would like help with a specific option let us know which one. Quote Link to comment Share on other sites More sharing options...
magcr23 Posted June 22, 2015 Author Share Posted June 22, 2015 Maybee the URL option, but i don't have many experience with that, i get a litle confused after a few lines of code... but it's the only one i've already used. Wich one do you advise? Quote Link to comment Share on other sites More sharing options...
fastsol Posted June 22, 2015 Share Posted June 22, 2015 I use something like this: $_SESSION['last_page_queried'] = $_SERVER['SCRIPT_NAME'].'?'.$_SERVER['QUERY_STRING']; <a href="<?php echo $_SESSION['last_page_queried']; ?>">Back</a> Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted June 23, 2015 Share Posted June 23, 2015 I, personally would use sessions, but that's just my opinion, there are arguments to and for each method. If you are most familiar with passing data through the url, then just go with it. Firstly you need to alter all your links to the page to make sure they are passing the variable that you need. //page1.php links: $linkPostfix = "?refPage=1"; //for mobility and convenience make a variable to append the new url variable data ... <a href="function.php<?php echo $linkPostfix;?>">Forward</a> <!-- these will be your new links (assuming the same formatting as your other code) Any decent code editor will change all entries in a single, well formed, search/replace. --> ... //page2.php is pretty much the same $linkPostfix = "?refPage=2"; //obviously this one is 2 instead of one ... <a href="function.php<?php echo $linkPostfix;?>">Forward</a> <!-- again, search and replace should make this pretty painless. --> ... Then, in function.php, use a conditional statement to check the value of $_GET['refPage'] and generate the "Back" link to suit. You can either use if/elseif/else or a switch statement. I'm going to stick on ye olde if. //function.php if(!isset($_GET['refPage']){ //code to perform if no refPage variable is passed - such as a header redirect to a home page or an error message } elseif($_GET['refPage'] == "1"){ $backlink = "page1.php"; // relate a backlink to refPage value of 1 - which we set in page1.php } elseif($_GET['refPage'] == "2"){ $backlink = "page2.php"; //you get the idea } //notice no else - we don't want a "catch-all" value applied to the backlink for values out of range, but if you did want to this is where it would go. /*-------------------------------- //because there is ever the possibility that someone will end up on the page without passing the refPage var, //and because we only set a value to $backlink if refPage is set, then we need to make sure $backlink is set //before trying to render the links, or else the script will get upset --------------------------------*/ if(!isset($backlink)){$backlink="#"} ... //The last thing left to do is change the links on the page to: <a href="<?php echo $backlink; ?>">Back</a> <!-- remember Search and Replace is your friend --> ... // all done. That's pretty much all there is to it. 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.