title: PHP Display Count Total for a Variable within FOREACH loop
body: <p>I have a foreach loop and I want to get the count/total of $item for the second echo. Is there a way to count a variable and display the total within a foreach loop? If not, can I do a seperate sql and store the count and pass it to the spot indicated below, or can you only return variables from one sql array ($row in this case)?</p>
  
  <p>The results would look like:<br>
  Cost   &nbsp;&nbsp;Item<br>
  Cost 1 &nbsp;&nbsp;3<br>
  Cost 2  &nbsp;&nbsp;1<br>
  Cost 3  &nbsp;&nbsp;4  etc..</p>
  
  <p>However, the items are not stored in the same table as Cost. They are connected by a table ID join. So in the foreach, $cost and $item have to be the results of two seperate queries. My question I guess is, if I do that, how will each row know which item to match with each cost as it loops? </p>
  
  <pre><code>foreach ($row as $this) {
  echo "&lt;td&gt;$this[cost]&lt;/td&gt;"; 
  echo "&lt;td&gt; //want to get count of $item variable here! &lt;/td&gt;";
  </code></pre>