gpt4 book ai didi

php - 来自 value API 的 HTML 星级

转载 作者:可可西里 更新时间:2023-11-01 00:54:33 25 4
gpt4 key购买 nike

有一个挑战,自从我使用 HTML 和 PHP 以来已经很久了。

从第 3 方客户反馈工具,他们提供了一个 API。幸福值很容易从 API 中提取,并以百分比显示。仅此而已..

<?php include('https://app.customerthermometer.com/api.php?apiKey=********************&getMethod=getHappinessValue'); ?>

我想知道并尝试通过将 PHP 包含到文档中来实现这一点,并制作一个更加丰富多彩和可见的 View 。例如;

http://jsfiddle.net/b6tqtaLn/1/

Star-rating with css

将百分比(例如来自 API 的 95%)转换为在星标中可见的值的最佳方法是什么?

提前致谢!

最佳答案

首先,您需要定义将使用多少(最大)星星。

例如,如果您从 API 收到的最大值是 100%,并且您使用的是 10 颗星,那么这很容易做到。

示例:

$api_percentage_value = '95%'; // Given from API
$converted_percentage_value = (int) $api_percentage_value; // Make it an integer

echo $converted_percentage_value; // outcome = 95 (integer)

/**
* Personally I would like to have the 'raw' data to be in a string like above.
* But if you prefer to have it in a single string simply delete the `$api_percentage_value` and drop the API data inside `$converted_percentage_value` right away.
* But remember you need to add `(int)` before the result. Otherwise it will not be an integer.
* Also, incase they do deliver only an integer, it might work for some time.
* But we all know many API developers just change stuff within their output without communication (before and/or after the change).
* So I prefer to have it split up and make my own validation.
*/

所以,现在我们有一个整数。

假设 1 星 = 10 分。

这背后的计算是:

95 / 100 = 0,95. * 10. = 9.5 (stars) // Incase 1 star = 20 points: simply do 95 / 200, etc.

-或-

$stars = ($converted_percentage_value / 100) * 10;

echo $stars; // outcome: (int) 9.5

因此,现在您可以显示 9.5(或 9)星 :-)。

$score = floor($converted_percentage_value); // outcome: 9.

现在你可以做一个foreach,每次减去一个$score ($score--)。一旦 $score 低于 (int) 1,您就可以停止脚本。

您知道如何执行此操作吗?或者您是否希望获得一个在带有倒计时的 foreach 中显示星星的示例代码?

关于php - 来自 value API 的 HTML 星级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51781527/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com