title: Keep all previous data according to condition
body: <p>I am trying to display two product names depending on a counter. I've declared a session variable 'counter'. I want to show the first product name if the button is clicked, and I want to show two product names if the button is clicked again.</p>
  <blockquote>
  <p>Dell</p>
  <p>Mac</p>
  </blockquote>
  <p>However, if I click the button the second time, the second product name replaces the first product name; I want both products to display.</p>
  <p>How would I fix this?</p>
  <p>My code:</p>
  <pre><code>&lt;?php
  session_start();
  include('connect.php');
  
  // if counter is not set, set to zero
  if (!isset($_SESSION['counter'])) {
      $_SESSION['counter'] = 0;
  }
  
  // if button is pressed, increment counter
  if (isset($_POST['button'])) {
      ++$_SESSION['counter'];
      // Get data according to id 
  
      $userid2 = $_POST['id2'];
      $query3 = &quot;select * from product where  id='&quot; . $userid2 . &quot;'&quot;;
      $result3 = mysql_query($query3) or die(mysql_error());
      $data3 = mysql_fetch_array($result3);
  
      if ($_SESSION['counter'] == 1) {
          echo $data3['name'] . &quot;&lt;br&gt;&quot;;
      }
  
      if ($_SESSION['counter'] == 2) {
          echo $data3['name'];
      }
  }
  
  // reset counter
  if (isset($_POST['reset'])) {
      $_SESSION['counter'] = 0;
  }
  
  $query2 = &quot;select * from product &quot;;
  $result = mysql_query($query2) or die(mysql_error());
  
  $data = mysql_fetch_array($result);
  
  $sr = $data['sr'];
  $name = $data['name'];
  $price = $data['price'];
  $qunt = $data['quantity'];
  
  $total += $data['price'];
  
  while ($data = mysql_fetch_array($result)) {
      ?&gt;
      &lt;form method=&quot;POST&quot; action=&quot;&quot;&gt;
          &lt;table border=&quot;1&quot;&gt;
              &lt;tr&gt;
                  &lt;?PHP
                  echo &quot;&lt;tr&gt;&lt;td&gt;&quot; . $data['id'] . &quot;&lt;/td&gt;&lt;td&gt;&quot; . $sr . &quot;&lt;/td&gt;&lt;td &gt;&quot; . $name . &quot;&lt;/a&gt;&lt;/td&gt;&lt;td&gt;&quot; . $price . &quot;&lt;/td&gt;&lt;td&gt;&quot; . $qunt . &quot;&lt;/td&gt; &quot;;
                  ?&gt;
              &lt;tr&gt;
          &lt;/table&gt;
          &lt;input type=&quot;hidden&quot; name=&quot;id2&quot; value=&quot;&lt;?php echo $data['id'] ?&gt;&quot;/&gt;
  
          &lt;input type=&quot;hidden&quot; name=&quot;counter&quot; value=&quot;&lt;?php echo $_SESSION['counter']; ?&gt;&quot;/&gt;
          &lt;input type=&quot;submit&quot; name=&quot;button&quot; value=&quot;Counter&quot;/&gt;
          &lt;input type=&quot;submit&quot; name=&quot;reset&quot; value=&quot;Reset&quot;/&gt;
  
          &lt;br/&gt;&lt;?php echo $_SESSION['counter']; ?&gt;
      &lt;/form&gt;
  &lt;?php }
  ?&gt;
  </code></pre>