Jump to content

last page


magcr23

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.