我想从数据库中提取行,将值附加到这些行,然后根据值对这些行进行排序。问题是,在尝试通过while循环执行此操作时,我收到错误:
“解析错误:语法错误,意外'=>' (T_DOUBLE_ARROW)in 第98行的C:\ xampp \ htdocs \ productivitysuite \ index.php“
我的目标是从数据库中检索行并根据我在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
?>
答案 0 :(得分:1)
这是您的子阵列声明的语法错误。
更改
$results[] = ($row => $priority);
要
$results[] = array($row => $priority);
更正:$row
是一个数组,不能用作键。
我想你可能意味着:
$results[]=array($row['taskName']=>$priority);
或者也许:
$results[]=array($priority=>$row);
取决于您的首选结构。
顺便说一下,我会用查询数组替换你的taskType
条件块,这是一个干净/更紧凑的代码,我相信它更易于维护和阅读。