Jump to content

Problem With Max(Array) Function


surajkandukuri

Recommended Posts

Hi all,

 

  I am new to php and I was trying to find the latest date in my database but I am getting strange results, please help me out.

 

        My Database Looks like

job_cat_obj      Completiondate

1-1-1              2009-01-12

1-1-2              2009-03-02

1-1-3              2009-10-05

1-1-4              2009-02-11

1-2-1              2009-01-04

1-2-2              2009-03-09

 

          All I want to get is the latest date for each job_cat for example:- latest date for 1-1 is 2009-10-05 , and latest date for 1-2 is 2009-03-09.

 

          I get job_cat_obj number in $querysearchvariable, and here is my code

 

  $datescompleted[]=array();

    $categorycompletiondate[]=array();

    /* To retreve completed objectives dates*/

     

              $i=0;$j=0;$k=1;$m=0;

              for($i;$i<$numberofcategories;$i++)

              {

              echo("<br>");

             

              echo("<br>");

              $querysearch='1'."-".$categorynumber[$i](This variable gives whether it is 1 or2 i,e 1-1 or 1-2); ($querysearch=1-1/1-2/1-3)

              for($j=0,$k=1;$j<$values[$i+1];$j++)

                    {

                    $querysearchvariable=$querysearch."-".$k;

                    echo($querysearchvariable);

                    $k++;

                   

                    /*selecting the date based on the objective selected*/

                    $sql2="select * from objective_completiondate where Obj_cat_job='$querysearchvariable' and EmployeeNumber='34550'";

                    $result2=mysql_query($sql2);

                    $row2=mysql_fetch_array($result2);

                   

                    $datescompleted[$j]=$row2['completiondate'];

                    echo($datescompleted[$j]);   

                    echo("<br>");

                               

                    }

                   

                  echo("calculatecatcompletion");

                  echo("<br>");

                  echo($i);

                  $categorycompletiondate[$i]=max($datescompleted);

                  echo("<br>");

                    echo($categorycompletiondate[$i]);

                    echo("<br>");

                    //echo($categorycompletiondate[$i]);

                    echo("<br>");

             

                   

              }

 

          I get this output

 

1-1-12009-01-12

1-1-22009-03-02

1-1-32009-10-05

1-1-42009-02-11

calculatecatcompletion

0

2009-10-05

 

 

 

1-2-12009-01-04

1-2-22009-03-09

calculatecatcompletion

1

2009-10-05

 

 

 

I am totally confused where i made the mess !! First time the max function is returning correct date but second time its giving the first latest date only.

 

Anyhelp is greatly appreciated.

 

 

 

 

 

 

 

                   

Link to comment
https://forums.phpfreaks.com/topic/176881-problem-with-maxarray-function/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.