camelCaseCowboy
camelCaseCowboy

Reputation: 986

PHP trying to add values to an array in a while loop

I want to pull rows from the database, attach a value to those rows, then order those rows based on a value. The problem is that in trying to do this via the while loop I get an error:

"Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW) in C:\xampp\htdocs\productivitysuite\index.php on line 98"

My goal is to retrieve rows from a database and organize those rows based on a value I calculate in PHP php ($priority), then display certain values of those rows based on their priority. The problem is that I get this darn error and I don't know why, nor do I know where to look for help to resolve it! Hopefully SO can help diagnose this.

code:

<?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

?>

Upvotes: 0

Views: 148

Answers (1)

mickmackusa
mickmackusa

Reputation: 47864

This is a syntax error with your subarray declaration.

Change

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

To

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

Correction: $row is an array, that cannot be used as a key.

I think you might mean:

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

Or maybe:

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

Depending on your preferred structure.

As an aside, I would replace your taskType conditional block with a lookup array as a matter of clean / more compact code which I believe is easier to maintain and read.

Upvotes: 1

Related Questions