繁体   English   中英

PHP尝试在while循环中向数组添加值

[英]PHP trying to add values to an array in a while loop

我想从数据库中提取行,将值附加到这些行,然后根据值对这些行进行排序。 问题是,尝试通过while循环执行此操作时出现错误:

“解析错误:语法错误,在第98行的C:\\ xampp \\ htdocs \\ productivitysuite \\ index.php中出现意外的'=>'(T_DOUBLE_ARROW)”

我的目标是从数据库中检索行并根据我在PHP php( $priority )中计算出的值来组织这些行,然后根据其优先级显示这些行的某些值。 问题是我得到了该死的错误,我不知道为什么,也不知道在哪里寻求帮助来解决它! 希望SO可以帮助诊断这一点。

码:

<?php

$mysqli = mysqli_connect('127.0.0.1','root','', 'taskdb'); //open connection
//retrieve variables
$sql = "SELECT * FROM tasklist WHERE completion_flag = FALSE"; 
$sqldata = mysqli_query($mysqli,$sql) or die('error retrieving data');
//Display results
echo "<table>";
echo "<tr><th>Task</th><th>Type</th><th>Due Date</th>";
$counter = 0;  //may need to make global, not sure
$results = array();

while($row = mysqli_fetch_array($sqldata, MYSQLI_ASSOC)){
    //store each row as variables
    if ($row['externalFlag'] == 1) {
        $E = 1;
    } else{
        $E = 0.5;
    }
    //days until completion
    $nowDate = date("m/d/Y");
    $D = 1 / (date_diff($row['dueDate']- $nowDate));

    //Activity category multiplier
    if($row['taskType'] == "Daily"){
        $C = 0.5;
    } elseif($row['taskType'] == "Maintenance"){
        $C =  0.2;
    } elseif ($row['taskType'] == "School_Study") {
        $C = 0.75; 
    } elseif ($row['taskType'] == "Personal_Project") {
        $C = 0.80;
    } elseif ($row['taskType'] == "Work_Project") {
        $C = 0.65;
    }

    $U = ($C - (1 / $D))* $E;  //urgency rating; SET PER ROW!

    $I =  $row['importance']; //Importance rating

    $priority = $U + $I;

    $results[] = ($row => $priority);
    //The array 

    $priorityOutput = $priorityRow.$counter;

    ++$counter;

    echo "<tr><td>";
    echo $row['taskName'];
    echo "</td><td>";
    echo $row['taskType'];
    echo "</td><td>";
    echo $row['dueDate'];
    echo "</td></tr>";

    //Make the below its own loop where I output in order of decreasing UI value

}
//now we have an array of key => value pairs with rows tied to a priority.
//We need to organize the rows by the priority

arsort($results);

echo "</table>"

$mysqli->close(); //close connection

?>

这是子数组声明的语法错误。

更改

 $results[] = ($row => $priority); 

 $results[] = array($row => $priority); 

更正: $row是一个数组,不能用作键。

我想你可能是说:

$results[]=array($priority=>$row);

或者可能:

 $results[]=array($priority=>$row); 

取决于您的首选结构。

taskType ,我将使用干净的/更紧凑的代码将您的taskType条件块替换为查找数组,我认为它更易于维护和阅读。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM