这是我从学校开始的第一个编码项目。我有一个任务要检查,两个条件是否成立,如果两个条件成立,我必须计算发生的次数。我现在真的很困,我会很感激每一个有用的答案。
所以这是代码的一部分:
$verbindung = mysqli_connect($server, $user, $pass, $database)
or die ($meldung);
$driverID = $_POST['fahrer'];
$datum_von = $_POST['date_von'];
//Change Date Format
$datum_von_new = date_format(new DateTime($datum_von), 'Y-m-d');
$datum_bis = $_POST['date_bis'];
//Change Date Format
$datum_bis_new = date_format(new DateTime($datum_bis), 'Y-m-d');
$sql = "SELECT drivers.forename, drivers.surname, results.positionOrder, races.name, races.date ";
$sql.= "FROM drivers ";
$sql.= "INNER JOIN results ON drivers.driverId = results.driverId ";
$sql.= "INNER JOIN races ON results.raceId = races.raceId ";
$sql.= "WHERE drivers.driverId = '$driverID' ";
$sql.= "AND races.date BETWEEN '$datum_von_new' ";
$sql.= "AND '$datum_bis_new' ";
$sql.= "AND results.positionOrder <= 10;"
<table class="table table-striped">
<thead class="thead-dark">
<tr>
<th scope="col">Fahrername</th>
<th scope="col">Streckenname</th>
<th scope="col">Datum</th>
<th scope="col">Platzierung</th>
<th scope="col">Häufigkeit</th>
</tr>
</thead>
$ergebnis = mysqli_query($verbindung, $sql);
$countPosition = 0
if ($ergebnis != False) {
while($zeile = mysqli_fetch_array($ergebnis)) {
echo "<tr>";
echo "<td>" . $zeile['forename'] . ' ' . $zeile['surname'] . "</td>";
echo "<td>" . $zeile['name'] . "</td>";
echo "<td>" . $zeile['date'] . "</td>";
echo "<td>" . $zeile['positionOrder'] . "</td>";
for($i = 0; $i<=sizeof($zeile); $i++) {
for($j = 0; $j <= sizeof ($zeile); $j++) {
if($zeile['name'][$i] == $zeile['name'][$j] &&
$zeile['positionOrder'][$i] == $zeile['positionOrder'][$j]) {
$countPosition += 1;
echo "<td>" . $countPosition . "</td>";
}
}
}
echo "</tr>";
} else {
echo mysqli_error($verbindung);
}
$result = mysqli_close($verbindung);
所以我的目标是检查第1行中的名称是否等于第2行中的名称,以及第1行中的positionOrder等于第2行中的posisitionOrder。如果是,则计算这是正确的次数。 感谢帮助。谢谢!
答案 0 :(得分:0)
几件事。
您将要在for循环中将<=
更改为<
,否则将使索引超出绑定异常。参见Should one use < or <= in a for loop
您可以使用MySql轻松解决此问题,很遗憾,您尚未共享SQL查询,因此我无法为您编写此代码。但这可能是group by
的某种形式。
我注意到与$zeil['name'][$j]
相比,您在$zeil[$j]['name']
上做很多事情,这对我来说很奇怪,因为这意味着您拥有每个元素的数组,而不是具有附加属性的“对象”(某种)数组。这不是常见的做法,但我仍将尝试使用它。
让我们假设您的数组如下所示:
$zeil = [
'name' => [
'nathan',
'raverx1',
'someone',
'nathan',
'nathan',
'nathan',
'anyone',
'nathan',
'anyone',
],
'positionOrder' => [
4,
7,
9,
4,
4,
4,
7,
4,
7
]
];
您将需要以下代码来完成您的任务:
// Loop through your main array while tracking
// the current index in the variable $i.
// Since you're using sub arrays, we use them
// as the index. Which means the sub arrays
// have to be of identical size, else you will
// get an index out of bounds error.
for($i = 0; $i < sizeof($zeil['name']); $i++) {
// We set this to an initial value of 1 since
// it is the first occurrence.
$occurrenceCount = 1;
// Create local variables containing the current values.
$name = $zeil['name'][$i];
$positionOrder = $zeil['positionOrder'][$i];
// Loop through all previous entries to
// compare them to the current one.
for($j = $i-1; $j > -1; $j--) {
if (
$zeil['name'][$j] == $name &&
$zeil['positionOrder'][$j] == $positionOrder
) {
$occurrenceCount += 1;
}
}
// If multiple occurrences were found
// for this entry, output them.
if ($occurrenceCount > 1)
echo 'Occurrence Count ('.$name.') : '.$occurrenceCount.PHP_EOL;
}
输出如下:
Occurrence Count (nathan) : 2
Occurrence Count (nathan) : 3
Occurrence Count (nathan) : 4
Occurrence Count (nathan) : 5
Occurrence Count (anyone) : 2
一种更好的方法是将出现次数大于1的任何事物的出现次数存储在其自己的数组中,然后将其打印出来。
$multipleOccurrences = [];
// Loop through your main array while tracking
// the current index in the variable $i.
// Since you're using sub arrays, we use them
// as the index. Which means the sub arrays
// have to be of identical size, else you will
// get an index out of bounds error.
for($i = 0; $i < sizeof($zeil['name']); $i++) {
// We set this to an initial value of 1 since
// it is the first occurrence.
$occurrenceCount = 1;
// Create local variable containing the current values.
$name = $zeil['name'][$i];
$positionOrder = $zeil['positionOrder'][$i];
// Loop through all previous entries to
// compare them to the current one.
for($j = $i-1; $j > -1; $j--) {
if (
$zeil['name'][$j] == $name &&
$zeil['positionOrder'][$j] == $positionOrder
) {
$occurrenceCount += 1;
}
}
// If multiple occurrences were found
// for this entry, store them using $name as the key.
if ($occurrenceCount > 1)
$multipleOccurrences[$name] = $occurrenceCount;
}
// Print the occurrences.
echo 'All occurrences greater than \'1\''.PHP_EOL;
echo '--------------------------------'.PHP_EOL;
foreach ($multipleOccurrences as $name => $occurrenceCount) {
echo 'Occurrences ('.$name.') : '. $occurrenceCount . PHP_EOL;
}
输出看起来像这样:
All occurrences greater than '1'
--------------------------------
Occurrences (nathan) : 5
Occurrences (anyone) : 2
请记住,您的初始方法有些错误。
您实际上并没有跟踪事件的发生,因为每次迭代还将所有先前的事件也添加到$occurrenceCount
变量中。
您正在使用的数据结构对正确跟踪出现的次数不是特别友好。为值存储子数组是异常的,更常见的方式是:
$zeil = [
[
'name' => 'nathan',
'positionOrder' => 4
],
[
'name' => 'bob',
'positionOrder' => 10
],
. . .
];
构建数据结构的方式要求您为两个子数组拥有相同数量的索引。如果忘记了,最终可能会导致不必要的错误。
答案 1 :(得分:-1)
遍历结果时,您希望将当前行与最后处理的行进行比较。
for($i = 0; $i<=sizeof($zeile); $i++) {
if($i != 0 && $zeile['name'][$i] == $zeile['name'][$i - 1] &&
$zeile['positionOrder'][$i] == $zeile['positionOrder'][$i - 1]) {
$countPosition += 1;
echo "<td>" . $countPosition . "</td>";
}
}
上面的代码仅对行进行一次迭代,但始终将最后读取的行与当前行进行比较。它不会检查前几行(仅检查前一行)。因此,这可能不是您想要的理想解决方案。更好的选择可能是在group by
MySQL
中使用SELECT
选项。您可能需要阅读以下内容:Mysql Tutorials - Group by