mellis95 Posted January 14, 2010 Share Posted January 14, 2010 I have a problem with PHP seemingly dropping leading zeros from my variables... I am displaying ICD9 codes, which sometimes have leading zeroes, sometimes have two decimal places, sometimes just one decimal place, which is why I decided that storing as char() would probably work the best for this. The data is storing exactly as I need it in the table, but when I display it on a page, it drops the leading zeros. Here is what I have I have the following result in a mysql query: mysql> select code, description from tbl_icd9 WHERE code like '%078%'; +--------+--------------+ | code | description | +--------+--------------+ | 078.12 | PLANTAR WART | +--------+--------------+ 1 row in set (0.00 sec) I have the following on my display page: $query = "SELECT code, description FROM tbl_icd9 where code like %$var% order by code"; The value of $var is "078" , so the query is exactly the same as what I did on the command line. However the following outputs $code as "78.12" instead of "078.12". $numresults= mysql_query($query); $numrows=mysql_num_rows($numresults); // next determine if s has been passed to script, if not use 0 if (empty($s)) { $s=0; } print "\n<table>\n<tr>\n" . "\n\t<th></th>" . "\n\t<th>Code</th>" . "\n\t<th>Description</th>" . "\n</tr>"; // get results $query .= " limit $s,$limit"; $result=mysql_query($query); $code_array=mysql_fetch_array($result); $num=mysql_numrows($result); $i=0; while ($i < $num) { $code=mysql_result($result,$i,"code"); $description=mysql_result($result,$i,"description"); print "\n<tr>"; print "<td><a href=\"../poc/use_code.php?pocid=$eid&code=$code_array["code"]\">Select</a></td> <td>$code_array[code]</td> <td>$description</td>"; print "\n</tr>"; $i++; } print "\n</table>\n"; I can't find anything that should be trimming zeros or anything like that. What should I be looking for here? Thanks for the help. Matt[/code] Quote Link to comment Share on other sites More sharing options...
taquitosensei Posted January 14, 2010 Share Posted January 14, 2010 sprintf echo sprintf("%02d",$code_array['code']); should do it Quote Link to comment 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.