PHP云打印類完整示例
本文實(shí)例講述了PHP云打印類。分享給大家供大家參考,具體如下:
一個(gè)項(xiàng)目需求要幾百臺(tái)電腦都有打印功能,本來是想用網(wǎng)絡(luò)打印機(jī)的,后來發(fā)現(xiàn)沒有網(wǎng)絡(luò)打印機(jī),就自己動(dòng)手寫一個(gè)打印類算了。
類實(shí)現(xiàn)想法是:先把要打印的數(shù)據(jù)都收集起來,在用js調(diào)用window打印函數(shù)。目前就使用于IE。
類提供打印排隊(duì)功能。(PS,說白了就是一條一條讀取數(shù)據(jù))
class Wprint{
//收集打印代碼
private $data = array();
//處理打印代碼
private $handle;
public function __construct()
{
header("Content-type:text/html;charsetutf-8");
$this->link(); //鏈接數(shù)據(jù)庫
$this->collect($_POST["username"],$_POST["content"],$_POST["ip"]);
$this->handle();
}
//鏈接數(shù)據(jù)庫
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);//數(shù)據(jù)節(jié)點(diǎn)入棧
}
//處理打印代碼入庫
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 "數(shù)據(jù)收集成功,正在排隊(duì)打印,排隊(duì)ID為".$id;
$this->num($id);
}
else
{
echo "數(shù)據(jù)收集失敗,請3秒后再一次提交";
}
}
}
//檢查傳人數(shù)據(jù)是否為空
private function check($string)
{
if(strlen($string) == 0 || $string == " ")
{
echo "數(shù)據(jù)收集失敗,打印內(nèi)容為空";
exit;
}else
{
return $string;
}
}
//獲取打印排隊(duì)人數(shù)
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."個(gè)人在排隊(duì)";
}
//打印數(shù)據(jù)
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"];
}
}
}
思想很簡單,收集數(shù)據(jù)再一個(gè)一個(gè)處理。 這樣就不僅解決了網(wǎng)絡(luò)打印的問題,還避免了網(wǎng)絡(luò)打印打印過程排隊(duì)的問題。
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語法入門教程》、《php操作office文檔技巧總結(jié)(包括word,excel,access,ppt)》、《php日期與時(shí)間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP下通過exec獲得計(jì)算機(jī)的唯一標(biāo)識(shí)[CPU,網(wǎng)卡 MAC地址]
PHP下通過exec獲得計(jì)算機(jī)的唯一標(biāo)識(shí)的代碼,可獲得CPU,網(wǎng)卡 MAC地址信息。2011-06-06
php使用curl_init()和curl_multi_init()多線程的速度比較詳解
這篇文章主要介紹了php使用curl_init()和curl_multi_init()多線程的速度比較,結(jié)合實(shí)例形式詳細(xì)分析了curl_init()和curl_multi_init()的具體使用方法及相關(guān)效率比較,需要的朋友可以參考下2018-08-08
php結(jié)合web uploader插件實(shí)現(xiàn)分片上傳文件
這篇文章主要為大家詳細(xì)介紹了php結(jié)合web uploader插件實(shí)現(xiàn)分片上傳文件, 采用大文件分片并發(fā)上傳,極大的提高了文件上傳效率,感興趣的小伙伴們可以參考一下2016-05-05
php+jquery編碼方面的一些心得(utf-8 gb2312)
在開發(fā)php與jquery的過程中,需要注意的一些心得,防止亂碼的出現(xiàn)。2010-10-10
PHP中date()日期函數(shù)有關(guān)參數(shù)整理
PHP中date()日期函數(shù)有關(guān)參數(shù)整理,需要的朋友可以參考下。2011-07-07
php基于 swoole 實(shí)現(xiàn)的異步處理任務(wù)功能示例
這篇文章主要介紹了php基于 swoole 實(shí)現(xiàn)的異步處理任務(wù)功能,涉及swoole的安裝、配置及php調(diào)用swoole守護(hù)進(jìn)程監(jiān)控任務(wù)相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-08-08
PHP+MySQL實(shí)現(xiàn)模糊查詢員工信息功能示例
這篇文章主要介紹了PHP+MySQL實(shí)現(xiàn)模糊查詢員工信息功能,結(jié)合實(shí)例形式分析了php連接mysql數(shù)據(jù)庫及使用like語句進(jìn)行模糊查詢與顯示相關(guān)操作技巧,需要的朋友可以參考下2018-06-06

