PHP实现打印类(实现队列排队打印)
打印类实现原理很简单就是用户点击打印时把数据保存到数据库,然后由js来调用打印机来打印,如果打印完了把记录删除,再执行一条打印了,具体代码如下。
类实现想法是:先把要打印的数据都收集起来,在用js调用window打印函数。目前就使用于IE。
类提供打印排队功能。(PS,说白了就是一条一条读取数据)
<?php class Wprint { //收集打印代码 private $data = array(); //处理打印代码 private $handle; public function __construct() { header("Content-type:text/html;charsetutf-8"); $this->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 "<script tyle="text / javascript"> window.print(); </script>"; $id = $row["id"]; $sql = "update print set state=1 where id=" . $id; mysql_query($sql); echo "打印处理完成"; } else { echo $row["content"]; } } }
思想很简单,收集数据再一个一个处理。这样就不仅解决了网络打印的问题,还避免了网络打印打印过程排队的问题
文章地址:http://www.phprm.com/code/81539.html
转载随意^^请带上本文地址!