niche Posted October 8, 2011 Share Posted October 8, 2011 Is it possible to turn this php script into a mysql UPDATE query (minus the echo)? Where a row would be updated with the value in $total and the intial value for $str comes from the table. <?php$str = 12345678901;$total = 0;while (strlen($str) > 0) { $total = $total + substr($str,0,1); $str = substr($str,1);} $total = 10 - fmod($total,10);echo $total . '</br>';?> Link to comment Share on other sites More sharing options...
Ingolme Posted October 8, 2011 Share Posted October 8, 2011 I suppose you're trying to sum all the numbers in $str, I'm not exactly sure why.Since you're doing string manipulation then you should put quotes around the string. Your code could be condensed to this: $str = '12345678901';$total = 10 - fmod(array_sum(str_split($str)), 10); I don't know if this is even possible in MySQL, but it would be pretty complicated. More than its worth, I'd say. Link to comment Share on other sites More sharing options...
niche Posted October 8, 2011 Author Share Posted October 8, 2011 Ingolme, thank-you for your help. That's a load off my mind. Also, thanks for str_split()! FYI, this is part of the work needed to calculate the check digit for a postnet barcode http://en.wikipedia.org/wiki/POSTNET Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.