0

我正在做一个 php 工作日历项目。这就是我想要做的:查询数据库并获取计划信息(我得到的那部分),获取该信息并根据每个记录集创建一个工作对象,然后我需要按部门和开始日期对对象进行排序/time,然后根据部门和时间将它们显示在动态表中。我已经做到了这一点:

<?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
{

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

我可以通过以下方式访问该作业并从另一个页面执行此操作:

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

这只给了我从 sql 调用创建的对象。我曾尝试将它放入一个数组并将其传递给 jobinfo 函数,但这给了我一个数组数组并且使处理变得一团糟。我怎样才能完成我想做的事情?

4

2 回答 2

1

您所有的财产都是私有的。加上对象名称是作业,您正在启动一个名为作业的对象。另外,您的构造函数不接受传入变量,并且您正在尝试向其发送字符串。同样的,jobInfo()它没有任何传入参数,并且您正试图向它发送一堆值。

要执行您尝试执行的操作,您可以创建一个返回作业对象数组的静态函数。

于 2013-07-02T17:21:01.037 回答
1

您的排序问题

最简单的排序方法是在 SQL 查询中进行。因此可以将顺序更改为 ORDER BY DepartmentName, StartTime。

你的对象问题

您应该创建一个作业类,而不是拥有一个包含所有数据的作业类,并且该作业类有许多作业实例。也许是这样的:

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>";
    }
}
于 2013-07-02T18:00:00.133 回答