当前位置导航:炫浪网>>网络学院>>编程开发>>JAVA教程>>Java进阶

投票系统制作基本模式


  功能介绍:

可以随机产生一个投票主题,

其中很多部分尚待扩展!希望有时间的与我交流 !

首先创建数据库:

CREATE TABLE voteadmin (

id int(10) NOT NULL auto_increment,

name varchar(200) NOT NULL default '0',

select1 varchar(200) NOT NULL default '0',

select2 varchar(200) NOT NULL default '0',

select3 varchar(200) NOT NULL default '0',

select4 varchar(200) NOT NULL default '0',

total int(100) NOT NULL default '0',

data1 int(100) NOT NULL default '0',

data2 int(100) NOT NULL default '0',

data3 int(100) NOT NULL default '0',

data4 int(100) NOT NULL default '0',

UNIQUE KEY name (name),

UNIQUE KEY id (id)

) TYPE=MyISAM;

index.php

$id=rand("1","5");

require_once("mysqlconn.php");

$db=mysql_select_db("vote");

$sql="select * from voteadmin where (id='$id')";

$query=mysql_query($sql);

$array=mysql_fetch_array($query) or die ("数据出错原因:数据未查找到!");

require_once("mysql_to_tpl.php");

mysql_to_tpl("template/01/index.html",$array);

?>

refrese.php

if($id)

{

if($id!="{id}")

{

if($action)

{

require_once("mysqlconn.php");

$db=mysql_select_db("vote");

$sql="UPDATE voteadmin SET $action=$action+1,total=total+1 where (id='$id')";

$array=mysql_query($sql) or die ("数据出错原因:数据未查找到!");

echo "投票成功!看看结果吧!";

echo "";

}

else

{

echo "出错了!";

}

}

else

{

echo "出错了!";

}

}

else

{

echo "出错了!";

}

?>

showvote.php

if($id)

{

require_once("mysqlconn.php");

$db=mysql_select_db("vote");

$sql="select * from voteadmin where(id='$id')";

$query=mysql_query($sql);

$array=mysql_fetch_array($query) or die ("数据出错原因:数据未查找到!");

require_once("mysql_to_tpl.php");

mysql_to_tpl("template/01/show.html",$array);

echo "";

}

else

{

echo "请您按照正确的浏览顺序执行该程序!3ks!";

}

?>

补充:

tpl.php为一个模板类

//国内完整的phplib中template类很少出现,无意中在瑞典的一个网站上找到了一个老外分离的类

//在为djlook编写代码的时候,我总是有灵感从脑子里面跳出来,代码越来越简练

//感谢支持我的djlook! crain Tuesday March 18, 2003 10:11

class Template

{

var $debug = false;

var $file = array();

var $root = "";

var $_varKeys = array();

var $_varVals = array();

var $unknowns = "remove";

var $haltOnError = "yes";

var $_lastError = "";

function Template($root = ".", $unknowns = "remove")

{

$this->setRoot($root);

$this->setUnknowns($unknowns);

}

function setRoot($root)

{

if (!is_dir($root)) {

$this->halt("setRoot: $root is not a directory.");

return false;

}

$this->root = $root;

return true;

}

function setUnknowns($unknowns = "keep")

{

$this->unknowns = $unknowns;

}

function setfile($handle, $filename = "")

{

if (!is_array($handle)) {

if ($filename == "") {

$this->halt("setFile: For handle $handle filename is empty.");

return false;

}

$this->file[$handle] = $this->_filename($filename);

} else {

reset($handle);

while (list($h, $f) = each($handle)) {

$this->file[$h] = $this->_filename($f);

}

}

}

function setBlock($parent, $handle, $name = "")

{

if (!$this->_loadFile($parent)) {

$this->halt("subst: unable to load $parent.");

return false;

}

if ($name == "") {

$name = $handle;

}

$str = $this->getVar($parent);

$reg = "/(.*)\n\s*/sm";

preg_match_all($reg, $str, $m);

$str = preg_replace($reg, "{" . "$name}", $str);

$this->setVar($handle, $m[1][0]);

$this->setVar($parent, $str);

}

function setvar($varname, $value = "")

{

if (!is_array($varname)) {

if (!empty($varname))

if ($this->debug) print "scalar: set *$varname* to *$value*
\n";

$this->_varKeys[$varname] = "/".$this->_varname($varname)."/";

$this->_varVals[$varname] = $value;

} else {

reset($varname);

while (list($k, $v) = each($varname)) {

if (!empty($k))

if ($this->debug) print "array: set *$k* to *$v*
\n";

$this->_varKeys[$k] = "/".$this->_varname($k)."/";

$this->_varVals[$k] = $v;

}

}

}

function subst($handle)

{

if (!$this->_loadFile($handle)) {

$this->halt("subst: unable to load $handle.");

return false;

}

$str = $this->getVar($handle);

$str = @preg_replace($this->_varKeys, $this->_varVals, $str);

return $str;

}

function pSubst($handle)

{

print $this->subst($handle);

return false;

}

function parse($target, $handle, $append = false)

{

if (!is_array($handle)) {

$str = $this->subst($handle);

if ($append) {

$this->setVar($target, $this->getVar($target) . $str);

} else {

$this->setVar($target, $str);

}

} else {

reset($handle);

while (list($i, $h) = each($handle))

相关内容
赞助商链接