類實現想法是:先把要列印的資料都收集起來,在用js調用window列印函數。目前就使用於IE。
類提供列印排隊功能。(PS,說白了就是一條一條讀取資料)
代碼如下 |
複製代碼 |
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"]; } } } |
思想很簡單,收集資料再一個一個處理。 這樣就不僅解決了網路列印的問題,還避免了網路列印列印過程排隊的問題