根据id表将单个数组结果发送到打印机

时间:2013-07-15 18:32:15

标签: php javascript mysql printing

我正在研究一个简单的PHP脚本,它将用户详细信息存储在MySQL数据库中。只要查询与姓氏(预定义)匹配,我就可以运行查询并让它返回单个记录。一旦我有了记录,我希望能够回显每条记录旁边的打印按钮,以便用户可以单独打印每条记录。

代码是几个代码片段的混搭,并且工作得很好,除了"打印用户数据"部分。我不是PHP的新手,但我也知道足以浏览脚本。这是我到目前为止所做的事情。

<?php
//capture search term and remove spaces at its both ends if the is any
$searchTerm = trim($_GET['keyname']);

//check whether the name parsed is empty
if($searchTerm == "")
{
echo "Enter name you are searching for.";
exit();
}

//database connection info
$host = "localhost"; //server
$db = "users"; //database name
$user = "root"; //dabases user name
$pwd = "password1"; //password

//connecting to server and creating link to database
$link = mysqli_connect($host, $user, $pwd, $db);

//MYSQL search statement
$query = "SELECT * FROM details WHERE lastname LIKE '%$searchTerm%'";

$results = mysqli_query($link, $query);

/* check whether there were matching records in the table
by counting the number of results returned */
if(mysqli_num_rows($results) >= 1)
{
$output = "";
while($row = mysqli_fetch_array($results))
{

        echo "<tr valign=bottom>";
        echo "<td bgcolor=#ffffff background='img/dots.gif' colspan=6><img     src=img/blank.gif width=1 height=1></td>";
        echo "</tr>";

    echo "<tr valign=center>";
    echo "<td class=tabval><img src=img/blank.gif width=10 height=20></td>";

    echo "<td class=tabval><b>".htmlspecialchars($row['firstname'])."</b> </td>";
    echo "<td class=tabval>".htmlspecialchars($row['lastname'])."&nbsp;</td>";
    echo "<td class=tabval>".htmlspecialchars($row['address'])."</td> ";
    echo "<td class=tabval>".htmlspecialchars($row['phone'])."&nbsp;</td>";
    echo "<button type=\"button\" class=\"formbutton\" onclick=\"printpage('" .     $lastname . "'); \">Print User Data</button>";



    echo "<td class=tabval></td>";
    echo "</tr>";
    $i++;

}
echo "<tr valign=bottom>";
    echo "<td bgcolor=#fb7922 colspan=6><img src=img/blank.gif width=1 height=8></td>";
    echo "</tr>";
echo $output;

}
else
echo "There was no matching record for the name " . $searchTerm;
?>


<br />
<a href="javascript:history.go(-1)">Done</a>

1 个答案:

答案 0 :(得分:0)

echo "<button type=\"button\" class=\"formbutton\" onclick=\"printpage('" . $lastname . "'); \">Print User Data</button>";

这一行调用了一个名为“printpage”的javascript函数。一旦你在页面上有了这个javascript功能,你就会更接近预期的结果。