douqianmin5367 2018-06-13 19:27
浏览 101

如何格式化sql值“[a,b,c]”?

On a game, there are three different teams. For example, team a, b, and c. On the game's database the playtime is reported like this: "[a,b,c]" and each letter is how many minutes the user has played on that specific team. I used PHP to grab the data from the database to display on a website, but it is just displaying as "[14,0,1]" as I have 14 minutes on team a, 0 on team b, and 1 on team c.

How can I format this, through PHP or JSON, to add a, b, and c, or in this case 14+0+1?

The code in PHP:

$sql = "SELECT * FROM players WHERE playerid ='". $steamprofile['steamid']  
."'";
$result = $db->query($sql);
if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        $playtime = $row["playtime"];
    }

The code in HTML:

<span id="label">Playtime</span>: <b><span id="playerPlaytime"><?php echo 
$playtime; ?></span></b><br>

Output: http://prntscr.com/jum85v

  • 写回答

2条回答 默认 最新

  • dongying6896 2018-06-13 19:37
    关注

    Create an array by splitting on commas and then calculate the sum (trim extra stuff first):

    $playtime = array_sum(explode(',', trim($row['playtime'], '"[]')));
    

    To use JSON you still need to trim the quotes:

    $playtime = array_sum(json_decode(trim($row['playtime'], '"')));
    
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀