【问题标题】:Compare array values in php and update table比较php中的数组值并更新表
【发布时间】:2017-06-20 16:15:18
【问题描述】:

我在 php $resultA 和 $resultB 中有两个来自 SQL 选择语句的数组。该数组是用 mysqli_fetch_array(MYSQLI_ASSOC) 构建的。我想比较每个科目的成绩,或者得到提高、降低或保持不变的结果。比较数组中的这些值并将结果添加到数组末尾的新字段(即 ReadingPerformance、MathPerformance、SciencePerformance 和 AttendancePerformance)的最佳做法是什么,以便可以访问它们以显示在表格中

Array ( [mID] => 1 [mFirst] => Mike [mLast] => Davis [mNumber] => 123456789 [mSchool] => SSAS [mGrade] => 8 [gID] => 0 [gYear] => 2017 [gReading] => A [gMath] => A [gScience] => B [gSemester] => Q1 [gDaysAbsent] => 1 ) 
Array ( [mID] => 1 [mFirst] => Mike [mLast] => Davis [mNumber] => 123456789 [mSchool] => SSAS [mGrade] => 8 [gID] => 1 [gYear] => 2017 [gReading] => B [gMath] => B [gScience] => A [gSemester] => Q2 [gDaysAbsent] => 4 )

最终表格标题

【问题讨论】:

    标签: php arrays html mysqli


    【解决方案1】:

    注意:mysqli_fetch_array() 在 PHP7 中已弃用,请考虑将 mysqli 类与 fetch_assoc() 一起使用

    我假设您已经订购了您的查询,可能是通过 mID,这将使这种方式更容易。 我的建议是创建一个数组,在其中收集数据,然后循环遍历它以创建表(我将按顺序将其写出,但您应该考虑将其分割成类方法)。注意:这是未经测试的。

    // execute the query
    $result = mysqli_query($handler, $query);
    
    // create a stack to collect the data
    $data = array();
    
    // the table header, will also be used to store values in order
    $metaData = array(
        'mID' => 'MemberID(system)',
        'mFirst' => 'First Name',
        // other meta data (make sure you use the field names)...
    );
    
    $grades = array(
        'gReading' => 'Reading',
        'gMath' => 'Math',
        'gScience' => 'Science',
        'gDaysAbsent' => 'Absent'
    );
    
    // create a table header
    $tableHeader = $metaData;
    // create 3 fields for each subject
    foreach ($grades as $fieldname => $headline) {
        $tableHeader[$fieldname . 'Q2'] = $headline . ' Q2';
        $tableHeader[$fieldname . 'Q1'] = $headline . ' Q1';
        $tableHeader[$fieldname . 'Performance'] = $headline . ' Performance';
    }
    
    // read each row of the result
    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
        // we haven't handled this user before
        if (!array_key_exists($row['mID'], $data)) {
            // create a row with the meta data
            $tableRow = array();
            foreach ($metaData as $fieldname => $headline) {
                $tableRow[$fieldname] = $row[$fieldname];
            }
        } else {
            // fetch this users previous data
            $tableRow = $data[$row['mID']];
        }
        // populate the grades for this quarter and calculate the performance
        foreach ($grades as $gradename => $headline) {
            // f.ex. gReadingQ1
            $tableRow[$gradename . $row['gSemester']] = $row[$gradename];
            // calculate the performance
            if ($tableRow[$gradename . 'Q1'] && $tableRow[$gradename . 'Q2']) {
                // TODO: do some magic here
                // f.ex. calculate the number of the letter in the alphabet and get the average
                $tableRow[$gradename . 'Performance'] = 'SOMETHING';
            }
        }
        // store the row
        $data[$row['mID']] = $tableRow;
    }
    
    // TODO: create the table in whatever format you want
    print_r($data);
    

    【讨论】:

    • 涵盖了大部分内容,但我有来自两个包含数据的查询的两个数组。对每个数组执行此操作,然后将两者处理到第三个数组?
    • 为什么会有两个查询?结果看起来像是来自同一张表。如果是这种情况,您应该使您的查询更通用(例如semester IN ("Q1", "Q2") 以避免过于频繁地访问数据库。如果您确实需要执行两个查询,只需将结果放在一个数组中,而不是 while 循环获取行对结果堆栈执行 foreach。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多