How to form this result so that I can pad 0 to id

@fretburner
I have this working code

            $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 this is the result

{“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 I want my result id to have padded 0, how can I achieve to have this output with padded 0

{"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"}]}

I know how to put padded 0 if I will use the while loop

str_pad(((int)$row[‘id’]),5,“0”,STR_PAD_LEFT);

but I cannot output same format above.

best is to do that in SQL.

for MySQL that would be LPAD(id, 5, '0') AS id

1 Like

Thank you it works.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.