[英]Group database records and echo group name and records of it
I'm using PHP and MySQL Currently i have database structure like this 我正在使用PHP和MySQL,目前我具有这样的数据库结构
EventID | MatchName | Tournament | Results
1234 | match1 | Tour1 | 1-0
5678 | match2 | Tour2 | 0-1
7890 | match3 | Tour1 | 1-2
I need to group them by Tournament and get each tournament records like this 我需要按比赛分组,并获得每个比赛记录
<thead>
<th>Tour1</th>
</thead>
<tr>
<td>1234</td><td>match1</td><td>Tour1</td><td>1-0</td>
<td>7890</td><td>match3</td><td>Tour1</td><td>1-2</td>
</tr>
<thead>
<th>Tour2</th>
</thead>
<tr>
<td>5678</td><td>match2</td><td>Tour2</td><td>0-1</td>
</tr>
I know that there are more easier ways to do that you want, however, with the following library you will be able to do that you want and much more: 我知道,您可以通过更简单的方法来执行所需的操作,但是,使用以下库,您将可以执行所需的操作,甚至更多:
In the following example you will see how to do that you want: 在以下示例中,您将看到所需的操作:
use FunctionalPHP\common\Object;
use FunctionalPHP\common\functional\Collectors;
use FunctionalPHP\iterable\collection\lists\ArrayList;
class Event extends Object {
protected $eventId;
protected $matchName;
protected $tournament;
protected $results;
public function __construct (int $eventId, string $matchName
,string $tournament, string $results) {
$this->eventId = $eventId;
$this->matchName = $matchName;
$this->tournament = $tournament;
$this->results = $results;
}
}
$arrayListOfEvents = new ArrayList();
$arrayListOfEvents->add (new Event (1234, 'match1', 'Tour1', '1-0'));
$arrayListOfEvents->add (new Event (5678, 'match2', 'Tour2', '0-1'));
$arrayListOfEvents->add (new Event (7890, 'match3', 'Tour1', '1-2'));
$hashMap = $arrayListOfEvents->stream()
->collect (Collectors::groupingBy (function (Event $event) : string {
return $event->tournament;
}));
And you will be able to access to the stored elements with: 您将可以通过以下方式访问存储的元素:
foreach ($hashMap->iterator() as $internalKey => $internalValue) {
echo "\nKey of current element of Map: ", $internalKey;
foreach ($internalValue->getIterable()->iterator() as $value) {
echo "\n\tValue of current element of Map: ", $value->eventId
, ", " , $value->matchName, ", ", $value->tournament
, ", ", $value->results;
}
}
You will get the following: 您将获得以下内容:
Key of current element of Map: Tour1
Value of current element of Map: 1234, match1, Tour1, 1-0
Value of current element of Map: 7890, match3, Tour1, 1-2
Key of current element of Map: Tour2
Value of current element of Map: 5678, match2, Tour2, 0-1
Now you only need to add the required HTML tags. 现在,您只需要添加所需的HTML标签。
You will be able to access to mysql database using the following functions: 您将可以使用以下功能访问mysql数据库:
mysqli_connect
mysqli_fetch_array
See the example #2 of the following link: 请参阅以下链接的示例#2:
Get results from mysql database 从mysql数据库获取结果
The final code could be something like this: 最终的代码可能是这样的:
$link = mysqli_connect ("my_host", "my_user", "my_password", "my_database");
$results = mysqli_query ($link, "select * from events order by Tournament");
$currentTournament = "";
while ($row = mysqli_fetch_array ($results, MYSQLI_ASSOC)) {
if (strcmp ($currentTournament, $row["Tournament"]) != 0) {
$currentTournament = $row["Tournament"];
echo "\n<thead><th>".$currentTournament."</th></thead>";
}
echo "\n<tr><td>".$row["EventID"]."</td><td>"
.$row["MatchName"]."</td><td>"
.$row["Tournament"]."</td><td>"
.$row["Results"]."</td></tr>";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.