PHP implementation of the print class (Implementation queue printing)

Source: Internet
Author: User

Class implementation idea is: first to print the data are collected, in the use of JS call window printing function. Currently used for IE.

class provides print queuing functionality. (PS, plainly is a piece of reading data)

The code is as follows Copy Code
Class wprint{
Collect Print Code
Private $data = Array ();
Working with print Code
Private $handle;
Public Function __construct ()
{
Header ("Content-type:text/html;charsetutf-8");
$this->link (); Link Database
$this->collect ($_post["username"],$_post["content"],$_post["IP"));
$this->handle ();
}
Link Database
Private Function link ()
{
$link = mysql_connect (' localhost ', ' root ', ' 123456 ');
mysql_select_db (' Shen ', $link);
mysql_query (' SET NAMES utf8 ');
}
Collect Print Code
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);//Data node into stack
}
Handling Print Code Warehousing
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 (); Gets the ID obtained by the recent insert operation
echo "Data collection is successful, is queued for printing, queue ID is". $id;
$this->num ($id);
}
Else
{
echo "Data collection failed, please submit again after 3 seconds";
}
}
}
Check if descendant data is empty
Private function Check ($string)
{
if (strlen ($string) = = 0 | | $string = = "")
{
echo "Data collection failed with blank print";
Exit
}else
{
return $string;
}
}
Get Number of print queues
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 ", you also have". $num. " individuals in line ";
}
Print data
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 "Print processing complete";
}else
{
echo $row ["content"];
}
}
}

The idea is simple, collect the data one by one and deal with it. This not only solves the problem of network printing, but also avoids the problem of queueing in the network print printing process.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.