本文实例讲述了PHP云打印类。分享给大家供大家参考,具体如下:
一个项目需求要几百台电脑都有打印功能,本来是想用网络打印机的,后来发现没有网络打印机,就自己动手写一个打印类算了。
类实现想法是:先把要打印的数据都收集起来,在用js调用window打印函数。目前就使用于IE。
类提供打印排队功能。(PS,说白了就是一条一条读取数据)
link(); //链接数据库
$this->collect($_POST["username"],$_POST["content"],$_POST["ip"]);
$this->handle();
}
//链接数据库
private function link()
{
$link = MysqL_connect('localhost','root','123456');
MysqL_select_db('shen',$link);
MysqL_query('SET NAMES utf8');
}
//收集打印代码
private function collect($username,$content,$ip)
{
$code["username"] = $username;
$code["content"] = $this->check($content);
$code["ip"] = $ip;
$code["state"] = 0;
$code["priority"] = 0;
array_push($this->data,$code);//数据节点入栈
}
//处理打印代码入库
private function handle()
{
foreach($this->data as $value)
{
$sql = "insert into print(username,content,ip,state,priority)
values('{$value["username"]}','{$value["content"]}','{$value["ip"]}','{$value["state"]}','{$value["priority"]}')";
$query = MysqL_query($sql);
if($query)
{
$id = MysqL_insert_id(); //获取最近insert操作得到的ID
echo "数据收集成功,正在排队打印,排队ID为".$id;
$this->num($id);
}
else
{
echo "数据收集失败,请3秒后再一次提交";
}
}
}
//检查传人数据是否为空
private function check($string)
{
if(strlen($string) == 0 || $string == " ")
{
echo "数据收集失败,打印内容为空";
exit;
}else
{
return $string;
}
}
//获取打印排队人数
private function num($id)
{
$sql = "select id from print where state=0 and id<".$id." order by id asc";
$query = MysqL_query($sql);
$num = MysqL_num_rows($query);
echo ",您前面还有".$num."个人在排队";
}
//打印数据
public function Yprint()
{
$sql = "select id,content from print where state=0 order by id asc limit 1";
$query = MysqL_query($sql);
$row = MysqL_fetch_array($query);
if(!empty($row["content"]))
{
echo "