title: PHP MySQL image hit counter
body: <p>I'm new to PHP and have been working on a hit counter. The hit counter works great but now I want to convert the numbers into images.</p>
  
  <p>I have created 12 images 0-9, a spacer, and a comma image.</p>
  
  <p>I have searched high and low for a hint of what I need to do to convert the number format into images and have had no success. So far all I have found is how to make a basic hit counter using files only, PHP/MySQL, and how to display an encrypted image using PHP/MySQL.</p>
  
  <p><strong>So the question is:</strong>
  How do I tell the given code to show images in place of each number?</p>
  
  <p><em>Example of current PHP result: <strong>Hit: 2,435</em></strong></p>
  
  <p>I want my PHP to get the total number of hits <em>(example)</em> and then take and replace the 2,435 with the following code:</p>
  
  <pre><code>&lt;img src="img/2.png"&gt;&lt;img src="img/comma.png"&gt;&lt;img src="img/4.png"&gt;&lt;img src="img/3.png"&gt;&lt;img src="img/5.png"&gt;
  </code></pre>
  
  <p><hr>
  <strong>Note: I use lots of notes in the code I show here. This way any new coders can more easily understand the scripts being displayed. I will add my final/completed code at the bottom of this post so everyone can see the final product when I have found a solution.</strong>
  <hr>
  <em>This code is fully fictional as a text hit counter</em></p>
  
  <pre><code>// Begin open SQL connection to database
  $concount = mysqli_connect("site","username","password","database");
  // End connection to database
  
  // Begin update number of hits
  mysqli_query($concount,"UPDATE counter SET hits = hits + 1");
  // End update number of hits
  
  // Begin get number of hits
  $hits = ("SELECT SUM(hits) FROM counter");
  // End get number of hits
  
  // Begin show number of hits
  $result = mysqli_query($concount,$hits);
  while($row = mysqli_fetch_array($result)) {
    echo "Hits:&amp;nbsp;" . number_format((float)$row['0']) . "&amp;nbsp;";
  }
  // End show number of hits
  
  // Begin close SQL connection
  mysqli_close($con);
  // End close SQL connection
  </code></pre>
  
  <p><hr>
  <strong>Edit: Below is the final result of my code.
  Note that the array in this script puts a ' at the beginning and end of the image array. (See following example)</strong></p>
  
  <pre><code>Array ( [0] =&gt; ' [1] =&gt; 2 [2] =&gt; 4 [3] =&gt; 3 [4] =&gt; 5 [5] =&gt; ' )
  </code></pre>
  
  <p><strong>So unless I wanted a broken image on ether end of my hit counter I had to utilize them. I renamed my transparent image that I had already planed on using on both ends to '.png (See following example)</strong></p>
  
  <pre><code>&lt;img src="img/'.png"&gt;&lt;img src="img/2.png"&gt;&lt;img src="img/4.png"&gt;&lt;img src="img/3.png"&gt;&lt;img src="img/5.png"&gt;&lt;img src="img/'.png"&gt;
  </code></pre>
  
  <p><hr>
  <strong>Final Code</strong>
  <em>This code is fully fictional as a image hit counter</em></p>
  
  <pre><code>// Begin open SQL connection to database
  $concount = mysqli_connect("site","username","password","database");
  // End connection to database
  
  // Begin update number of hits
  mysqli_query($concount,"UPDATE counter SET hits = hits + 1");
  // End update number of hits
  
  // Begin get number of hits
  $hits = ("SELECT SUM(hits) FROM counter");
  // End get number of hits
  
  // Begin assign $hits an id
  $result = mysqli_query($concount,$hits);
  while($row = mysqli_fetch_array($result)) {
  $totalhits=("'" . $row[0] . "'");
  }
  // End assign $hits an id
  
  // Begin get id for number of hits, split the string into array, and assign id to numbers
  $arr = str_split($totalhits);
  $numbers = $arr;
  foreach ($numbers as $value) {
  // End get id for number of hits, split the string into array, and assign id to numbers
  
  // Begin show number of hits as images
      echo "&lt;img src=\"img/".$value.".png\"&gt;";
  } 
  // End show number of hits as images
  
  // Begin close SQL connection
  mysqli_close($con);
  // End close SQL connection
  </code></pre>
  
  <p><hr>
  <strong>Final Notes:
  I have not tried adding a comma to larger numbers or removing the apostrophe on the array yet. If I do I'll come back and edit this.</strong></p>