igorek24 Posted July 22, 2016 Share Posted July 22, 2016 How can I display an image for a number? For example, if I have a number 185 that I pulle from DB, I would like to rendered as html as: <img src="1.jpg" alt="1"> <img src="8.jpg" alt="8"> <img src="5.jpg" alt="5"> Any help would be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/301546-how-to-display-an-image-for-each-number/ Share on other sites More sharing options...
Psycho Posted July 22, 2016 Share Posted July 22, 2016 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; } Quote Link to comment https://forums.phpfreaks.com/topic/301546-how-to-display-an-image-for-each-number/#findComment-1534844 Share on other sites More sharing options...
ginerjm Posted July 22, 2016 Share Posted July 22, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/301546-how-to-display-an-image-for-each-number/#findComment-1534845 Share on other sites More sharing options...
igorek24 Posted July 22, 2016 Author Share Posted July 22, 2016 str_split is what I was looking for. Thank you so much everyone! Quote Link to comment https://forums.phpfreaks.com/topic/301546-how-to-display-an-image-for-each-number/#findComment-1534850 Share on other sites More sharing options...
ginerjm Posted July 22, 2016 Share Posted July 22, 2016 If you say so.... Quote Link to comment https://forums.phpfreaks.com/topic/301546-how-to-display-an-image-for-each-number/#findComment-1534851 Share on other sites More sharing options...
Solution igorek24 Posted July 22, 2016 Author Solution Share Posted July 22, 2016 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; } Quote Link to comment https://forums.phpfreaks.com/topic/301546-how-to-display-an-image-for-each-number/#findComment-1534855 Share on other sites More sharing options...
Jacques1 Posted July 22, 2016 Share Posted July 22, 2016 None of this requires str_split(). Just iterate over the characters: <?php $input = '1234'; for ($i = 0; $i < strlen($input); $i++) { $char = $input[$i]; var_dump($char); } Quote Link to comment https://forums.phpfreaks.com/topic/301546-how-to-display-an-image-for-each-number/#findComment-1534858 Share on other sites More sharing options...
Psycho Posted July 22, 2016 Share Posted July 22, 2016 @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. Quote Link to comment https://forums.phpfreaks.com/topic/301546-how-to-display-an-image-for-each-number/#findComment-1534864 Share on other sites More sharing options...
cyberRobot Posted July 22, 2016 Share Posted July 22, 2016 Depending where the mileage comes from, you will want to make sure you are dealing with numbers...as suggested by Psycho in Reply #2. The ctype_digit() function can help with that. http://php.net/manual/en/function.ctype-digit.php Quote Link to comment https://forums.phpfreaks.com/topic/301546-how-to-display-an-image-for-each-number/#findComment-1534865 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.