title: How to pass a SESSION variable within a loop between PHP pages
body: <p>I'm trying to pass the variables <code>$candid</code> and <code>$clientid</code> from active.php to editor.php. This is the code I have so far for active.php:</p>
  
  <pre><code>$active_tsquery = db_select("SELECT * FROM timesheets WHERE status=\"cand\" OR status=\"client\" ORDER BY weekending ASC"); 
      echo "&lt;table width=\"90%\"&gt;
          &lt;tr&gt;
          &lt;td width=\"20%\"&gt;Client Company&lt;/td&gt;
          &lt;td width=\"20%\"&gt;Candidate&lt;/td&gt;
          &lt;td width=\"20%\"&gt;Week Ending&lt;/td&gt;
          &lt;td width=\"20%\"&gt;Action&lt;/td&gt;
          &lt;/tr&gt;";
  
      foreach($active_tsquery as $key =&gt; $value) {
          $clientid = $value["clientid"];
          $_SESSION['clientid'] = $clientid;
          $weekend = $value["weekending"];
          $clientresult = db_select("SELECT * FROM timesheetlogin WHERE id=\"$clientid\"");
          $candid = $value["candid"];
          $_SESSION['candid'] = $candid;
          $candresult = db_select("SELECT * FROM timesheetlogin WHERE id=\"$candid\"");
          $ts_id = $value["ts_id"];
          $_SESSION['tsid'] = $ts_id;
  
          foreach ($clientresult as $key =&gt; $value) {
              $company = $value["company"];
          }
          foreach ($candresult as $key =&gt; $value) {
              $candidate = $value["name"];
          }
  
          echo "&lt;tr&gt;&lt;td class=\"main-text\"&gt;$company&lt;/td&gt;&lt;td class=\"main-text\"&gt;
  $candidate&lt;/td&gt;&lt;td class=\"main-text\"&gt;$weekend&lt;/td&gt;&lt;td class=\"main-text\"&gt;
  &lt;a href=\"editor.php?id=$ts_id\" class=\"main-link\"&gt;Edit&lt;/a&gt;&lt;/td&gt;&lt;td class=\"main-link\"&gt;
  &lt;a href=\"deleteconfirm.php?id=$ts_id\" class=\"main-link\"&gt;Delete&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;";
      } 
  </code></pre>
  
  <p>Active.php is working as <code>&lt;a href=\"editor.php?id=$ts_id\" class=\"main-link\"&gt;Edit&lt;/a&gt;</code> is giving the correct urls. </p>
  
  <p>This is the code I have in editor.php:</p>
  
  <pre><code>$candid = $_SESSION['candid'];
  $clientid = $_SESSION['clientid'];
  $candresult = db_select("SELECT * FROM timesheetlogin WHERE id=\"$candid\"");
  $clientresult = db_select("SELECT * FROM timesheetlogin WHERE id=\"$clientid\"");
  $tsresult = db_select("SELECT * FROM timesheets WHERE candid=\"$candid\"");
  var_dump($tsresult);
  </code></pre>
  
  <p>The <code>var_dump($tsresult);</code> is an array of 1, returning the last timesheet every time. I'm assuming because the <code>$candid</code> is wrong. The foreach loop in active.php has finished and <code>$candid</code> is being returned from the last item in the array?</p>
  
  <p>I am using <code>session_start();</code> at the top of each file BTW.</p>