PHP云打印類完整示例
本文實(shí)例講述了PHP云打印類。分享給大家供大家參考,具體如下:
一個(gè)項(xiàng)目需求要幾百臺(tái)電腦都有打印功能,本來(lái)是想用網(wǎng)絡(luò)打印機(jī)的,后來(lái)發(fā)現(xiàn)沒有網(wǎng)絡(luò)打印機(jī),就自己動(dòng)手寫一個(gè)打印類算了。
類實(shí)現(xiàn)想法是:先把要打印的數(shù)據(jù)都收集起來(lái),在用js調(diào)用window打印函數(shù)。目前就使用于IE。
類提供打印排隊(duì)功能。(PS,說(shuō)白了就是一條一條讀取數(shù)據(jù))
class Wprint{
//收集打印代碼
private $data = array();
//處理打印代碼
private $handle;
public function __construct()
{
header("Content-type:text/html;charsetutf-8");
$this->link(); //鏈接數(shù)據(jù)庫(kù)
$this->collect($_POST["username"],$_POST["content"],$_POST["ip"]);
$this->handle();
}
//鏈接數(shù)據(jù)庫(kù)
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)入棧
}
//處理打印代碼入庫(kù)
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ù)收集失敗,請(qǐng)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"];
}
}
}
思想很簡(jiǎn)單,收集數(shù)據(jù)再一個(gè)一個(gè)處理。 這樣就不僅解決了網(wǎng)絡(luò)打印的問(wèn)題,還避免了網(wǎng)絡(luò)打印打印過(guò)程排隊(duì)的問(wèn)題。
更多關(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基本語(yǔ)法入門教程》、《php操作office文檔技巧總結(jié)(包括word,excel,access,ppt)》、《php日期與時(shí)間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP實(shí)現(xiàn)獲取毫秒時(shí)間戳的方法【使用microtime()函數(shù)】
這篇文章主要介紹了PHP實(shí)現(xiàn)獲取毫秒時(shí)間戳的方法,結(jié)合實(shí)例形式分析了php使用microtime()函數(shù)獲取、轉(zhuǎn)換毫秒級(jí)時(shí)間戳的相關(guān)操作技巧,需要的朋友可以參考下2019-03-03
PHP隨機(jī)生成隨機(jī)個(gè)數(shù)的字母組合示例
在很多系統(tǒng)環(huán)境下大家都會(huì)用到字母組合各種編碼。下面為大家介紹下使用php隨機(jī)生成隨機(jī)個(gè)數(shù)的字母組合,感興趣的朋友可以了解下2014-01-01
php中使用in_array() foreach array_search() 查找數(shù)組是否包含時(shí)的性能對(duì)比
這篇文章主要介紹了php中使用in_array() foreach array_search() 查找數(shù)組是否包含時(shí)的性能對(duì)比,需要的朋友可以參考下2015-04-04
如何在PHP環(huán)境中使用ProtoBuf數(shù)據(jù)格式
這篇文章主要介紹了如何在PHP環(huán)境中使用ProtoBuf數(shù)據(jù)格式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
php中$_SERVER[PHP_SELF] 和 $_SERVER[SCRIPT_NAME]之間的區(qū)別
php中$_SERVER[PHP_SELF] 和 $_SERVER[SCRIPT_NAME]之間的區(qū)別2009-09-09

