Jump to content

While loop to isolate array items help


Plugnz13

Recommended Posts

Hi all,

I have a table that shows data from an xml response that has been converted to an array.

I have successfully managed to get that to work but it's showing more items than I want.

I'm trying to isolate the items by state. There are 11 or so different states, of which I only want 7. 

I've tried the following with a while loop but I keep getting the message "undefined index: job_status"  

Here's my code (partial)

$projects = array();    
    
        $xml=simplexml_load_string($response) or die("Error: Cannot create object");
     
    foreach($xml->Jobs->Job as $item) {
     
    $projects[] = array(
                     'job_no'          => (string)$item->ID,
                     'job_name'        => (string)$item->Name,
                     'job_due'         => (string)$item->DueDate,
                     'job_status'      => (string)$item->State,
                     'job_staff'       => (string)$item->Assigned->Staff->Name,
                    );
     }
     
 usort($projects, function($a,$b) {return $a['job_due'] <=> $b['job_due']; } );

$projects = array_reverse($projects);
     
while ($projects['job_status'] == ('Feasibility') 
          && ('Measure Up') 
          && ('Model Drawing') 
          && ('Concept Design') 
          && ('Developed Design') 
          && ('Resource Consent') 
          && ('Construction Documentation')) {
    
foreach ($projects as $proj) {
$formatted = date('d/m/Y', strtotime($proj['job_due'])); 
    $job_no ="{$proj['job_no']}";
    $job_name ="{$proj['job_name']}";
    $job_due ="$formatted";
    $job_status ="{$proj['job_status']}";
    $job_staff ="{$proj['job_staff']}";

Any help would be much appreciated thanks. 

Link to comment
Share on other sites

Your while() criteria is incorrect and unnecessary. I haven't tested this code, but give it a shot:

$acceptedStates = [
	'Feasibility',
	'Measure Up',
	'Model Drawing',
	'Concept Design',
	'Developed Design',
	'Resource Consent',
	'Construction Documentation',
];

$xml = simplexml_load_string($response);
$xml = usort($xml, function($a, $b){
	return $a['job_due'] <=> $b['job_due'];
});

foreach($xml->Jobs->Job as $item) {

	if(!in_array((string)$item->State, $acceptedStates)){
		continue;
	}

	$projects[] = [
			'job_no'          => (string)$item->ID,
			'job_name'        => (string)$item->Name,
			'job_due'         => date('d/m/Y', strtotime($item->DueDate)),
			'job_status'      => (string)$item->State,
			'job_staff'       => (string)$item->Assigned->Staff->Name,
	];

}

 

Link to comment
Share on other sites

Use an array to get the ones you want...

$projects = array();

$required = ['Feasibility', 'Measure Up', 'Model Drawing', 'Concept Design', 'Developed Design', 'Resource Consent', 'Construction Documentation' ];

$xml=simplexml_load_string($response) or die("Error: Cannot create object");
 
foreach($xml->Jobs->Job as $item) {
    if (in_array((string)$item->State, $required)) {
        $projects[] = array(
                         'job_no'          => (string)$item->ID,
                         'job_name'        => (string)$item->Name,
                         'job_due'         => (string)$item->DueDate,
                         'job_status'      => (string)$item->State,
                         'job_staff'       => (string)$item->Assigned->Staff->Name,
                        );
    }
}
     
 usort($projects, function($a,$b) {return $b['job_due'] <=> $a['job_due']; } );       // reverse $a and $b for desc sort

// $projects = array_reverse($projects);          // no longer required

This code is just plain wrong...

while ($projects['job_status'] == ('Feasibility') 
          && ('Measure Up') 
          && ('Model Drawing') 
          && ('Concept Design'                ) 
          && ('Developed Design'               ) 
          && ('Resource Consent'               ) 
          && ('Construction Documentation')) {

and this is a waste of time and effort. Why just move a value from one variable to another and why put a single string variable inside quotes?

$formatted = date('d/m/Y', strtotime($proj['job_due'])); 
    $job_no ="{$proj['job_no']}";
    $job_name ="{$proj['job_name']}";
    $job_due ="$formatted";
    $job_status ="{$proj['job_status']}";
    $job_staff ="{$proj['job_staff']}";

 

Link to comment
Share on other sites

If, for other purposes, you require all records to be in the array then you can filter the records when you process them to gat those with a required status value EG

$status_filter = function ($v) use ($required) { 
                    return in_array($v['job_status'], $required); 
                 };
                
foreach (array_filter($projects, $status_filter) as $proj) {
    // process $proj
}

 

Link to comment
Share on other sites

Hi again, sorry to be a pain but further to the previous question, I have the added requirement to be able to show the due date of a particular task but only if that task-name is the same as the current job state.

The xml code looks something like this 

<Job>
  <ID></ID>
  <Name>Job 1</Name>
  <State>Concept Design</State>
  <StartDate>2019-02-01</StartDate>
  <DueDate>2019-10-01</DueDate>
  <Tasks>
    <Task>
      <ID></ID>
      <Name>Measure Up</Name>
      <StartDate>2019-07-01</StartDate>
      <DueDate>2019-07-30</DueDate>
    </Task>
    <Task>
      <ID></ID>
      <Name>Concept Design</Name>
      <StartDate>2019-08-01</StartDate>
      <DueDate>2019-08-31</DueDate>
    </Task>
    ....etc

  my somewhat lame attempt looks like this

$projects = array();    

$required = ['Feasibility', 'Measure Up', 'Model Drawing', 'Concept Design', 'Developed Design', 'Resource Consent', 'Construction Documentation' ];

$xml=simplexml_load_string($response) or die("Error: Cannot create object");
     
foreach($xml->Jobs->Job as $item) {
    
    
if (in_array((string)$item->State, $required) ) { 
   
    $projects[] = array(
                     'job_no'          => (string)$item->ID,
                     'job_name'        => (string)$item->Name,
                     'job_due'        => date('d/m/Y', strtotime($item->Tasks->Task->DueDate)),
                     'job_status'      => (string)$item->State,
                     'task_name'       => (string)$item->Tasks->Task->Name,
                     'job_staff'       => (string)$item->Assigned->Staff->Name,
                    );
     }
}  
 usort($projects, function($a,$b) {return $b['job_due'] <=> $a['job_due']; } );
         
foreach ($projects as $proj) {
   
    $job_no =$proj['job_no'];
    $job_name =$proj['job_name'];
    $job_status =$proj['job_status'];
    $job_staff =$proj['job_staff'];
    if (in_array((string)$item->Tasks->Task->Name, $job_status)){
    $job_due =$proj['task_due'];
    }
    $task_name =$proj['task_name'];
    
 

Thanks again for your help. 

Edited by Plugnz13
Link to comment
Share on other sites

Given that xml extract

$required = ['Feasibility', 'Measure Up', 'Model Drawing', 'Concept Design', 'Developed Design', 'Resource Consent', 'Construction Documentation' ];
$str = <<<XML
<Jobs>
  <Job>
    <ID></ID>
    <Name>Job 1</Name>
    <State>Concept Design</State>
    <StartDate>2019-02-01</StartDate>
    <DueDate>2019-10-01</DueDate>
    <Tasks>
      <Task>
        <ID></ID>
        <Name>Measure Up</Name>
        <StartDate>2019-07-01</StartDate>
        <DueDate>2019-07-30</DueDate>
      </Task>
      <Task>
        <ID></ID>
        <Name>Concept Design</Name>
        <StartDate>2019-08-01</StartDate>
        <DueDate>2019-08-31</DueDate>
      </Task>
    </Tasks>
  </Job>
</Jobs>
XML;
$xml = simplexml_load_string($str);
// echo '<pre>', print_r($xml, 1), '</pre>';
foreach ($xml->Job as $job) {
    $state = (string)$job->State;
    if (!in_array($state, $required)) continue;

    echo "{$job->Name} — $state — ";
    
    foreach ($job->Tasks->Task as $task) {
        if ($task->Name == $state) {
            echo $task->DueDate . '<br>';
        }
    }
    
}

Output

Job 1 — Concept Design — 2019-08-31

EDIT - Alternative method

$xml = simplexml_load_string($str);

foreach ($xml->xpath("//Job") as $job) {
    $state = (string)$job->State;
    if (!in_array($state, $required)) continue;
    
    echo "{$job->Name} &mdash; $state &mdash; ";
    
    $task = $job->xpath("//Task[Name='$state']") ;
    echo $task[0]->DueDate . '<br>';
}

 

Edited by Barand
Link to comment
Share on other sites

Hi Barand,  Thanks again for your response. I wasn't able to achieve what you had suggested in my script unfortunately. 

I've attached a screen shot of what it is I'm trying to achieve. Basically in the due date column I want to show the due date of the specific task that is currently shown in the column alongside for that job.

The xml file is very long so below is a snippet from one job to give you an idea of how each job is set out. As you can see there is about 9 states each with a different start and due date. So I need to find the due date of the task that matches the job/state.

so in this case job AA1811 show the date of 31 03 2019 because the state is Construction Documentaton

 <Job>
            <ID>AA1811</ID>
            <Name>Widdowson House</Name>
            <Description>Your brief includes ... </Description>
            <Client>
                <ID>16913817</ID>
                <Name>Client </Name>
            </Client>
            <ClientOrderNumber></ClientOrderNumber>
            <State>Construction Documentation</State>
            <Type>Active Private Client</Type>
            <StartDate>2018-07-16T00:00:00</StartDate>
            <DueDate>2019-07-31T00:00:00</DueDate>
            <Contact>
                <ID>10634004</ID>
                <Name>Client</Name>
            </Contact>
            <InternalID>27797690</InternalID>
            <Manager>
                <ID>610781</ID>
                <Name>Manager</Name>
            </Manager>
            <Partner>
                <ID>610781</ID>
                <Name>Partner</Name>
            </Partner>
            <Assigned>
                <Staff>
                    <ID>610781</ID>
                    <Name>Manager</Name>
                </Staff>
            </Assigned>
            <Tasks>
                <Task>
                    <ID>100605569</ID>
                    <Name>Measure Up</Name>
                    <TaskID>3462948</TaskID>
                    <EstimatedMinutes>60</EstimatedMinutes>
                    <ActualMinutes>120</ActualMinutes>
                    <Description>Measure areas of house specific to works. Existing drawings will be used to model remainder of house. </Description>
                    <Completed>true</Completed>
                    <Billable>true</Billable>
                    <Folder></Folder>
                    <StartDate>2018-07-16T00:00:00</StartDate>
                    <DueDate>2018-07-20T00:00:00</DueDate>
                    <Assigned>
                        <Staff>
                            <ID>610781</ID>
                            <Name>Staff1</Name>
                        </Staff>
                    </Assigned>
                </Task>
                <Task>
                    <ID>100605570</ID>
                    <Name>Model Existing</Name>
                    <TaskID>3462949</TaskID>
                    <EstimatedMinutes>240</EstimatedMinutes>
                    <ActualMinutes>638</ActualMinutes>
                    <Description>Set up computer model of existing for documentation purposes. Includes showing proposed works for presentation prior to starting working drawings.</Description>
                    <Completed>true</Completed>
                    <Billable>true</Billable>
                    <Folder></Folder>
                    <StartDate>2018-07-16T00:00:00</StartDate>
                    <DueDate>2018-07-20T00:00:00</DueDate>
                    <Assigned>
                        <Staff>
                            <ID>610781</ID>
                            <Name>Staff2</Name>
                        </Staff>
                    </Assigned>
                </Task>
                <Task>
                    <ID>100605571</ID>
                    <Name>Town Planning Assessment</Name>
                    <TaskID>3462967</TaskID>
                    <EstimatedMinutes>60</EstimatedMinutes>
                    <ActualMinutes>0</ActualMinutes>
                    <Description>Checking town planning rules to assess whether resource consent is required.</Description>
                    <Completed>true</Completed>
                    <Billable>true</Billable>
                    <Folder></Folder>
                    <StartDate>2018-07-16T00:00:00</StartDate>
                    <DueDate>2018-07-28T00:00:00</DueDate>
                    <Assigned>
                        <Staff>
                            <ID>610781</ID>
                            <Name>Staff1</Name>
                        </Staff>
                    </Assigned>
                </Task>
                <Task>
                    <ID>103761923</ID>
                    <Name>Developed Design</Name>
                    <TaskID>3581926</TaskID>
                    <EstimatedMinutes>120</EstimatedMinutes>
                    <ActualMinutes>239</ActualMinutes>
                    <Description>Modify and develop drawings after presentation of concept design. Includes preparation of drawings and outline spec for estimate pricing and liaison/ communication with builder to ensure budget constraints are achieved. </Description>
                    <Completed>true</Completed>
                    <Billable>true</Billable>
                    <Folder></Folder>
                    <StartDate>2018-08-16T00:00:00</StartDate>
                    <DueDate>2018-08-24T00:00:00</DueDate>
                    <Assigned>
                        <Staff>
                            <ID>610781</ID>
                            <Name>Staff2</Name>
                        </Staff>
                    </Assigned>
                </Task>
                <Task>
                    <ID>103023106</ID>
                    <Name>Consultant Liaison</Name>
                    <TaskID>3755602</TaskID>
                    <EstimatedMinutes>180</EstimatedMinutes>
                    <ActualMinutes>60</ActualMinutes>
                    <Description>Meetings/ correspondence with consultants. Ie Landscape designers, engineers. </Description>
                    <Completed>false</Completed>
                    <Billable>true</Billable>
                    <Folder></Folder>
                    <StartDate>2018-08-08T00:00:00</StartDate>
                    <DueDate>2019-03-31T00:00:00</DueDate>
                    <Assigned>
                        <Staff>
                            <ID>610781</ID>
                            <Name>Staff1</Name>
                        </Staff>
                    </Assigned>
                </Task>
                <Task>
                    <ID>100605574</ID>
                    <Name>Consultant Engagement</Name>
                    <TaskID>3462950</TaskID>
                    <EstimatedMinutes>60</EstimatedMinutes>
                    <ActualMinutes>202</ActualMinutes>
                    <Description>Engagement of any consultants required ie: structural/geotechnical engineer, surveyor. Please note this does not include the consultants respective fees. Also allows for correspondence and integration of engineers design into consent documentation. 
</Description>
                    <Completed>true</Completed>
                    <Billable>true</Billable>
                    <Folder></Folder>
                    <StartDate>2018-10-01T00:00:00</StartDate>
                    <DueDate>2019-03-31T00:00:00</DueDate>
                    <Assigned>
                        <Staff>
                            <ID>610781</ID>
                            <Name>Staff1</Name>
                        </Staff>
                    </Assigned>
                </Task>
                <Task>
                    <ID>103022922</ID>
                    <Name>Resource Consent</Name>
                    <TaskID>3462952</TaskID>
                    <EstimatedMinutes>360</EstimatedMinutes>
                    <ActualMinutes>1083</ActualMinutes>
                    <Description>Produce documentation and submit documents to council for processing. Does not allow for time in hearings or notified consents</Description>
                    <Completed>true</Completed>
                    <Billable>true</Billable>
                    <Folder></Folder>
                    <StartDate>2018-08-08T00:00:00</StartDate>
                    <DueDate>2018-10-31T00:00:00</DueDate>
                    <Assigned>
                        <Staff>
                            <ID>610781</ID>
                            <Name>Staff2</Name>
                        </Staff>
                    </Assigned>
                </Task>
                <Task>
                    <ID>100605572</ID>
                    <Name>Construction Documentation</Name>
                    <TaskID>3462955</TaskID>
                    <EstimatedMinutes>1800</EstimatedMinutes>
                    <ActualMinutes>834</ActualMinutes>
                    <Description>Full documentation for Building consent submission. Includes specifications. Does not include kitchen, bathroom or other joinery documentation. </Description>
                    <Completed>false</Completed>
                    <Billable>true</Billable>
                    <Folder></Folder>
                    <StartDate>2018-10-01T00:00:00</StartDate>
                    <DueDate>2019-03-31T00:00:00</DueDate>
                    <Assigned>
                        <Staff>
                            <ID>610781</ID>
                            <Name>Staff2</Name>
                        </Staff>
                    </Assigned>
                </Task>
                <Task>
                    <ID>100605573</ID>
                    <Name>Consent Submission</Name>
                    <TaskID>3462956</TaskID>
                    <EstimatedMinutes>180</EstimatedMinutes>
                    <ActualMinutes>0</ActualMinutes>
                    <Description>Compiling, printing and submission of Consent Documentation.
Does not include correspondence with Council during Consenting process or changes to documents once consent has been issued. </Description>
                    <Completed>false</Completed>
                    <Billable>true</Billable>
                    <Folder></Folder>
                    <StartDate>2018-10-29T00:00:00</StartDate>
                    <DueDate>2019-03-31T00:00:00</DueDate>
                    <Assigned>
                        <Staff>
                            <ID>610781</ID>
                            <Name>Staff2</Name>
                        </Staff>
                    </Assigned>
                </Task>
            </Tasks>
        </Job>

The code I've been trying this time...

        
        </table>
      <table id="dtVerticalScrollExample" class="bg-white table table-bordered table-sm" cellspacing="0"
  width="100%">
  <thead>
    <tr>
      <th class="th-sm-1">Project Number</th>
      <th class="th-sm-1">Project Name</th>  
      <th class="th-sm-1">Feasibility</th>
      <th class="th-sm-1">Measure Up</th>
      <th class="th-sm-1">Model Drawing</th>
      <th class="th-sm-1">Concept Design</th>
      <th class="th-sm-1">Developed Design</th>
      <th class="th-sm-1">Resource Consent</th>
      <th class="th-sm-1">Construction Docs</th>
      <th class="th-sm-1">Milestone</th>
      <th class="th-sm-1">Due Date</th>
    </tr>
  </thead>
  <tbody>
    <?php 
$job_priority ="";
$js_1 =''; 
$js_2 =''; 
$js_3 =''; 
$js_4 =''; 
$js_5 =''; 

if ($err) {
  echo "cURL Error #:" . $err;
} else {
$projects = array();    

$required = ['Feasibility', 'Measure Up', 'Model Drawing', 'Concept Design', 'Developed Design', 'Resource Consent', 'Construction Documentation' ];

$xml=simplexml_load_string($response) or die("Error: Cannot create object");
     
foreach($xml->Jobs->Job as $item) {
       
if (in_array((string)$item->State, $required) ) { 
   
    $projects[] = array(
                     'job_no'          => (string)$item->ID,
                     'job_name'        => (string)$item->Name,
                     'job_due'         => (string)$item->Tasks->Task->DueDate,
                     'job_status'      => (string)$item->State,
                     'job_task'        => (string)$item->Tasks->Task->Name,
                     'job_staff'       => (string)$item->Assigned->Staff->Name,
                    );
     }
}
 
   // usort($projects, function($a,$b) {return $b['job_due'] <=> $a['job_due']; } );
         
foreach ($projects as $proj) {
    
    $job_no =$proj['job_no'];
    $job_name =$proj['job_name'];
    $job_status =$proj['job_status'];
    $job_staff =$proj['job_staff'];  
    $job_task =$proj['job_task'];
             
//if ($job_due > date('d-m-Y')) { 
    

if ($job_staff == 'Staff1') {
  $js_1 ='SP';  
}    
if ($job_staff == 'Staff2') {
  $js_2 ='MB';  
} 
if ($job_staff == 'Staff3') {
  $js_3 ='BF';  
} 
if ($job_staff == 'Staff4') {
  $js_4 ='MCP';  
} 
if ($job_staff == 'Staff5') {
  $js_5 ='DG';  
} 
$j_s_1 = $js_1 .' '. $js_2 .' '. $js_3 .' '. $js_4 .' '. $js_5;
$j_s_2 = $js_1 .' '. $js_2 .' '. $js_3 .' '. $js_4 .' '. $js_5;
$j_s_3 = $js_1 .' '. $js_2 .' '. $js_3 .' '. $js_4 .' '. $js_5;
$j_s_4 = $js_1 .' '. $js_2 .' '. $js_3 .' '. $js_4 .' '. $js_5;
$j_s_5 = $js_1 .' '. $js_2 .' '. $js_3 .' '. $js_4 .' '. $js_5;
$j_s_6 = $js_1 .' '. $js_2 .' '. $js_3 .' '. $js_4 .' '. $js_5;
$j_s_7 = $js_1 .' '. $js_2 .' '. $js_3 .' '. $js_4 .' '. $js_5;

   
    echo "<tr>";
    echo "<td><a href='managejob.php?job_id=$job_no'><strong>$job_no</strong></td>";
    echo "<td>$job_name</a></td>"; 
 if ($job_status == 'Feasibility') {
    echo "<td>";
    echo "<form action='' method='POST'>";
    echo "<select class='form-control bg-danger text-white' name ='StaffName' onchange='this.form.submit();'>";
    echo "<option value =''>$j_s_1</option>"; 
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Feasibility </option> ";      
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Measure Up </option> ";                  
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Model Drawing</option> ";                  
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Concept Design </option> "; 
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Developed Design </option> "; 
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Resource Consent </option> "; 
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Construction Docs  </option> ";
    echo "</select>";
    echo "</form></td>";
            } else {
    echo "<td class ='bg-white text-white text-center'></td>";
            }
if ($job_status == 'Measure Up') {
    echo "<td>";
    echo "<form action='' method='POST'>";
    echo "<select class='form-control bg-warning text-white' name ='StaffName' onchange='this.form.submit();'>";
    echo "<option value =''>$j_s_2</option>"; 
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Feasibility </option> ";      
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Measure Up </option> ";                  
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Model Drawing </option> ";                  
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Concept Design </option> "; 
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Developed Design </option> "; 
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Resource Consent </option> "; 
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Construction Docs  </option> ";           
    echo "</select>";
    echo "</form></td>";
            } else {
    echo "<td class ='bg-white text-white text-center'></td>";
            }          
    if ($job_status == 'Model Drawing') {
    echo "<td>";
    echo "<form action='' method='POST'>";
    echo "<select class='form-control bg-success text-white' name ='StaffName' onchange='this.form.submit();'>";
    echo "<option value =''>$j_s_3</option>"; 
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Feasibility </option> ";      
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Measure Up </option> ";                  
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Model Drawing </option> ";                  
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Concept Design </option> "; 
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Developed Design </option> "; 
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Resource Consent </option> "; 
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Construction Docs  </option> ";            
    echo "</select>";
    echo "</form></td>";
            } else {
    echo "<td class ='bg-white text-white text-center'></td>";
            } 
    if ($job_status == 'Concept Design') {
    echo "<td>";
    echo "<form action='' method='POST'>";
    echo "<select class='form-control bg-primary text-white' name ='StaffName' onchange='this.form.submit();'>";
    echo "<option value =''>$j_s_4</option>"; 
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Feasibility </option> ";      
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Measure Up </option> ";                  
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Model Drawing </option> ";                  
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Concept Design </option> "; 
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Developed Design </option> "; 
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Resource Consent </option> "; 
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Construction Docs  </option> ";            
    echo "</select>";
    echo "</form></td>";
            } else {
    echo "<td class ='bg-white text-white text-center'></td>";
            }
    if ($job_status == 'Developed Design') {
    echo "<td>";
    echo "<form action='' method='POST'>";
    echo "<select class='form-control bg-info text-white' name ='StaffName' onchange='this.form.submit();'>";
    echo "<option value =''>$j_s_5</option>"; 
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Feasibility </option> ";      
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Measure Up </option> ";                  
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Model Drawing </option> ";                  
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Concept Design </option> "; 
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Developed Design </option> "; 
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Resource Consent </option> "; 
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Construction Docs  </option> ";            
    echo "</select>";
    echo "</form></td>";
            } else {
    echo "<td class ='bg-white text-white text-center'></td>";
            }
    if ($job_status == 'Resource Consent') {
    echo "<td>";
    echo "<form action='' method='POST'>";
    echo "<select class='form-control bg-secondary text-white' name ='StaffName' onchange='this.form.submit();'>";
    echo "<option value =''>$j_s_6</option>"; 
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Feasibility </option> ";      
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Measure Up </option> ";                  
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Model Drawing </option> ";                  
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Concept Design </option> "; 
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Developed Design </option> "; 
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Resource Consent </option> "; 
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Construction Docs  </option> ";            
    echo "</select>";
    echo "</form></td>";
            } else {
    echo "<td class ='bg-white text-white text-center'></td>";
            }
    if ($job_status == 'Construction Documentation') {
    echo "<td>";
    echo "<form action='' method='POST'>";
    echo "<select class='form-control bg-dark text-white' name ='StaffName' onchange='this.form.submit();'>";
    echo "<option value =''>$j_s_7</option>"; 
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Feasibility </option> ";      
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Measure Up </option> ";                  
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Model Drawing </option> ";                  
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Concept Design </option> "; 
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Developed Design </option> "; 
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Resource Consent </option> "; 
    echo "<option class='form-control col-sm-3 bg-white text-dark' value ='status'>Construction Docs </option> ";            
    echo "</select>";
    echo "</form></td>";
            } else {
    echo "<td class ='bg-white text-white text-center'></td>";
            }
    echo "<td>$job_priority</td>";
  
//foreach ($xml->xpath("/Jobs/Job") as $job) {
   // $state = (string)$job->State;
  // if (!in_array($state, $required)) continue;
//    $task = $job->xpath("Tasks/Task[Name='$state'}");
   // $job_due = date('d/m/Y', strtotime($task[0]->DueDate)); 
//     $job_due = $task->DueDate;  
  $job_state = $xml->xpath("Jobs/Job[(State= '$job_status') and (Tasks/Task/Name= '$job_status')]");
  $date_due = $xml->xpath("Jobs/Job/Tasks/Task[DueDate]");
      if (in_array($date_due, $job_state)) {

            $job_due = date('d/m/Y', strtotime($date_due));
         
    echo "<td><strong></strong>$job_due</td>";
    echo "</tr>";
            }
 
} 

It's so close but still doesn't get the result I'm after. I do appreciate te help you are giving. Sorry for being difficult.

Capture.PNG

Link to comment
Share on other sites

This is my take on it. I copy/pasted a couple of extra jobs to give...

 

image.thumb.png.a04f143031066d53eb7b5efc1880b884.png

CODE

<?php
$required = ['Feasibility', 'Measure Up', 'Model Drawing', 'Concept Design', 'Developed Design', 'Resource Consent', 'Construction Documentation' ];

$colors =   array_combine($required, ['w3-red', 'w3-green', 'w3-orange', 'w3-deep-orange', 'w3-teal', 'w3-yellow', 'w3-purple'] );

$staff_arr = [ 'Staff1' => 'SP',
               'Staff2' => 'MB',
               'Staff3' => 'BF',
               'Staff4' => 'MCP',
               'Staff5' => 'DG'
             ];

function state_dropdown($staff, $color)
{
    return "<form action='' method='POST'>" .
     "<select class='w3-input w3-round $color' name ='StaffName' onchange='this.form.submit()'>" .                       // why is a menu of states called "StaffName" ?
     "<option value =''>$staff</option>" . 
     "<option class='form-control col-sm-3 bg-white text-dark'>Feasibility </option> " .      
     "<option class='form-control col-sm-3 bg-white text-dark'>Measure Up </option> " .                  
     "<option class='form-control col-sm-3 bg-white text-dark'>Model Drawing </option> " .                  
     "<option class='form-control col-sm-3 bg-white text-dark'>Concept Design </option> " . 
     "<option class='form-control col-sm-3 bg-white text-dark'>Developed Design </option> " . 
     "<option class='form-control col-sm-3 bg-white text-dark'>Resource Consent </option> " . 
     "<option class='form-control col-sm-3 bg-white text-dark'>Construction Docs  </option> " .            
     "</select>" .
     "</form>";
}
  
  
$xml = simplexml_load_file('plugnz.xml');
$data = [];

//
//  collect the jobs and current task data into an array
//
foreach ($xml->Jobs->Job as $job) {
    $id = (string)$job->ID;
    $state = (string)$job->State;
    if (!in_array($state, $required)) continue;
    
    $data[$id] = [ 'name' => (string)$job->Name,
                   'state' => $state
                 ];
    $tasks = $job->xpath("Tasks/Task[Name='$state']");
    $clr = $colors[$state];
    $due = (string)$tasks[0]->DueDate;
    $data[$id]['due'] = date('Y-m-d', strtotime($due));
    $data[$id]['display_date'] = date('M d Y', strtotime($due));
    
    $assigned = [];
    foreach ($tasks[0]->Assigned->Staff as $s) {
        $assigned[] = $staff_arr[(string)$s->Name];
    }
    $staff_str = join(' ', $assigned);
    $data[$id]['task'] = [ 'staff' => $staff_str, 'clr' => $clr ];
}

//
//  sort the data array on the task due date DESC
//
uasort($data, function($a,$b) { return $b['due'] <=> $a['due']; } );

//
// output the array as a table
//
$tdata = '';
foreach ($data as $jid => $jdata) {
    $tdata .= "<tr><td class='jobno'>$jid</td><td>{$jdata['name']}</td>";
    foreach ($required as $stat) {
        if ($jdata['state']==$stat) {
            $tdata .= "<td>" . state_dropdown($jdata['task']['staff'], $jdata['task']['clr']) . "</td>";
        }
        else {
            $tdata .= "<td>&nbsp;</td>";
        }
    }
    $tdata .= "<td>&nbsp;</td>";
    $tdata .= "<td>{$jdata['display_date']}</td></tr>";
}

?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="creation-date" content="05/10/2019">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<title>Job Status Table</title>
<style type="text/css">
    body { font-family: verdana,sans-serif; font-size: 10pt; padding: 20px 50px; }
    table {border-collapse: collapse;}
    .th-sm-1 { font-size: 8pt; text-align: left; }
    .jobno   { font-weight: 600; color: #2196f3; }
    select   { width: 120px; }
</style>
</head>
<body>
    <table border=1>
        <thead>
        <tr>
          <th class="th-sm-1">Project Number</th>
          <th class="th-sm-1">Project Name</th>  
          <th class="th-sm-1">Feasibility</th>
          <th class="th-sm-1">Measure Up</th>
          <th class="th-sm-1">Model Drawing</th>
          <th class="th-sm-1">Concept Design</th>
          <th class="th-sm-1">Developed Design</th>
          <th class="th-sm-1">Resource Consent</th>
          <th class="th-sm-1">Construction Docs</th>
          <th class="th-sm-1">Milestone</th>
          <th class="th-sm-1">Due Date</th>
        </tr>
        </thead>
        <tbody>
            <?=$tdata?>
        </tbody>    
    </table>
</body>
</html>

 

Edited by Barand
code correction
  • Like 2
Link to comment
Share on other sites

  • 4 months later...

Hi Barand, 

 

Images to help explain my message to you. 

As you can see in the main table MC is not assigned to JC1919 Eng Deck, AA1913 Cannell Deck, JC1928 King Alterations, DW1904 Strachan, DW1905 Ohagan but in the dashboard page they are included which is not correct.

Thanks again. 

2021349464_Staffcurrenttasks.thumb.JPG.f53de70eba847f9d9e4ba6affb5622c3.JPG972138685_currenttasks.thumb.JPG.eaf045f0a42a6105fa10c70663a858a8.JPG

Capture.PNG

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.