当前位置: 编程技术>php
本页文章导读:
▪php mysql分页类与调用实例 本节分享一个php、mysql分页类,代码如下:
<?php
/****
文件名 : pagination.class.php
功 能 : php mysql 分页类
****/
class pagination {
var $fullresult; // 数据库中整个记录的结果集
var $to.........
▪php实例:收集Email地址与用户反馈 1,收集Email地址 //index.htm
<html>
<body>
<form action="/blog_article/index.html" method="post">
姓 名:<br>
<input type="text" name="name" size="20" maxlength="20" value=""><br>
Email:<br>
<input type="text.........
▪php实例:计算表单数据-计算器的代码 1,计算表单中的数据,类似一个小型计算器。
<html>
<head>
<title>计算表单数据-www.</title>
</head>
<body>
<form action = "calc.php" method = "post">
值 1: <input t.........
[1]php mysql分页类与调用实例
来源: 互联网 发布时间: 2013-12-24
本节分享一个php、mysql分页类,代码如下:
<?php
/****
文件名 : pagination.class.php
功 能 : php mysql 分页类
****/
class pagination {
var $fullresult; // 数据库中整个记录的结果集
var $totalresult; // 总记录数
var $query; // 用户查询
var $resultPerPage; // 每页显示的记录数
var $resultpage; // 每一页的记录集
var $pages; // 总页数
var $openPage; // 当前页
/*
@param - 查询语句
@param - 每页记录数
*/
function createPaging($query,$resultPerPage)
{
$this->query = $query;
$this->resultPerPage= $resultPerPage;
$this->fullresult = mysql_query($this->query);
$this->totalresult = mysql_num_rows($this->fullresult);
$this->pages = $this->findPages($this->totalresult,$this->resultPerPage);
if(isset($_GET['page']) && $_GET['page']>0) {
$this->openPage = $_GET['page'];
if($this->openPage > $this->pages) {
$this->openPage = 1;
}
$start = $this->openPage*$this->resultPerPage-$this->resultPerPage;
$end = $this->resultPerPage;
$this->query.= " LIMIT $start,$end";
}
elseif($_GET['page']>$this->pages) {
$start = $this->pages;
$end = $this->resultPerPage;
$this->query.= " LIMIT $start,$end";
}
else {
$this->openPage = 1;
$this->query .= " LIMIT 0,$this->resultPerPage";
}
$this->resultpage = mysql_query($this->query);
}
/*
@param - 总记录数
@param - 每页记录数
*/
function findPages($total,$perpage)
{
$pages = intval($total/$perpage);
if($total%$perpage > 0) $pages++;
return $pages;
}
/*
显示分页
*/
function displayPaging()
{
$self = $_SERVER['PHP_SELF'];
if($this->openPage<=0) {
$next = 2;
}
else {
$next = $this->openPage+1;
}
$prev = $this->openPage-1;
$last = $this->pages;
if($this->openPage > 1) {
echo "<a href=/blog_article/$self/page/1/gt;First/lt;/a/gt;/amp;nbsp/amp;nbsp;.html";
echo "<a href=/blog_article/$self/page/$prev/gt;Prev/lt;/a/gt;/amp;nbsp/amp;nbsp;.html";
}
else {
echo "First  ";
echo "Prev  ";
}
for($i=1;$i<=$this->pages;$i++) {
if($i == $this->openPage)
echo "$i  ";
else
echo "<a href=/blog_article/$self/page/$i/gt;$i/lt;/a/gt;/amp;nbsp/amp;nbsp;.html";
}
if($this->openPage < $this->pages) {
echo "<a href=/blog_article/$self/page/$next/gt;Next/lt;/a/gt;/amp;nbsp/amp;nbsp;.html";
echo "<a href=/blog_article/$self/page/$last/gt;Last/lt;/a/gt;/amp;nbsp/amp;nbsp;.html";
}
else {
echo "Next  ";
echo "Last  ";
}
}
}
?>
调用示例:
<?php
/***
文件名称 : showresult.php
功 能 : 显示分页结果
***/
include_once("pagination.class.php"); // 调用分页类
$pagination = new pagination();
$query = "SELECT * FROM `student` WHERE 1"; // 查询语句
/*
调用方法,创建分页
@param - 数据库查询
@param - 每页显示的记录数
*/
$pagination->createPaging($query,10);
echo '<table border="1" width="400" align="center">';
while($row=mysql_fetch_object($pagination->resultpage)) {
echo '<tr><td>'.$row->name.'</td><td>'.$row->age.'</td></tr>'; // display name and age from database
}
echo '</table>';
echo '<table border="1" width="400" align="center">';
echo '<tr><td>';
$pagination->displayPaging();
echo '</td></tr>';
echo '</table>';
?>
[2]php实例:收集Email地址与用户反馈
来源: 互联网 发布时间: 2013-12-24
1,收集Email地址 //index.htm
<html> <body> <form action="/blog_article/index.html" method="post"> 姓 名:<br> <input type="text" name="name" size="20" maxlength="20" value=""><br> Email:<br> <input type="text" name="email" size="20" maxlength="40" value=""><br> <input type="submit" value="确认提交"> </form> </body> </html> //index.php <html> <head> <title>收集Email地址</title> </head> <body> <? print "您好, $name!. 您提交的Email地址为:$email"; ?> </body> </html>
2,反馈表单
<html> <head> <title>反馈表单</title> </head> <body> <form action="/blog_article/feedback.html" method="post"> 用户名:<input type="text" name="username" size="30"> <br><br> Email地址:<input type="text" name="useraddr" size="30"> <br><br> <textarea name="comments" cols="30" rows="5"> </textarea><br> <input type="submit" value="提 交"> </form> </body> </html>
3,接收反馈内容的程序: feedback.php
<?php $username = $_POST['username']; $useraddr = $_POST['useraddr']; $comments = $_POST['comments']; $to = "php@"; $re = "来自网站的用户反馈"; $msg = $comments; $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; $headers .= "From: $useraddr \r\n"; $headers .= "Cc: another@hotmail.com \r\n"; mail( $to, $re, $msg, $headers ); ?> <html> <head> <title>收集反馈内容</title> </head> <body> <h3>感谢您的参与:</h3> 反馈内容来自:<?php echo($username); ?><br> 回复给:<?php echo($useraddr); ?> </body> </html>
[3]php实例:计算表单数据-计算器的代码
来源: 互联网 发布时间: 2013-12-24
1,计算表单中的数据,类似一个小型计算器。
<html> <head> <title>计算表单数据-www.</title> </head> <body> <form action = "calc.php" method = "post"> 值 1: <input type = "text" name = "val1" size = "10"> 值 2: <input type = "text" name = "val2" size = "10"> <br> 计算: <br> <input type = "radio" name = "calc" value = "add"> 加 <input type = "radio" name = "calc" value = "sub"> 减 <input type = "radio" name = "calc" value = "mul"> 乘 <input type = "radio" name = "calc" value = "div"> 除 <hr> <input type = "submit" value = "计算"> <input type = "reset" value = "清除"> </form> </body> </html>
2,php程序文件: calc.php
<html>
<head>
<title>计算结果</title>
</head>
<body>
<?php
$val1 = $_POST['val1'];
$val2 = $_POST['val2'];
$calc = $_POST['calc'];
if( is_numeric( $val1 ) && is_numeric( $val2 ) )
{
if( $calc != null )
{
switch( $calc )
{
case "add" : $result = $val1 + $val2; break;
case "sub" : $result = $val1 - $val2; break;
case "mul" : $result = $val1 * $val2; break;
case "div" : $result = $val1 / $val2; break;
}
echo( "结果为: $result" );
}
}
else
{
echo( "数据无效,请再试一次。" );
}
?>
</body>
</html>最新技术文章: