I have a table with column name = recording_size.
In this column I am storing the size in bytes,
but on the Web I am showing the size in KB.
public static function getKbFromBytes($string){
if($string){
return ($string/1024);
}
}
Now I have a Filtration Functionality in Web. So as I am showing size in KB so I’ll certainly take Input for Searching from user in KB & not in bytes although in DB I have that in bytes. For that I take input in KB and than convert it again to bytes like:
public static function getBytesFromKb($string){
if($string){
return ($string * 1024);
}
}
Example:
size in Bytes = 127232
When I apply my function so it = 124.25 KB
now when user write exactly like 124.25 then the search works,
but I want that user does not write exactly the same.
The user can also write 124 instead of 124.54, and when the user writes 124 then my search is not working — meaning it does not show any records.
I have also ADD & Subtract 50 from the converted bytes but it is not working.
$sql = $sql->where('r.recording_size BETWEEN "'.(Engine::getBytesFromKb($opt['sn']) - 50) .'" AND "'.(Engine::getBytesFromKb($opt['sn']) + 50) .'"');
How can I achieve this?
Searching for size should probably be as a range instead of an equality anyway. At least, the default should be the range, unless your app’s primary focus is the exact size. For the SQL:
By the way, you should omit the quotation marks (
"). They are invalid/nonstandard even for strings. And you are comparingint‘s.Another thing to watch for is KiB vs KB.