Jump to content

Converting Php Script Into A Mysql Query - Solved With Our Thanks In The Last Post


niche

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...