我正在做一个php工作日历项目。这就是我要做的:查询数据库并获取计划信息(我得到的部分),获取这些信息并根据每个记录集创建一个作业对象,然后我需要按部门和开始日期/时间对对象进行排序,然后根据部门和时间将它们显示在动态表中。我已经走了这么远:
<?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函数,但这给了我一个数组数组,处理起来很麻烦。我怎样才能完成我想要做的事情?
您的分拣问题
最简单的排序方法是在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>";
}
}
您的所有属性都是私有的。另外,对象名称是jobs,您正在启动一个名为job的对象。另外,您的构造函数不接受传入的变量,您正试图向它发送一个字符串。jobInfo()
也是如此,它没有任何传入参数,您正试图向它发送一堆值。
要执行您正在尝试执行的操作,可以创建一个静态函数,该函数返回一组作业对象。