-2

I'm trying to get my head around this below code, being relatively new to php/mysql its a bit of a head scratcher, the part i'm trying to work out is the progress/percentage part.

$prog = $tprog > 0 ? ($cprog/$tprog) * 100 : 0;

$prog = $prog > 0 ?  number_format($prog,2) : $prog;

happy if someone can explain it?

 <?php
            $i = 1;
            $stat = array("Started (Received by Client)","In-Progress (with Estimator)","Complete (Handed to Delivery)");
            $where = "";
            if($_SESSION['login_type'] == 2){
              $where = " where manager_id = '{$_SESSION['login_id']}' ";
            }elseif($_SESSION['login_type'] == 3){
              $where = " where concat('[',REPLACE(user_ids,',','],['),']') LIKE '%[{$_SESSION['login_id']}]%' ";
            }
            $qry = $conn->query("SELECT * FROM project_list $where order by name asc");
            while($row= $qry->fetch_assoc()):
              $prog= 0;
            
            $tprog = $conn->query("SELECT * FROM task_list where project_id = {$row['id']}")->num_rows;
            $cprog = $conn->query("SELECT * FROM task_list where project_id = {$row['id']} and status = 3")->num_rows;
            $prog = $tprog > 0 ? ($cprog/$tprog) * 100 : 0;
            $prog = $prog > 0 ?  number_format($prog,2) : $prog;
            $prod = $conn->query("SELECT * FROM user_productivity where project_id = {$row['id']}")->num_rows;
            if($row['status'] == 0 && strtotime(date('Y-m-d')) >= strtotime($row['start_date'])):
            if($prod  > 0  || $cprog > 0)
              $row['status'] = 2;
            else
              $row['status'] = 1;
            elseif($row['status'] == 0 && strtotime(date('Y-m-d')) > strtotime($row['end_date'])):
            $row['status'] = 4;
            endif;
              ?>
DarkBee
  • 16,592
  • 6
  • 46
  • 58
portfox
  • 1
  • 2
  • 2
    Check the section **"?: Ternary Operator"** [Reference Guide: What does this symbol mean in PHP? (PHP Syntax)](https://stackoverflow.com/questions/3737139/reference-guide-what-does-this-symbol-mean-in-php-php-syntax) – shingo Apr 20 '23 at 04:59

1 Answers1

0
  1. set up array of project statuses $stat with three elements: "Started (Received by Client)", "In-Progress (with Estimator)" and "Complete (Handed to Delivery)"

  2. set up variable $where based on user's login type(manager -- the query will filter by their ID -- team member -- filter by their ID in user_ids field of project_list table)

  3. execute SQL query to select all the projects from the project_list table based on $where filter

  4. loop through each project returned by query + calculate project progress %

(calculate progress percentage -- 1 get total number of tasks for the project $tprog + # of tasks that have been completed $cprog -- if no tasks for the project -- percentage set to 0 else calculate (completed tasks / total tasks) * 100)

  1. format percentage to two decimal places using number_format

  2. check project status based on start and end dates + any tasks have been completed

  3. output project name + progress percentage + status in HTML table row

Lemonina
  • 712
  • 2
  • 14