Jump to content

How to display an image for each number


Go to solution Solved by igorek24,

Recommended Posts

1. Create/download 10 different image files for each digit.

2. Create a function to take a numeric string value and return the HTML with an image for each digit

 

The function could go something like this:

 

 

function convertToImage($numberStr)
{
    //Check that input only has numeric characters
    if(!ctype_digit()) { return false; }
 
    //Split string into an array of each character
    $numbersAry = str_split($numberStr);
 
    //Loop through each character and create HTML for image
    $output = "";
    foreach($numbersAry as $number)
    {
        $output .= "<img src='1.jpg' alt='1'>";
    }
 
    //Return results
    return $output;
}

So - you have a value. You want to create another consistent value from it? Assuming that they are all going to be jpg files and that they are all in the current folder then don't you just want to do this:

 

Assuming that your query produced $results containing a field named 'number':

$output_html = '';
while ($row = $results->fetch())
{
$srcname = $row['number'] . '.jpg';
$alt = $row['number'];
$output_html .= "<img src='$srcname' alt='$alt'>";

 

Now echo out $output_html where you want it.

 

I'll let you fit this into your scheme.

  • Solution

Here is the result:

public function odometer($carID)
    {
        $mileage = str_split($this->pullMileageLastRecord($carID));
        $output = "";
        foreach ($mileage as $mile)
        {
            $output .= "<img src='img/speedo-numbers/$mile.gif' alt='$mile'>";
        }
        return $output;
    }

img2.png

@ginerjm, the OP will not have an image for every possible number value. E.g. there will be no "525.jpg" for the number 525. So, the correct process is to iterate over each character in the numeric string.

 

@Jacques1, you are correct. That approach removes the necessity for splitting the string into an actual array. It's never seemed logical to me to reference characters in a string as if it was an array, so that did not come to mind when providing a solution.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.