dongliqin6939 2018-12-21 14:36
浏览 97

我可以存储该函数的字符串结果并将其连接到另一个字符串吗?

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%)
  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 js调用html页面需要隐藏某个按钮
    • ¥15 ads仿真结果在圆图上是怎么读数的
    • ¥20 Cotex M3的调试和程序执行方式是什么样的?
    • ¥20 java项目连接sqlserver时报ssl相关错误
    • ¥15 一道python难题3
    • ¥15 牛顿斯科特系数表表示
    • ¥15 arduino 步进电机
    • ¥20 程序进入HardFault_Handler
    • ¥15 oracle集群安装出bug
    • ¥15 关于#python#的问题:自动化测试