[英]Add values from mysqli_fetch_array
我是這是我學校的第一個編碼項目。 我有一個任務要檢查,兩個條件是否成立,如果兩個條件成立,我必須計算發生的次數。 我現在真的很困,我會很感激每一個有用的答案。
因此,這是代碼的一部分:
$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。如果是,則計算這是正確的次數。 感謝幫助。 謝謝!
所以,幾件事。
您將要在for循環中將<=
更改為<
,否則將使索引超出綁定異常。 請參閱是否應在for循環中使用<或<=
您可以使用MySql輕松解決此問題,很遺憾,您尚未共享SQL查詢,因此我無法為您編寫此代碼。 但這是某種形式的group by
。
我注意到,與$zeil[$j]['name']
相比,您正在做$zeil['name'][$j]
很多事情,這對我來說很奇怪,因為這意味着您有一個數組而不是包含附加屬性的“對象”(某種)數組。 這不是常見的做法,但是我仍將嘗試使用它。
假設您的數組如下所示:
$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 ], . . . ];
數據結構的構建方式要求您為兩個子數組擁有相同數量的索引。 如果您忘記這樣做,最終可能會導致不必要的錯誤。
遍歷結果時,您希望將當前行與最后處理的行進行比較。
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>";
}
}
上面的代碼僅對行進行一次迭代,但始終將最后讀取的行與當前的行進行比較。 它不會檢查前幾行(僅檢查前一行)。 因此,這可能不是您想要的理想解決方案。 更好的選擇可能是在MySQL
SELECT
使用group by
選項。 您可能需要閱讀以下內容: Mysql Tutorials-Group by
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.