当前位置: 编程技术>php
本页文章导读:
▪php+ajax做仿百度搜索下拉自动提示框(有实例)
php+mysql+ajax实现百度搜索下拉提示框 主要有3个文件三个文件在同一个目录里 如下图 下面是三个文件的代码 把sql文件导入到mysql数据库里 修改下数据库密码为自己的 记得哦是UTF-8编码 php+mys.........
▪php计算十二星座的函数代码
核心代码: 代码如下: <?php /* * 计算星座的函数 string get_zodiac_sign(string month, string day) * 输入:月份,日期 * 输出:星座名称或者错误信息 */ function get_zodiac_sign($month, $day) { // 检查参数有.........
▪表格展示无限级分类(PHP版)
TreeTable通过对单元格的行合并和列合并实现了无限层级也能较好的展示层级架构。 1.构建ID/PID/NAME的数组,后期可通过数据库生成的动态数据。Tree算法请点击 代码如下: array( * 1 => array('id.........
[1]php+ajax做仿百度搜索下拉自动提示框(有实例)
来源: 互联网 发布时间: 2013-11-30
php+mysql+ajax实现百度搜索下拉提示框 主要有3个文件三个文件在同一个目录里 如下图
下面是三个文件的代码 把sql文件导入到mysql数据库里 修改下数据库密码为自己的 记得哦是UTF-8编码
php+mysql+ajax实现百度搜索下拉提示框
效果图
rpc.php文件
<?php
mysql_connect('localhost', 'root' ,'');
mysql_select_db("test");
$queryString = $_POST['queryString'];
if(strlen($queryString) >0) {
$sql= "SELECT value FROM countries WHERE value LIKE '".$queryString."%' LIMIT 10";
$query = mysql_query($sql);
while ($result = mysql_fetch_array($query,MYSQL_BOTH)){
$value=$result['value'];
echo '<li onClick="fill(\''.$value.'\');">'.$value.'</li>';
}
}
?>
index.htm文件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ajax Auto Suggest</title>
<script type="text/javascript" src="http://www.iiwnet.com/templets/niu/js/jquery.min.js"></script>
<script type="text/javascript">
function lookup(inputString) {
if(inputString.length == 0) {
// Hide the suggestion box.
$('#suggestions').hide();
} else {
$.post("rpc.php", {queryString: ""+inputString+""}, function(data){
if(data.length >0) {
$('#suggestions').show();
$('#autoSuggestionsList').html(data);
}
});
}
} // lookup
function fill(thisValue) {
$('#inputString').val(thisValue);
setTimeout("$('#suggestions').hide();", 200);
}
</script>
<style type="text/css">
body {
font-family: Helvetica;
font-size: 11px;
color: #000;
}
h3 {
margin: 0px;
padding: 0px;
}
.suggestionsBox {
position: relative;
left: 30px;
margin: 10px 0px 0px 0px;
width: 200px;
background-color: #212427;
-moz-border-radius: 7px;
-webkit-border-radius: 7px;
border: 2px solid #000;
color: #fff;
}
.suggestionList {
margin: 0px;
padding: 0px;
}
.suggestionList li {
margin: 0px 0px 3px 0px;
padding: 3px;
cursor: pointer;
}
.suggestionList li:hover {
background-color: #659CD8;
}
</style>
</head>
<body>
<div>
<form>
<div>
Type your county:
<br />
<input type="text" size="30" value="" id="inputString" onkeyup="lookup(this.value);" onblur="fill();" />
</div>
<div id="suggestions" >
<img src="/blog_article/upArrow.png" alt="upArrow" />
<div id="autoSuggestionsList">
</div>
</div>
</form>
</div>
</body>
</html>
sql数据库autoComplete.sql文件(导入到mysql)
-- phpMyAdmin SQL Dump
-- version 3.3.5
-- http://www.phpmyadmin.net
--
-- 主机: localhost
-- 生成日期: 2010 年 12 月 09 日 02:36
-- 服务器版本: 5.0.22
-- PHP 版本: 5.2.14
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--
-- 数据库: `test`
--
-- --------------------------------------------------------
--
-- 表的结构 `countries`
--
CREATE TABLE IF NOT EXISTS `countries` (
`id` int(6) NOT NULL auto_increment,
`value` varchar(250) NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=243 ;
--
-- 转存表中的数据 `countries`
--
INSERT INTO `countries` (`id`, `value`) VALUES
(1, 'Afghanistan'),
(2, 'Aringland Islands'),
(3, 'Albania'),
(4, 'Algeria'),
(5, 'American Samoa'),
(6, 'Andorra'),
(7, 'Angola'),
(8, 'Anguilla'),
(9, 'Antarctica'),
(10, 'Antigua and Barbuda'),
(11, 'Argentina'),
(12, 'Armenia'),
(13, 'Aruba'),
(14, 'Australia'),
(15, 'Austria'),
(16, 'Azerbaijan'),
(17, 'Bahamas'),
(18, 'Bahrain'),
(19, 'Bangladesh'),
(20, 'Barbados'),
(21, 'Belarus'),
(22, 'Belgium'),
(23, 'Belize'),
(24, 'Benin'),
(25, 'Bermuda'),
(26, 'Bhutan'),
(27, 'Bolivia'),
(28, 'Bosnia and Herzegovina'),
(29, 'Botswana'),
(30, 'Bouvet Island'),
(31, 'Brazil'),
(32, 'British Indian Ocean territory'),
(33, 'Brunei Darussalam'),
(34, 'Bulgaria'),
(35, 'Burkina Faso'),
(36, 'Burundi'),
(37, 'Cambodia'),
(38, 'Cameroon'),
(39, 'Canada'),
(40, 'Cape Verde'),
(41, 'Cayman Islands'),
(42, 'Central African Republic'),
(43, 'Chad'),
(44, 'Chile'),
(45, 'China'),
(46, 'Christmas Island'),
(47, 'Cocos (Keeling) Islands'),
(48, 'Colombia'),
(49, 'Comoros'),
(50, 'Congo'),
(51, 'Congo'),
(52, ' Democratic Republic'),
(53, 'Cook Islands'),
(54, 'Costa Rica'),
(55, 'Ivory Coast (Ivory Coast)'),
(56, 'Croatia (Hrvatska)'),
(57, 'Cuba'),
(58, 'Cyprus'),
(59, 'Czech Republic'),
(60, 'Denmark'),
(61, 'Djibouti'),
(62, 'Dominica'),
(63, 'Dominican Republic'),
(64, 'East Timor'),
(65, 'Ecuador'),
(66, 'Egypt'),
(67, 'El Salvador'),
(68, 'Equatorial Guinea'),
(69, 'Eritrea'),
(70, 'Estonia'),
(71, 'Ethiopia'),
(72, 'Falkland Islands'),
(73, 'Faroe Islands'),
(74, 'Fiji'),
(75, 'Finland'),
(76, 'France'),
(77, 'French Guiana'),
(78, 'French Polynesia'),
(79, 'French Southern Territories'),
(80, 'Gabon'),
(81, 'Gambia'),
(82, 'Georgia'),
(83, 'Germany'),
(84, 'Ghana'),
(85, 'Gibraltar'),
(86, 'Greece'),
(87, 'Greenland'),
(88, 'Grenada'),
(89, 'Guadeloupe'),
(90, 'Guam'),
(91, 'Guatemala'),
(92, 'Guinea'),
(93, 'Guinea-Bissau'),
(94, 'Guyana'),
(95, 'Haiti'),
(96, 'Heard and McDonald Islands'),
(97, 'Honduras'),
(98, 'Hong Kong'),
(99, 'Hungary'),
(100, 'Iceland'),
(101, 'India'),
(102, 'Indonesia'),
(103, 'Iran'),
(104, 'Iraq'),
(105, 'Ireland'),
(106, 'Israel'),
(107, 'Italy'),
(108, 'Jamaica'),
(109, 'Japan'),
(110, 'Jordan'),
(111, 'Kazakhstan'),
(112, 'Kenya'),
(113, 'Kiribati'),
(114, 'Korea (north)'),
(115, 'Korea (south)'),
(116, 'Kuwait'),
(117, 'Kyrgyzstan'),
(118, 'Lao People''s Democratic Republic'),
(119, 'Latvia'),
(120, 'Lebanon'),
(121, 'Lesotho'),
(122, 'Liberia'),
(123, 'Libyan Arab Jamahiriya'),
(124, 'Liechtenstein'),
(125, 'Lithuania'),
(126, 'Luxembourg'),
(127, 'Macao'),
(128, 'Macedonia'),
(129, 'Madagascar'),
(130, 'Malawi'),
(131, 'Malaysia'),
(132, 'Maldives'),
(133, 'Mali'),
(134, 'Malta'),
(135, 'Marshall Islands'),
(136, 'Martinique'),
(137, 'Mauritania'),
(138, 'Mauritius'),
(139, 'Mayotte'),
(140, 'Mexico'),
(141, 'Micronesia'),
(142, 'Moldova'),
(143, 'Monaco'),
(144, 'Mongolia'),
(145, 'Montserrat'),
(146, 'Morocco'),
(147, 'Mozambique'),
(148, 'Myanmar'),
(149, 'Namibia'),
(150, 'Nauru'),
(151, 'Nepal'),
(152, 'Netherlands'),
(153, 'Netherlands Antilles'),
(154, 'New Caledonia'),
(155, 'New Zealand'),
(156, 'Nicaragua'),
(157, 'Niger'),
(158, 'Nigeria'),
(159, 'Niue'),
(160, 'Norfolk Island'),
(161, 'Northern Mariana Islands'),
(162, 'Norway'),
(163, 'Oman'),
(164, 'Pakistan'),
(165, 'Palau'),
(166, 'Palestinian Territories'),
(167, 'Panama'),
(168, 'Papua New Guinea'),
(169, 'Paraguay'),
(170, 'Peru'),
(171, 'Philippines'),
(172, 'Pitcairn'),
(173, 'Poland'),
(174, 'Portugal'),
(175, 'Puerto Rico'),
(176, 'Qatar'),
(177, 'Runion'),
(178, 'Romania'),
(179, 'Russian Federation'),
(180, 'Rwanda'),
(181, 'Saint Helena'),
(182, 'Saint Kitts and Nevis'),
(183, 'Saint Lucia'),
(184, 'Saint Pierre and Miquelon'),
(185, 'Saint Vincent and the Grenadines'),
(186, 'Samoa'),
(187, 'San Marino'),
(188, 'Sao Tome and Principe'),
(189, 'Saudi Arabia'),
(190, 'Senegal'),
(191, 'Serbia and Montenegro'),
(192, 'Seychelles'),
(193, 'Sierra Leone'),
(194, 'Singapore'),
(195, 'Slovakia'),
(196, 'Slovenia'),
(197, 'Solomon Islands'),
(198, 'Somalia'),
(199, 'South Africa'),
(200, 'South Georgia and the South Sandwich Islands'),
(201, 'Spain'),
(202, 'Sri Lanka'),
(203, 'Sudan'),
(204, 'Suriname'),
(205, 'Svalbard and Jan Mayen Islands'),
(206, 'Swaziland'),
(207, 'Sweden'),
(208, 'Switzerland'),
(209, 'Syria'),
(210, 'Taiwan'),
(211, 'Tajikistan'),
(212, 'Tanzania'),
(213, 'Thailand'),
(214, 'Togo'),
(215, 'Tokelau'),
(216, 'Tonga'),
(217, 'Trinidad and Tobago'),
(218, 'Tunisia'),
(219, 'Turkey'),
(220, 'Turkmenistan'),
(221, 'Turks and Caicos Islands'),
(222, 'Tuvalu'),
(223, 'Uganda'),
(224, 'Ukraine'),
(225, 'United Arab Emirates'),
(226, 'United Kingdom'),
(227, 'United States of America'),
(228, 'Uruguay'),
(229, 'Uzbekistan'),
(230, 'Vanuatu'),
(231, 'Vatican City'),
(232, 'Venezuela'),
(233, 'Vietnam'),
(234, 'Virgin Islands (British)'),
(235, 'Virgin Islands (US)'),
(236, 'Wallis and Futuna Islands'),
(237, 'Western Sahara'),
(238, 'Yemen'),
(239, 'Zaire'),
(240, 'Zambia'),
(241, 'Zimbabwe');
你在本地服务器测试下 一定很给力 而且原理很简单 没那么麻烦
下面是三个文件的代码 把sql文件导入到mysql数据库里 修改下数据库密码为自己的 记得哦是UTF-8编码
php+mysql+ajax实现百度搜索下拉提示框
效果图
rpc.php文件
代码如下:
<?php
mysql_connect('localhost', 'root' ,'');
mysql_select_db("test");
$queryString = $_POST['queryString'];
if(strlen($queryString) >0) {
$sql= "SELECT value FROM countries WHERE value LIKE '".$queryString."%' LIMIT 10";
$query = mysql_query($sql);
while ($result = mysql_fetch_array($query,MYSQL_BOTH)){
$value=$result['value'];
echo '<li onClick="fill(\''.$value.'\');">'.$value.'</li>';
}
}
?>
index.htm文件
代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ajax Auto Suggest</title>
<script type="text/javascript" src="http://www.iiwnet.com/templets/niu/js/jquery.min.js"></script>
<script type="text/javascript">
function lookup(inputString) {
if(inputString.length == 0) {
// Hide the suggestion box.
$('#suggestions').hide();
} else {
$.post("rpc.php", {queryString: ""+inputString+""}, function(data){
if(data.length >0) {
$('#suggestions').show();
$('#autoSuggestionsList').html(data);
}
});
}
} // lookup
function fill(thisValue) {
$('#inputString').val(thisValue);
setTimeout("$('#suggestions').hide();", 200);
}
</script>
<style type="text/css">
body {
font-family: Helvetica;
font-size: 11px;
color: #000;
}
h3 {
margin: 0px;
padding: 0px;
}
.suggestionsBox {
position: relative;
left: 30px;
margin: 10px 0px 0px 0px;
width: 200px;
background-color: #212427;
-moz-border-radius: 7px;
-webkit-border-radius: 7px;
border: 2px solid #000;
color: #fff;
}
.suggestionList {
margin: 0px;
padding: 0px;
}
.suggestionList li {
margin: 0px 0px 3px 0px;
padding: 3px;
cursor: pointer;
}
.suggestionList li:hover {
background-color: #659CD8;
}
</style>
</head>
<body>
<div>
<form>
<div>
Type your county:
<br />
<input type="text" size="30" value="" id="inputString" onkeyup="lookup(this.value);" onblur="fill();" />
</div>
<div id="suggestions" >
<img src="/blog_article/upArrow.png" alt="upArrow" />
<div id="autoSuggestionsList">
</div>
</div>
</form>
</div>
</body>
</html>
sql数据库autoComplete.sql文件(导入到mysql)
代码如下:
-- phpMyAdmin SQL Dump
-- version 3.3.5
-- http://www.phpmyadmin.net
--
-- 主机: localhost
-- 生成日期: 2010 年 12 月 09 日 02:36
-- 服务器版本: 5.0.22
-- PHP 版本: 5.2.14
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--
-- 数据库: `test`
--
-- --------------------------------------------------------
--
-- 表的结构 `countries`
--
CREATE TABLE IF NOT EXISTS `countries` (
`id` int(6) NOT NULL auto_increment,
`value` varchar(250) NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=243 ;
--
-- 转存表中的数据 `countries`
--
INSERT INTO `countries` (`id`, `value`) VALUES
(1, 'Afghanistan'),
(2, 'Aringland Islands'),
(3, 'Albania'),
(4, 'Algeria'),
(5, 'American Samoa'),
(6, 'Andorra'),
(7, 'Angola'),
(8, 'Anguilla'),
(9, 'Antarctica'),
(10, 'Antigua and Barbuda'),
(11, 'Argentina'),
(12, 'Armenia'),
(13, 'Aruba'),
(14, 'Australia'),
(15, 'Austria'),
(16, 'Azerbaijan'),
(17, 'Bahamas'),
(18, 'Bahrain'),
(19, 'Bangladesh'),
(20, 'Barbados'),
(21, 'Belarus'),
(22, 'Belgium'),
(23, 'Belize'),
(24, 'Benin'),
(25, 'Bermuda'),
(26, 'Bhutan'),
(27, 'Bolivia'),
(28, 'Bosnia and Herzegovina'),
(29, 'Botswana'),
(30, 'Bouvet Island'),
(31, 'Brazil'),
(32, 'British Indian Ocean territory'),
(33, 'Brunei Darussalam'),
(34, 'Bulgaria'),
(35, 'Burkina Faso'),
(36, 'Burundi'),
(37, 'Cambodia'),
(38, 'Cameroon'),
(39, 'Canada'),
(40, 'Cape Verde'),
(41, 'Cayman Islands'),
(42, 'Central African Republic'),
(43, 'Chad'),
(44, 'Chile'),
(45, 'China'),
(46, 'Christmas Island'),
(47, 'Cocos (Keeling) Islands'),
(48, 'Colombia'),
(49, 'Comoros'),
(50, 'Congo'),
(51, 'Congo'),
(52, ' Democratic Republic'),
(53, 'Cook Islands'),
(54, 'Costa Rica'),
(55, 'Ivory Coast (Ivory Coast)'),
(56, 'Croatia (Hrvatska)'),
(57, 'Cuba'),
(58, 'Cyprus'),
(59, 'Czech Republic'),
(60, 'Denmark'),
(61, 'Djibouti'),
(62, 'Dominica'),
(63, 'Dominican Republic'),
(64, 'East Timor'),
(65, 'Ecuador'),
(66, 'Egypt'),
(67, 'El Salvador'),
(68, 'Equatorial Guinea'),
(69, 'Eritrea'),
(70, 'Estonia'),
(71, 'Ethiopia'),
(72, 'Falkland Islands'),
(73, 'Faroe Islands'),
(74, 'Fiji'),
(75, 'Finland'),
(76, 'France'),
(77, 'French Guiana'),
(78, 'French Polynesia'),
(79, 'French Southern Territories'),
(80, 'Gabon'),
(81, 'Gambia'),
(82, 'Georgia'),
(83, 'Germany'),
(84, 'Ghana'),
(85, 'Gibraltar'),
(86, 'Greece'),
(87, 'Greenland'),
(88, 'Grenada'),
(89, 'Guadeloupe'),
(90, 'Guam'),
(91, 'Guatemala'),
(92, 'Guinea'),
(93, 'Guinea-Bissau'),
(94, 'Guyana'),
(95, 'Haiti'),
(96, 'Heard and McDonald Islands'),
(97, 'Honduras'),
(98, 'Hong Kong'),
(99, 'Hungary'),
(100, 'Iceland'),
(101, 'India'),
(102, 'Indonesia'),
(103, 'Iran'),
(104, 'Iraq'),
(105, 'Ireland'),
(106, 'Israel'),
(107, 'Italy'),
(108, 'Jamaica'),
(109, 'Japan'),
(110, 'Jordan'),
(111, 'Kazakhstan'),
(112, 'Kenya'),
(113, 'Kiribati'),
(114, 'Korea (north)'),
(115, 'Korea (south)'),
(116, 'Kuwait'),
(117, 'Kyrgyzstan'),
(118, 'Lao People''s Democratic Republic'),
(119, 'Latvia'),
(120, 'Lebanon'),
(121, 'Lesotho'),
(122, 'Liberia'),
(123, 'Libyan Arab Jamahiriya'),
(124, 'Liechtenstein'),
(125, 'Lithuania'),
(126, 'Luxembourg'),
(127, 'Macao'),
(128, 'Macedonia'),
(129, 'Madagascar'),
(130, 'Malawi'),
(131, 'Malaysia'),
(132, 'Maldives'),
(133, 'Mali'),
(134, 'Malta'),
(135, 'Marshall Islands'),
(136, 'Martinique'),
(137, 'Mauritania'),
(138, 'Mauritius'),
(139, 'Mayotte'),
(140, 'Mexico'),
(141, 'Micronesia'),
(142, 'Moldova'),
(143, 'Monaco'),
(144, 'Mongolia'),
(145, 'Montserrat'),
(146, 'Morocco'),
(147, 'Mozambique'),
(148, 'Myanmar'),
(149, 'Namibia'),
(150, 'Nauru'),
(151, 'Nepal'),
(152, 'Netherlands'),
(153, 'Netherlands Antilles'),
(154, 'New Caledonia'),
(155, 'New Zealand'),
(156, 'Nicaragua'),
(157, 'Niger'),
(158, 'Nigeria'),
(159, 'Niue'),
(160, 'Norfolk Island'),
(161, 'Northern Mariana Islands'),
(162, 'Norway'),
(163, 'Oman'),
(164, 'Pakistan'),
(165, 'Palau'),
(166, 'Palestinian Territories'),
(167, 'Panama'),
(168, 'Papua New Guinea'),
(169, 'Paraguay'),
(170, 'Peru'),
(171, 'Philippines'),
(172, 'Pitcairn'),
(173, 'Poland'),
(174, 'Portugal'),
(175, 'Puerto Rico'),
(176, 'Qatar'),
(177, 'Runion'),
(178, 'Romania'),
(179, 'Russian Federation'),
(180, 'Rwanda'),
(181, 'Saint Helena'),
(182, 'Saint Kitts and Nevis'),
(183, 'Saint Lucia'),
(184, 'Saint Pierre and Miquelon'),
(185, 'Saint Vincent and the Grenadines'),
(186, 'Samoa'),
(187, 'San Marino'),
(188, 'Sao Tome and Principe'),
(189, 'Saudi Arabia'),
(190, 'Senegal'),
(191, 'Serbia and Montenegro'),
(192, 'Seychelles'),
(193, 'Sierra Leone'),
(194, 'Singapore'),
(195, 'Slovakia'),
(196, 'Slovenia'),
(197, 'Solomon Islands'),
(198, 'Somalia'),
(199, 'South Africa'),
(200, 'South Georgia and the South Sandwich Islands'),
(201, 'Spain'),
(202, 'Sri Lanka'),
(203, 'Sudan'),
(204, 'Suriname'),
(205, 'Svalbard and Jan Mayen Islands'),
(206, 'Swaziland'),
(207, 'Sweden'),
(208, 'Switzerland'),
(209, 'Syria'),
(210, 'Taiwan'),
(211, 'Tajikistan'),
(212, 'Tanzania'),
(213, 'Thailand'),
(214, 'Togo'),
(215, 'Tokelau'),
(216, 'Tonga'),
(217, 'Trinidad and Tobago'),
(218, 'Tunisia'),
(219, 'Turkey'),
(220, 'Turkmenistan'),
(221, 'Turks and Caicos Islands'),
(222, 'Tuvalu'),
(223, 'Uganda'),
(224, 'Ukraine'),
(225, 'United Arab Emirates'),
(226, 'United Kingdom'),
(227, 'United States of America'),
(228, 'Uruguay'),
(229, 'Uzbekistan'),
(230, 'Vanuatu'),
(231, 'Vatican City'),
(232, 'Venezuela'),
(233, 'Vietnam'),
(234, 'Virgin Islands (British)'),
(235, 'Virgin Islands (US)'),
(236, 'Wallis and Futuna Islands'),
(237, 'Western Sahara'),
(238, 'Yemen'),
(239, 'Zaire'),
(240, 'Zambia'),
(241, 'Zimbabwe');
你在本地服务器测试下 一定很给力 而且原理很简单 没那么麻烦
[2]php计算十二星座的函数代码
来源: 互联网 发布时间: 2013-11-30
核心代码:
<?php
/*
* 计算星座的函数 string get_zodiac_sign(string month, string day)
* 输入:月份,日期
* 输出:星座名称或者错误信息
*/
function get_zodiac_sign($month, $day)
{
// 检查参数有效性
if ($month < 1 || $month > 12 || $day < 1 || $day > 31)
return (false);
// 星座名称以及开始日期
$signs = array(
array( "20" => "宝瓶座"),
array( "19" => "双鱼座"),
array( "21" => "白羊座"),
array( "20" => "金牛座"),
array( "21" => "双子座"),
array( "22" => "巨蟹座"),
array( "23" => "狮子座"),
array( "23" => "处女座"),
array( "23" => "天秤座"),
array( "24" => "天蝎座"),
array( "22" => "射手座"),
array( "22" => "摩羯座")
);
list($sign_start, $sign_name) = each($signs[(int)$month-1]);
if ($day < $sign_start)
list($sign_start, $sign_name) = each($signs[($month -2 < 0) ? $month = 11: $month -= 2]);
return $sign_name;
}//函数结束
?>
代码如下:
<?php
/*
* 计算星座的函数 string get_zodiac_sign(string month, string day)
* 输入:月份,日期
* 输出:星座名称或者错误信息
*/
function get_zodiac_sign($month, $day)
{
// 检查参数有效性
if ($month < 1 || $month > 12 || $day < 1 || $day > 31)
return (false);
// 星座名称以及开始日期
$signs = array(
array( "20" => "宝瓶座"),
array( "19" => "双鱼座"),
array( "21" => "白羊座"),
array( "20" => "金牛座"),
array( "21" => "双子座"),
array( "22" => "巨蟹座"),
array( "23" => "狮子座"),
array( "23" => "处女座"),
array( "23" => "天秤座"),
array( "24" => "天蝎座"),
array( "22" => "射手座"),
array( "22" => "摩羯座")
);
list($sign_start, $sign_name) = each($signs[(int)$month-1]);
if ($day < $sign_start)
list($sign_start, $sign_name) = each($signs[($month -2 < 0) ? $month = 11: $month -= 2]);
return $sign_name;
}//函数结束
?>
[3]表格展示无限级分类(PHP版)
来源: 互联网 发布时间: 2013-11-30
TreeTable通过对单元格的行合并和列合并实现了无限层级也能较好的展示层级架构。
1.构建ID/PID/NAME的数组,后期可通过数据库生成的动态数据。Tree算法请点击
array(
* 1 => array('id'=>'1','parentid'=>0,'name'=>'一级栏目一'),
* 2 => array('id'=>'2','parentid'=>0,'name'=>'一级栏目二'),
* 3 => array('id'=>'3','parentid'=>1,'name'=>'二级栏目一'),
* 4 => array('id'=>'4','parentid'=>1,'name'=>'二级栏目二'),
* 5 => array('id'=>'5','parentid'=>2,'name'=>'二级栏目三'),
* 6 => array('id'=>'6','parentid'=>3,'name'=>'三级栏目一'),
* 7 => array('id'=>'7','parentid'=>3,'name'=>'三级栏目二')
* )
2. 导入TreeTable类库。
import('@.ORG.Util.TableTree'); //Thinkphp导入方法
3. 生成TreeTable HTML代码
$treeTable->init($treearr);
echo $treeTable->get_treetable();
注意:get_treetable()只生产表体部门,<TALBE></TABLE>请自行构建。
完整代码
<?php
/**
* File name: TreeTable.class.php
* Author: run.gao 312854458@qq.com Date: 2012-07-24 23:22 GMT+8
* Description: 通用的表格无限级分类
* */
/**
* 表格展示无限分类是将无线分类已表格的形式表现出来,更好的能体现出分类的所属关系
* 使用方法:
* 1. 实例化分类
* $treeTable = new TreeTable();
* 2. 初始化分类,$treearr必须是一个多维数组且包含 id,parentid,name字段
* $treeTable->init($treearr);
* 3. 获取无限分类HTML代码
* echo $treeTable->get_treetable();
* */
class TreeTable {
/**
* 生成树型结构所需要的2维数组
* @var array
*/
public $arr = array();
/**
* 表格列数
* @var int
*/
public $columns = 0;
/**
* 表格行数
* @var int
*/
public $rows = 0;
/**
* 初始化TreeTable数据
* @param array 2维数组
* array(
* 1 => array('id'=>'1','parentid'=>0,'name'=>'一级栏目一'),
* 2 => array('id'=>'2','parentid'=>0,'name'=>'一级栏目二'),
* 3 => array('id'=>'3','parentid'=>1,'name'=>'二级栏目一'),
* 4 => array('id'=>'4','parentid'=>1,'name'=>'二级栏目二'),
* 5 => array('id'=>'5','parentid'=>2,'name'=>'二级栏目三'),
* 6 => array('id'=>'6','parentid'=>3,'name'=>'三级栏目一'),
* 7 => array('id'=>'7','parentid'=>3,'name'=>'三级栏目二')
* )
*/
public function init($arr=array()){
if(!is_array($arr)) return false;
foreach ($arr as $k=>$v) {
$this->arr[$v['id']] = $v;
}
foreach ($this->arr as $k => $v){
$this->arr[$k]['column'] = $this->get_level($v['id']); // Y轴位置
$this->arr[$k]['arrchildid'] = $this->get_arrchildid($v['id']); // 所有子节点
$this->arr[$k]['arrparentid'] = $this->get_arrparentid($v['id']); // 所有父节点
$this->arr[$k]['child_bottom_num'] = $this->get_child_count($v['id']); // 所有底层元素节点
}
$this->columns = $this->get_columns(); // 总行数
$this->rows = $this->get_rows(); // 总列数
// 按照arrparentid和id号进行排序
$this->sort_arr();
foreach ($this->arr as $k => $v){
$this->arr[$k]['row'] = $this->get_row_location($v['id']); // X轴位置
$this->arr[$k]['rowspan'] = $v['child_bottom_num']; // 行合并数
$this->arr[$k]['colspan'] = $v['child_bottom_num'] == 0 ? $this->columns - $v['column'] + 1 : 0; //列合并数
}
return $this->get_tree_arr();
}
/**
* 获取数组
* */
public function get_tree_arr(){
return is_array($this->arr) ? $this->arr : false;
}
/**
* 按arrparentid/id号依次重新排序数组
* */
public function sort_arr(){
// 要进行排序的字段
foreach ($this->arr as $k => $v){
$order_pid_arr[$k] = $v['arrparentid'];
$order_iscost[] = $v['sort'];
$order_id_arr[$k] = $v['id'];
}
// 先根据arrparentid排序,再根据排序,id号排序
array_multisort(
$order_pid_arr, SORT_ASC, SORT_STRING,
$order_iscost, SORT_DESC, SORT_NUMERIC,
$order_id_arr, SORT_ASC, SORT_NUMERIC,
$this->arr);
// 获取每一个节点层次
for ($column = 1; $column <= $this->columns; $column++) {
$row_level = 0;
foreach ($this->arr as $key => $node){
if ($node['column'] == $column){
$row_level++;
$this->arr[$key]['column_level'] = $row_level;
}
}
}
// 重新计算以ID作为键名
foreach ($this->arr as $k=>$v) {
$arr[$v['id']] = $v;
}
$this->arr = $arr;
}
/**
* 得到父级数组
* @param int
* @return array
*/
public function get_parent($myid){
$newarr = array();
if(!isset($this->arr[$myid])) return false;
$pid = $this->arr[$myid]['parentid'];
$pid = $this->arr[$pid]['parentid'];
if(is_array($this->arr)){
foreach($this->arr as $id => $a){
if($a['parentid'] == $pid) $newarr[$id] = $a;
}
}
return $newarr;
}
/**
* 得到子级数组
* @param int
* @return array
*/
public function get_child($myid){
$a = $newarr = array();
if(is_array($this->arr)){
foreach($this->arr as $id => $a){
if($a['parentid'] == $myid) $newarr[$id] = $a;
}
}
return $newarr ? $newarr : false;
}
/**
* 获取当前节点所在的层级
* @param $myid 当前节点ID号
* */
public function get_level($myid, $init = true){
static $level = 1;
if($init) $level = 1;
if ($this->arr[$myid]['parentid']) {
$level++;
$this->get_level($this->arr[$myid]['parentid'], false);
}
return $level;
}
/**
* 获取当前节点所有底层节点(没有子节点的节点)的数量
* @param $myid 节点ID号
* @param $init 第一次加载将情况static变量
* */
public function get_child_count($myid, $init = true){
static $count = 0;
if($init) $count = 0;
if(!$this->get_child($myid) && $init) return 0;
if($childarr = $this->get_child($myid)){
foreach ($childarr as $v){
$this->get_child_count($v['id'], false);
}
}else{
$count++;
}
return $count;
}
/**
* 获取节点所有子节点ID号
* @param $catid 节点ID号
* @param $init 第一次加载将情况static初始化
* */
public function get_arrchildid($myid, $init = true) {
static $childid;
if($init) $childid = '';
if(!is_array($this->arr)) return false;
foreach($this->arr as $id => $a){
if($a['parentid'] == $myid) {
$childid = $childid ? $childid.','.$a['id'] : $a['id'];
$this->get_arrchildid($a['id'], false);
}
}
return $childid ;
}
/**
* 获取该节点所有父节点ID号
* @param $id 节点ID号
* */
public function get_arrparentid($id, $arrparentid = '') {
if(!is_array($this->arr)) return false;
$parentid = $this->arr[$id]['parentid'];
if($parentid > 0) $arrparentid = $arrparentid ? $parentid.','.$arrparentid : $parentid;
if($parentid) $arrparentid = $this->get_arrparentid($parentid, $arrparentid);
return $arrparentid;
}
/**
* 获取节点所在地行定位
* @param $myid 节点ID号
*/
public function get_row_location($myid){
$nodearr = $this->arr;
// 获取每一个节点所在行的位置
foreach ($nodearr as $key => $node){
if($myid == $node['id']) {
$node_row_count = 0;
$arrparentid = explode(',', $node['arrparentid']);
// 所有父节点小于当前节点层次的底层节点等于0的元素
foreach ($arrparentid as $pid){
foreach ($nodearr as $node_row){
if($node_row['column'] == $nodearr[$pid]['column'] && $nodearr[$pid]['column_level'] > $node_row['column_level'] && $node_row['child_bottom_num'] == 0){
$node_row_count ++;
}
}
}
// 所有当前节点并且节点层次(rowid_level)小于当前节点层次的个数
foreach ($nodearr as $node_row){
if($node['column'] == $node_row['column'] && $node_row['column_level'] < $node['column_level']){
$node_row_count += $node_row['child_bottom_num'] ? $node_row['child_bottom_num'] : 1;
}
}
$node_row_count++;
break;
}
}
return $node_row_count;
}
/**
* 获取表格的行数
* */
public function get_rows(){
$row = 0;
foreach ($this->arr as $key => $node){
if($node['child_bottom_num'] == 0){
$rows++; // 总行数
}
}
return $rows;
}
/**
* 获取表格的列数
* */
public function get_columns(){
$columns = 0 ;
foreach ($this->arr as $key => $node){
if($node['column'] > $columns){
$columns = $node['column']; // 总列数
}
}
return $columns;
}
/**
* 获取分类的表格展现形式(不包含表头)
* */
public function get_treetable(){
$table_string = '';
for($row = 1; $row <= $this->rows; $row++){
$table_string .= "\r\t<tr>";
foreach ($this->arr as $v){
if($v['row'] == $row){
$rowspan = $v['rowspan'] ? "rowspan='{$v['rowspan']}'" : '';
$colspan = $v['colspan'] ? "colspan='{$v['colspan']}'" : '';
$table_string .= "\r\t\t<td {$rowspan} {$colspan}>
{$v['name']}
</td>";
}
}
$table_string .= "\r\t</tr>";
}
return $table_string;
}
}
?>
1.构建ID/PID/NAME的数组,后期可通过数据库生成的动态数据。Tree算法请点击
代码如下:
array(
* 1 => array('id'=>'1','parentid'=>0,'name'=>'一级栏目一'),
* 2 => array('id'=>'2','parentid'=>0,'name'=>'一级栏目二'),
* 3 => array('id'=>'3','parentid'=>1,'name'=>'二级栏目一'),
* 4 => array('id'=>'4','parentid'=>1,'name'=>'二级栏目二'),
* 5 => array('id'=>'5','parentid'=>2,'name'=>'二级栏目三'),
* 6 => array('id'=>'6','parentid'=>3,'name'=>'三级栏目一'),
* 7 => array('id'=>'7','parentid'=>3,'name'=>'三级栏目二')
* )
2. 导入TreeTable类库。
代码如下:
import('@.ORG.Util.TableTree'); //Thinkphp导入方法
3. 生成TreeTable HTML代码
代码如下:
$treeTable->init($treearr);
echo $treeTable->get_treetable();
注意:get_treetable()只生产表体部门,<TALBE></TABLE>请自行构建。
完整代码
代码如下:
<?php
/**
* File name: TreeTable.class.php
* Author: run.gao 312854458@qq.com Date: 2012-07-24 23:22 GMT+8
* Description: 通用的表格无限级分类
* */
/**
* 表格展示无限分类是将无线分类已表格的形式表现出来,更好的能体现出分类的所属关系
* 使用方法:
* 1. 实例化分类
* $treeTable = new TreeTable();
* 2. 初始化分类,$treearr必须是一个多维数组且包含 id,parentid,name字段
* $treeTable->init($treearr);
* 3. 获取无限分类HTML代码
* echo $treeTable->get_treetable();
* */
class TreeTable {
/**
* 生成树型结构所需要的2维数组
* @var array
*/
public $arr = array();
/**
* 表格列数
* @var int
*/
public $columns = 0;
/**
* 表格行数
* @var int
*/
public $rows = 0;
/**
* 初始化TreeTable数据
* @param array 2维数组
* array(
* 1 => array('id'=>'1','parentid'=>0,'name'=>'一级栏目一'),
* 2 => array('id'=>'2','parentid'=>0,'name'=>'一级栏目二'),
* 3 => array('id'=>'3','parentid'=>1,'name'=>'二级栏目一'),
* 4 => array('id'=>'4','parentid'=>1,'name'=>'二级栏目二'),
* 5 => array('id'=>'5','parentid'=>2,'name'=>'二级栏目三'),
* 6 => array('id'=>'6','parentid'=>3,'name'=>'三级栏目一'),
* 7 => array('id'=>'7','parentid'=>3,'name'=>'三级栏目二')
* )
*/
public function init($arr=array()){
if(!is_array($arr)) return false;
foreach ($arr as $k=>$v) {
$this->arr[$v['id']] = $v;
}
foreach ($this->arr as $k => $v){
$this->arr[$k]['column'] = $this->get_level($v['id']); // Y轴位置
$this->arr[$k]['arrchildid'] = $this->get_arrchildid($v['id']); // 所有子节点
$this->arr[$k]['arrparentid'] = $this->get_arrparentid($v['id']); // 所有父节点
$this->arr[$k]['child_bottom_num'] = $this->get_child_count($v['id']); // 所有底层元素节点
}
$this->columns = $this->get_columns(); // 总行数
$this->rows = $this->get_rows(); // 总列数
// 按照arrparentid和id号进行排序
$this->sort_arr();
foreach ($this->arr as $k => $v){
$this->arr[$k]['row'] = $this->get_row_location($v['id']); // X轴位置
$this->arr[$k]['rowspan'] = $v['child_bottom_num']; // 行合并数
$this->arr[$k]['colspan'] = $v['child_bottom_num'] == 0 ? $this->columns - $v['column'] + 1 : 0; //列合并数
}
return $this->get_tree_arr();
}
/**
* 获取数组
* */
public function get_tree_arr(){
return is_array($this->arr) ? $this->arr : false;
}
/**
* 按arrparentid/id号依次重新排序数组
* */
public function sort_arr(){
// 要进行排序的字段
foreach ($this->arr as $k => $v){
$order_pid_arr[$k] = $v['arrparentid'];
$order_iscost[] = $v['sort'];
$order_id_arr[$k] = $v['id'];
}
// 先根据arrparentid排序,再根据排序,id号排序
array_multisort(
$order_pid_arr, SORT_ASC, SORT_STRING,
$order_iscost, SORT_DESC, SORT_NUMERIC,
$order_id_arr, SORT_ASC, SORT_NUMERIC,
$this->arr);
// 获取每一个节点层次
for ($column = 1; $column <= $this->columns; $column++) {
$row_level = 0;
foreach ($this->arr as $key => $node){
if ($node['column'] == $column){
$row_level++;
$this->arr[$key]['column_level'] = $row_level;
}
}
}
// 重新计算以ID作为键名
foreach ($this->arr as $k=>$v) {
$arr[$v['id']] = $v;
}
$this->arr = $arr;
}
/**
* 得到父级数组
* @param int
* @return array
*/
public function get_parent($myid){
$newarr = array();
if(!isset($this->arr[$myid])) return false;
$pid = $this->arr[$myid]['parentid'];
$pid = $this->arr[$pid]['parentid'];
if(is_array($this->arr)){
foreach($this->arr as $id => $a){
if($a['parentid'] == $pid) $newarr[$id] = $a;
}
}
return $newarr;
}
/**
* 得到子级数组
* @param int
* @return array
*/
public function get_child($myid){
$a = $newarr = array();
if(is_array($this->arr)){
foreach($this->arr as $id => $a){
if($a['parentid'] == $myid) $newarr[$id] = $a;
}
}
return $newarr ? $newarr : false;
}
/**
* 获取当前节点所在的层级
* @param $myid 当前节点ID号
* */
public function get_level($myid, $init = true){
static $level = 1;
if($init) $level = 1;
if ($this->arr[$myid]['parentid']) {
$level++;
$this->get_level($this->arr[$myid]['parentid'], false);
}
return $level;
}
/**
* 获取当前节点所有底层节点(没有子节点的节点)的数量
* @param $myid 节点ID号
* @param $init 第一次加载将情况static变量
* */
public function get_child_count($myid, $init = true){
static $count = 0;
if($init) $count = 0;
if(!$this->get_child($myid) && $init) return 0;
if($childarr = $this->get_child($myid)){
foreach ($childarr as $v){
$this->get_child_count($v['id'], false);
}
}else{
$count++;
}
return $count;
}
/**
* 获取节点所有子节点ID号
* @param $catid 节点ID号
* @param $init 第一次加载将情况static初始化
* */
public function get_arrchildid($myid, $init = true) {
static $childid;
if($init) $childid = '';
if(!is_array($this->arr)) return false;
foreach($this->arr as $id => $a){
if($a['parentid'] == $myid) {
$childid = $childid ? $childid.','.$a['id'] : $a['id'];
$this->get_arrchildid($a['id'], false);
}
}
return $childid ;
}
/**
* 获取该节点所有父节点ID号
* @param $id 节点ID号
* */
public function get_arrparentid($id, $arrparentid = '') {
if(!is_array($this->arr)) return false;
$parentid = $this->arr[$id]['parentid'];
if($parentid > 0) $arrparentid = $arrparentid ? $parentid.','.$arrparentid : $parentid;
if($parentid) $arrparentid = $this->get_arrparentid($parentid, $arrparentid);
return $arrparentid;
}
/**
* 获取节点所在地行定位
* @param $myid 节点ID号
*/
public function get_row_location($myid){
$nodearr = $this->arr;
// 获取每一个节点所在行的位置
foreach ($nodearr as $key => $node){
if($myid == $node['id']) {
$node_row_count = 0;
$arrparentid = explode(',', $node['arrparentid']);
// 所有父节点小于当前节点层次的底层节点等于0的元素
foreach ($arrparentid as $pid){
foreach ($nodearr as $node_row){
if($node_row['column'] == $nodearr[$pid]['column'] && $nodearr[$pid]['column_level'] > $node_row['column_level'] && $node_row['child_bottom_num'] == 0){
$node_row_count ++;
}
}
}
// 所有当前节点并且节点层次(rowid_level)小于当前节点层次的个数
foreach ($nodearr as $node_row){
if($node['column'] == $node_row['column'] && $node_row['column_level'] < $node['column_level']){
$node_row_count += $node_row['child_bottom_num'] ? $node_row['child_bottom_num'] : 1;
}
}
$node_row_count++;
break;
}
}
return $node_row_count;
}
/**
* 获取表格的行数
* */
public function get_rows(){
$row = 0;
foreach ($this->arr as $key => $node){
if($node['child_bottom_num'] == 0){
$rows++; // 总行数
}
}
return $rows;
}
/**
* 获取表格的列数
* */
public function get_columns(){
$columns = 0 ;
foreach ($this->arr as $key => $node){
if($node['column'] > $columns){
$columns = $node['column']; // 总列数
}
}
return $columns;
}
/**
* 获取分类的表格展现形式(不包含表头)
* */
public function get_treetable(){
$table_string = '';
for($row = 1; $row <= $this->rows; $row++){
$table_string .= "\r\t<tr>";
foreach ($this->arr as $v){
if($v['row'] == $row){
$rowspan = $v['rowspan'] ? "rowspan='{$v['rowspan']}'" : '';
$colspan = $v['colspan'] ? "colspan='{$v['colspan']}'" : '';
$table_string .= "\r\t\t<td {$rowspan} {$colspan}>
{$v['name']}
</td>";
}
}
$table_string .= "\r\t</tr>";
}
return $table_string;
}
}
?>
最新技术文章: