I'm reading an array from db and i want to format that to provide a table like text for my telegram bot.
I try to use the sprintf php funtion to format the text, but when i try to put the result into a string the result is that any string was putted in the string.
This is my array:
// This is my complete array.
{
"result":true,
"error":"nothing",
"content":{
"3269":{"Quantity":4,"Name":"Atom Seikhart"},
"3273":{"Quantity":4,"Name":"Brunhild"},
"3304":{"Quantity":3,"Name":"Isis"},
"3319":{"Quantity":1,"Name":"Arthur"},
"3329":{"Quantity":2,"Name":"Loki"},
"3343":{"Quantity":1,"Name":"Chamimi"},
"3351":{"Quantity":14,"Name":"Hanzo"},
"3379":{"Quantity":8,"Name":"Lucifer"}
},
"Total":37
}
And this is my php cycle and message send:
// This is my "select from db" function.
$res = get_event_rulers_breakdowns_by_id($mysqli, $value["Id"]);
$breakdown = $res["content"];
$message .= "
This is the event ruler breakdown (total " . $res["Total"] . "):
";
foreach ($breakdown as $value) {
$perc = round($value["Quantity"] / $res["Total"] * 100, 2);
$stringa = "%+4s:%+4d\t(<code>%+4g%</code>)
";
// This work but don't format the text.
$message .= $value["Name"] . ":\t" . $value["Quantity"] . " (<code>" . $perc . "%</code>)
";
// This dosen't work.
$formatt = sprintf("%+4s:%+4d (<code>%+4g%</code>)", $value["Name"], $value["Quantity"], $perc);
// This dosen't work too.
$formatt = sprintf("%+4s:%+4d (<code>%+4g%</code>)", "aa", 123, 12.3);
$message .= $formatt . "
";
}
The actual result is:
And the expected result will be like:
Atom Seikhart: 4 (10.81%)
Brunhild: 4 (10.81%)
Isis: 3 (8.11%)
Arthur: 1 (2.7%)
Loki: 2 (5.41%)
Chamimi: 1 (2.7%)
Hanzo: 14 (37.84%)
Lucifer: 8 (21.62%)
or like:
Atom Seikhart: 4 (10.81%)
Brunhild: 4 (10.81%)
Isis: 3 (8.11%)
Arthur: 1 (2.7%)
Loki: 2 (5.41%)
Chamimi: 1 (2.7%)
Hanzo: 14 (37.84%)
Lucifer: 8 (21.62%)