php - Pad data with zeros on the server-side -
i using jquery datatables. , have code working properly:
$res = $this->connection->prepare("...."); $res->bindparam(1,$id); $res->execute(); $result = $res->fetchall(pdo::fetch_assoc); $data = array( 'data'=>$result ); return json_encode($data); and how result looks like.
{"data":[{"id":"10","time_in":"2015-07-06 07:30:00","time_out":"2015-07-06 10:26:11" ,"attended":"yes"},{"id":"9","time_in":"2015-07-06 08:00:00","time_out":"2015-07-06 11:46:36","attended":"yes"}]}
but want id 0 padded shown below.
{"data":[{"id":"00010","time_in":"2015-07-06 07:30:00","time_out":"2015-07-06 10:26:11" ,"attended":"yes"},{"id":"00009","time_in":"2015-07-06 08:00:00","time_out":"2015-07-06 11:46:36","attended":"yes"}]}
how can achieve can output result , can display in data table?
mysql
in mysql can use lpad().
for example, pad field id zeros 5 characters, use statement below.
select lpad(id,5,'0') id table; postgresql
in postgresql can use lpad(). if id integer, need type-casted string.
for example, pad field id zeros 5 characters, use statement below.
select lpad(id::text,5,'0') id table;
Comments
Post a Comment