0

I am working on a php job calendar project. This is what I am trying to do: Query the database and get the scheduled information (that part I got), take that information and create a job object based on each record set, then I need to sort the objects by department and start date/time and then display them in a dynamic table based on the department and time. I have gotten this far:

<?php 
session_start();
include_once("functions.php");
class jobs
{
    //a job has a:
    private $eachJob;
    private $group;
    private $jID;
    private $num;
    private $jStatus;
    private $description;
    private $startDate;
    private $endDate;
    private $startTime;
    private $endTime;
    private $rSpan;
    private $department;


    public function __construct()
    {
        $this->buildJob($_SESSION['dept']);
    }

    public function buildJob($depatment)
    {
        $captionHeading = 'Traditional Print';
        $conn = connect();

        $tsql = "select + 'Job# ' + CAST (JobNum AS VARCHAR(10)) as JobNum, Description, datediff(MI,StartTime, EndTime) / 15 as 'RowSpan', AssetName, 
        AssetID, Status.ColorCode as tdCC, JobID, StartTime, EndTime, datediff(day,getDate(),StartTime)*24*60/15 as 'Blank', getdate() as now
        from Calendar_View, Departments, Status, Assets
        where Departments.DepartmentName = '$depatment' and Calendar_View.Department = Departments.DepartmentID and AssetStatus = Status.StatusID and 
        Calendar_View.Asset = Assets.AssetID
        order by AssetID asc";

        $stmt = sqlsrv_query($conn, $tsql);
        if ($stmt)
        {
            while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) 
            {
            /*******************************
            $this->jobDetails = array(
                $this->group    = $row{'AssetName'},
                $this->jID          = $row['JobID'],
                $this->num          = $row['JobNum'],
                $this->jStatus      = $row['tdCC'],
                $this->description  = "now =" . date_format($row['now'],'Y-m-d'),
                $this->startDate    = date_format($row['StartTime'],'Y-m-d'),
                $this->endDate      = date_format($row['EndTime'],'Y-m-d'),
                $this->startTime    = date_format($row['StartTime'],'g:i'),
                $this->endTime      = date_format($row['EndTime'],'g:i'),
                $this->rspan        = "Time needed: " . $row['RowSpan']);
                $this->jobInfo($this->jobDetails);
            **********************************/
                $this->group        = $row{'AssetName'};
                $this->jID          = $row['JobID'];
                $this->num          = $row['JobNum'];
                $this->jStatus      = $row['tdCC'];
                $this->description  = "now =" . date_format($row['now'],'Y-m-d');
                $this->startDate    = date_format($row['StartTime'],'Y-m-d');
                $this->endDate      = date_format($row['EndTime'],'Y-m-d');
                $this->startTime    = date_format($row['StartTime'],'g:i');
                $this->endTime      = date_format($row['EndTime'],'g:i');
                $this->rspan        = "Time needed: " . $row['RowSpan'];
                //$this->jobInfo();
            }
        }
        else
        {
            die( print_r(sqlsrv_errors(), true));
        }
    }
    public function jobInfo()
    {

    }
}
/******************************
class job
{

}
******************************/
?>

I can access the job and do so from another page with:

include_once("../php/job_class.php");
$obj=new  job("Traditional Print");
echo "<pre>";
echo print_r($obj);
echo "</pre>";

That only gives me the object created from the sql call. I have tried putting it into an array and passing it to the jobinfo function, but that gives me an array of arrays and makes a mess to deal with. How can I accomplish what I am trying to do?

1
  • Your while loop after the query is just going to overwrite the information in the job object everytime. You should probably place the query stuff in a jobFactory class or similar and use that class to create job objects (one object for each row). Commented Jul 2, 2013 at 17:21

2 Answers 2

1

Your sorting issue

The easiest way to sort is to do it in your SQL query. So could change the order to be ORDER BY DepartmentName, StartTime.

Your object issue

Instead of having a jobs class that holds all the data you should create a job class and the jobs class has many job instances. Maybe something like this:

class job
{
    //a job has a:
    private $eachJob;
    private $group;
    private $jID;
    private $num;
    private $jStatus;
    private $description;
    private $startDate;
    private $endDate;
    private $startTime;
    private $endTime;
    private $rSpan;
    private $department;

    public __construct($row) {
        $this->group        = $row{'AssetName'};
        $this->jID          = $row['JobID'];
        $this->num          = $row['JobNum'];
        $this->jStatus      = $row['tdCC'];
        $this->description  = "now =" . date_format($row['now'],'Y-m-d');
        $this->startDate    = date_format($row['StartTime'],'Y-m-d');
        $this->endDate      = date_format($row['EndTime'],'Y-m-d');
        $this->startTime    = date_format($row['StartTime'],'g:i');
        $this->endTime      = date_format($row['EndTime'],'g:i');
        $this->rspan        = "Time needed: " . $row['RowSpan'];
    }
}

class jobs
{
    private $jobs = array();


    public function __construct()
    {
        $this->buildJob($_SESSION['dept']);
    }

    public function buildJob($depatment)
    {
        $captionHeading = 'Traditional Print';
        $conn = connect();

        $tsql = "select + 'Job# ' + CAST (JobNum AS VARCHAR(10)) as JobNum, Description, datediff(MI,StartTime, EndTime) / 15 as 'RowSpan', AssetName, 
    AssetID, Status.ColorCode as tdCC, JobID, StartTime, EndTime, datediff(day,getDate(),StartTime)*24*60/15 as 'Blank', getdate() as now
    from Calendar_View, Departments, Status, Assets
    where Departments.DepartmentName = '$depatment' and Calendar_View.Department = Departments.DepartmentID and AssetStatus = Status.StatusID and 
    Calendar_View.Asset = Assets.AssetID
    order by AssetID asc";

        $stmt = sqlsrv_query($conn, $tsql);
        if ($stmt)
        {
            while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) 
            {

            $this->jobs[] = new job($row);
            }
        }
        else
        {
            die( print_r(sqlsrv_errors(), true));
        }
    }

    public function jobInfo()
    {
        $jobArray = array();
        echo "<pre>";
        echo print_r("$jobArray[1]");
        echo "</pre>";
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I believe this is exactly what I an trying to do. I am going to give this a try and will post back later on my progress. Thanks for taking the time to lend a hand!
Thanks again. With a couple of minor changes to your suggestion I have a working object set. Now, I understand why it is outputting the results the way it is, but I think there is an easier way. This gives me one jobs object that contains a jobs array that contains job object arrays. I had arrived at this point via a slightly different code structure. Is there a way to clean up the output? i.e. get away from the array of array of arrays??? No complaints about the help, just looking for cleaner and clearer output to deal with.
1

All your properties are private. Plus the object name is jobs and you are initiating a object called job. Plus your construct function doesn't take an incoming variable and you are trying to send it a string. Same goes for jobInfo() it doesn't have any incoming parameters and you are trying to send it a bunch of values.

To do what you are trying to do you could create a static function that returns an array of jobs objects.

1 Comment

Sorry, you were correct, I posted my edits and now the code I had working half way. Thanks for the input.

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.