1

我是我在学校的第一个编码项目。我有一个任务要检查,两个条件是否为真,如果是,我必须计算出现次数。我现在真的很困惑,我会很感激每一个有用的答案。

所以这是代码的一部分:

$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。如果是,请计算这是正确的次数。帮助表示赞赏。谢谢!

4

2 回答 2

0

所以,有几件事。

  1. 您将要在 for 循环中更改<=<,否则您将获得索引超出范围的异常。请参阅是否应该在 for 循环中使用 < 或 <=

  2. 你可以使用 MySql 更轻松地解决这个问题,不幸的是你没有共享你的 SQL 查询,所以我不能为你写这个。但这将是某种形式的group by.

  3. 我注意到与 相比,您做$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

请记住,您的初始方法存在一些错误。

  1. 您实际上并没有跟踪事件,因为每次迭代也会将所有先前的事件添加到$occurrenceCount变量中。

  2. 您使用的数据结构对于正确跟踪出现次数并不是特别友好。为值存储子数组是不正常的,更常见的方法是:

    $zeil = [
        [
            'name' => 'nathan',
            'positionOrder' => 4
        ],
        [
            'name' => 'bob',
            'positionOrder' => 10
        ],
    
        . . .
    ];
    
  3. 构建数据结构的方式要求您为两个子数组拥有相同数量的索引。如果您忘记这样做,最终可能会导致不必要的错误。

于 2018-12-16T16:48:29.267 回答
-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 byMySQL SELECT. 您可能想阅读以下内容:Mysql Tutorials - Group by

于 2018-12-16T16:05:21.643 回答