0

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

?>
1
  • Do you mean to print the unsorted resultset rows to screen AND generate a new results array to be sorted? Commented Jan 22, 2018 at 4:38

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I haven't had a proper look at the full code, but given enough careful dev time perhaps the entire preparation process could be accomplished in sql.
Is it preferable to use SQL to php?
Sql is very good at sorting data. The query might end up getting a bit complex, maybe not (I'm not at my computer right now so I can't take a careful look. If you mock up an sqlfiddle demo with some relevsnt sample data and post your code to CodeReview and ask if it can be accomplished with pure sql, you may get a response. (Of course always try, yourself, before asking for help and be very precise about the expected output.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.