Retaining a value


I have this problem I can’t sort out. This is a link in my code:

<a href="admin.php?do=teams:231&amp;year=2009&amp;state="NV"&amp;sport=1"><img src="images/admin_teams.gif" border="0"></a>

That sends it to this part:

if (isset($_GET["do"])) {
  $do = explode(":",$_GET["do"]);
  if ($do[0] == "teams") { teams(); }
  elseif ($do[0] == "logout") { logout(); }
} else { main(); }

This is where I am having trouble.

function teams() {
  if ( !isset($_POST['year']) ? $year='2011' : $year=$_POST['year'] );
  if ( !isset($_POST['state']) ? $state='KS' : $state=$_POST['state'] );
  if ( !isset($_POST['sport']) ? $sport=1 : $sport=$_POST['sport'] );
$page = <<<END
<form action="admin.php?do=teams" method="post">

How do I get the information from my first link to work with the $_POST in this last one? It works great if you go through the FORM or start from scratch, but how would I pass values from another function and have it recognize them, if it is possible?

Thank you.

You pass variables to a function through arguments

First thing is first and that is your HTML for the anchor link is invalid, you can’t use double quotes " within an attribute that’s wrapped with double quotes as it causes a break in the code. The below HTML is valid and won’t cause any errors:

<a href="admin.php?do=teams:231&amp;year=2009&amp;state=NV&amp;sport=1"><img src="images/admin_teams.gif" border="0"></a>

I am a little confused though as to how you didn’t find your answer to your question on Google or another search engine as it’s dead simple and the opposite of using $_POST, please see the below code which uses $_GET to grab the values from the URL and is less complex them what you currently have as you don’t need to declare the variables within a ternary operator.

$year  = !isset($_POST['year'])  ? isset($_GET['year'])  ? $_GET['year']  : '2011' : $_POST['year'];
$state = !isset($_POST['state']) ? isset($_GET['state']) ? $_GET['state'] : 'KS'   : $_POST['state'];
$sport = !isset($_POST['sport']) ? isset($_GET['sport']) ? $_GET['sport'] : 1      : $_POST['sport'];

I have no idea how your code works nor have I ever seen it done like so, but it works.

I understand how mine worked. if $year is not set, put this value, else use $year.

HOW does yours function? I am not trying to be silly. I have just never seen so much on one line.

Thank you for your help. I have toiled over this all day.

The code i posted above uses what’s called a ternary operator which is a short way of saying “shorthand if statement”, basically the question mark ? is the IF statement and the semicolon : is the ELSE statement. Unlike a regular IF statement which can become quite long and unpleasing to the eye a ternary operator is a lot shorter but allows for the same basic concept.

In the above code the following is happening:

  1. I set the variable in which the value will be set to
  2. I check if the $_POST value exists otherwise continue to the next ternary operator
  3. If the $_GET value exists it gets set otherwise it defaults to a static value

So essentially it’s exactly the same as the following example:

if (isset($_POST['var'])) {
    $var = $_POST['var'];
} else if (isset($_GET['var'])) {
    $var = $_GET['var'];
} else {
    $var = 'default';