title: Building a discount code system using PHP
body: <p>I'm working on a simple flat file database based discount code system. So the users get to the checkout page and they can enter a given code and the amount related to that code will be deducted from the final value of the shopping cart. It's nothing complicated.</p>
  <p>I can do the deduction etc. but I'm confused because I'm using a flat file database and I don't have a clue if things like <code>SELECT, WHERE clause</code> etc will work in a flat file database. And for that reason, I cannot relate the discount code to its amount to be deducted.</p>
  <p>Here is what I have so far:</p>
  <pre><code>if (isset($_POST['disCode'])) {
      $disCode = $_POST['disCode'];
      $arr=file(&quot;FlatFile1.txt&quot;);
  
      foreach($arr as $str)
      {
          list($code,$amount)=explode(&quot;|&quot;,$str);
      }
      
      $Total =  $Total - $disCode;
  }
  </code></pre>
  <p>As you can see in the code above, I use <code>$Total =  $Total - $disCode;</code>, so if i put a number (example 10 or 5 or any other number), and submit the form, it will take off (deduct) that amount from the <code>$Total</code>.</p>
  <p>But I need to use the <code>$code</code> which is pulled from the <code>FlatFile1.txt</code> file and then deduct the <code>$amount</code> related to that <code>$code</code> from the <code>$Total</code>.</p>
  <p>The flat file structure is like so:</p>
  <pre><code>RFHSEP34 | 20 
  
  RETBBHUJ | 10 
  
  TYRHBNNM | 5
  </code></pre>
  <p>The first part is the <code>$code</code> that will be given to the users and the second part after <code>|</code> is the <code>$amount</code> that will be deducted from the <code>$Total</code> is the $code is entered.</p>
  <h2>Edit</h2>
  <p>I've edited my code to the following. I am getting close to what I want but it doesn't matter what I type in the input field (disCode), I always get the <code>$amount</code> of the last discount code deducted from the <code>$Total</code>!</p>
  <pre><code>    $arr=file(&quot;flatFile1.txt&quot;);
  
      foreach($arr as $str)
      {
          list($code,$amount)=explode(&quot;|&quot;,$str);
      }
  
  
  if (isset($_POST['disCode'])) {
      $disCode = $_POST['disCode'];
  
      if ($disCode = $code)
      {
          $fDis = $amount;
          $Total =  $Total - $fDis;
      
      }
  }
  </code></pre>
  <p>What can I do to resolve this?</p>
  <p>To re-produce my issue, you can create a .txt file and call it <code>flatFile1.txt</code>. and then use the flat file code above in that file. and then create a simple php page with my last code in it. and finally create a simple form with an input field named <code>disCode</code> and simple submit button on the same .php page.</p>
  <p>As for the value of <code>$Total</code> you can simply use the following:</p>
  <p><code>$Total = 50;</code> or <code>$Total = '50';</code></p>
  <h2>Edit 2</h2>
  <p>I have been spending far too long for such simple function with no avail so I will give up on it if I can't get an answer to this final edit.</p>
  <p>I will paste the entire code from my php page so there won't any confusion!</p>
  <pre><code>&lt;?php
  $Total = 500;
  $arr=file(&quot;flatFile1.txt&quot;);
  
  foreach($arr as $str){
      list($code,$amount)=explode(&quot;|&quot;,$str);
  }
  if (isset($_POST['disCode'])) {
      $disCode = $_POST['disCode'];
  
      if ($disCode == $code)
      {
          $Total -= $amount;
      }
  }
  
  ?&gt;
  &lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
  &lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
  &lt;head&gt;
  &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
  &lt;title&gt;Untitled Document&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  
  &lt;div id=&quot;disDiv&quot;&gt;&lt;form action=&quot;carttest&quot; method=&quot;post&quot;&gt;
      
          &lt;input type=&quot;text&quot; name=&quot;disCode&quot;/&gt;&lt;br /&gt;&lt;br /&gt;
          
          &lt;input TYPE=&quot;image&quot; SRC=&quot;discountBtn.png&quot;
          BORDER=&quot;0&quot; name=&quot;discountCode&quot; id=&quot;disCount&quot; class=&quot;disCount&quot; /&gt;&lt;/form&gt;&lt;/div&gt;&lt;br /&gt;
          
          
          &lt;?php echo $Total; ?&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p>The code above doesn't work at all. which means it does not deduct the <code>$amount</code> from the <code>$Total</code>.</p>
  <p>However, if I change the <code>if ($disCode == $code)</code> to <code>if ($disCode = $code)</code>, it will deduct the last <code>$amount</code> in the <em>flatFile.txt</em> without typing anything in the disCode inputfield. so i just need to submit the form and the amount will be deduced which is not what I am looking for.</p>