随机数生成概率一解(2 位领导批示)

比较常见的方面是把随机数生成为一个数组. 不过随机数比较多的情况下产生的数组也会特别大.

下面是另一种算法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$array = array(1 => 30, 2 => 20, 3 => 20, 4 => 15, 5=> 10 , 6 => 5);
$rand = mt_rand();
$rand = $rand % 101;
$max = 0;
$v = 0;
foreach($array as $key => $value)
{
    $max += $value;
    if($rand <= $max)
    {
        $v = $key;
        break;
    }
}
echo $v;

PHP 求出3个坐标间的角度(0 位领导批示)

原题地址: http://bbs.blueidea.com/viewthread.php?tid=2982704

先计算 AC 的弧度, 然后将弧度转换为角度.

1
2
3
4
5
6
<?php
$a = array('x' => 50, 'y' => 60);
$b = array('x' => 80, 'y' => 60);
$c = array('x' => 80, 'y' => 70);
echo rad2deg(atan2($c['y'] - $a['y'], $c['x'] - $a['x']));
?>
Tags : ,

[备忘]MySQL记录货币建议使用DECIMAL类型(2 位领导批示)

DECIMAL和NUMERIC其实是视为相同的类型. 值是作为字符串存储, 而不是作为二进制浮点数. 这样就避免了FLOAT类型计算时出现的错误.

[备忘]一个常用分页类(0 位领导批示)

工作经常会用到的一个分页类..记录一下.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
Example: 
 
$page = (isset($this->params['named']['page'])) ? $this->params['named']['page'] : 1;
// 导入分页类
App::import('Vendor', 'page_class');
// 获取公告总数
$total = $this->Bulletin->getBulletinTotal();
$perpage = ITEMPREPAGE;
$offset = $perpage * ($page - 1);
$pagebar = new Pagebar($total, $page, $offset, $perpage);
$pagenav = $pagebar->construct_page_nav($total, $page, SYSTEMURL . '/bulletins/index', null, null);
*/
 
<?php
class Pagebar
{
 
    var $perpage = 20;
	/**
	* Ensures that the variables for a multi-page display are sane
	*
	* @param	integer	Total number of items to be displayed
	* @param	integer	(ref) Current page number
	* @param	integer	(ref) Desired number of results to show per-page
	*/
	function Pagebar($numresults, &$page, &$limitlower, $perpage)
	{
		$this->perpage = $perpage;
 
		$numpages = ceil($numresults / $this->perpage);
		if ($numpages == 0)
		{
			$numpages = 1;
		}
 
		if ($page < 1)
		{
			$page = 1;
		}
		else if ($page > $numpages)
		{
			$page = $numpages;
		}
 
		$limitlower = ($page - 1) * $this->perpage;
		$limitupper = ($page) * $this->perpage;
 
		if ($limitupper > $numresults)
		{
			$limitupper = $numresults;
			if ($limitlower > $numresults)
			{
				$limitlower = ($numresults - $this->perpage) - 1;
			}
		}
		if ($limitlower < 0)
		{
			$limitlower = 0;
		}
	}
 
	/**
	* Returns the HTML for multi-page navigation
	*
	* @param	integer	Total number of items found
	* @param	integer	Page number being displayed
	* @param	string	Base address for links eg: q.php?t=99{&page=4}
	* @param	string	Ending portion of address for links
	*
	* @return	string	Page navigation HTML
	*/
	function construct_page_nav($results, $pagenumber, $address, $address2 = '', $option = array())
	{
		$perpage = $this->perpage;
 
		$show = array();
		$curpage = 0;
		$pagenav = '';
		$firstlink = '';
		$prevlink = '';
		$lastlink = '';
		$nextlink = '';
 
		if ($results <= $perpage)
		{
			$show['pagenav'] = false;
			return '';
		}
 
		$show['pagenav'] = true;
 
		$total = number_format($results);
		$totalpages = ceil($results / $perpage);
 
		$show['prev'] = false;
		$show['next'] = false;
		$show['first'] = false;
		$show['last'] = false;
 
		if ($pagenumber > 1)
		{
			$prevpage = $pagenumber - 1;
			$prevnumbers = $this->fetch_start_end_total_array($prevpage, $perpage, $results);
			$show['prev'] = true;
		}
		if ($pagenumber < $totalpages)
		{
			$nextpage = $pagenumber + 1;
			$nextnumbers = $this->fetch_start_end_total_array($nextpage, $perpage, $results);
			$show['next'] = true;
		}
 
		// create array of possible relative links that we might have (eg. +10, +20, +50, etc.)
		$pagenavsarr = array(10, 50, 100, 500, 1000);
 
		$pagenavpages = 5;
 
		while ($curpage++ < $totalpages)
		{
			if (abs($curpage - $pagenumber) >= $pagenavpages AND $pagenavpages != 0)
			{
				if ($curpage == 1)
				{
					$firstnumbers = $this->fetch_start_end_total_array(1, $perpage, $results);
					$show['first'] = true;
				}
				if ($curpage == $totalpages)
				{
					$lastnumbers = $this->fetch_start_end_total_array($totalpages, $perpage, $results);
					$show['last'] = true;
				}
				// generate relative links (eg. +10,etc).
				if (in_array(abs($curpage - $pagenumber), $pagenavsarr) AND $curpage != 1 AND $curpage != $totalpages)
				{
					$pagenumbers = $this->fetch_start_end_total_array($curpage, $perpage, $results);
					$relpage = $curpage - $pagenumber;
 
					if ($relpage > 0)
					{
						$relpage = '+' . $relpage;
					}
 
					$pagenav .= "<a onclick=\"$option[onclick]\" class=\"smallfont\" href=\"$address". "/page:$curpage". ($address2 ? '?'.$address2 : '') . "\" title=\"显示结果从 $pagenumbers[first]$pagenumbers[last] 共计 $total\"><!--$relpage--> $curpage </a>";
				}
			}
			else
			{
				if ($curpage == $pagenumber)
				{
					$numbers = $this->fetch_start_end_total_array($curpage, $perpage, $results);
					$pagenav .= "	<span class=\"smallfont\" title=\"显示结果从 $numbers[first]$numbers[last] 共计 $total\"><strong> $curpage </strong></span>";
				}
				else
				{
					$pagenumbers = $this->fetch_start_end_total_array($curpage, $perpage, $results);
					$pagenav .= "<a onclick=\"$option[onclick]\" class=\"smallfont\" href=\"$address". "/page:$curpage". (($address2) ? '?'.$address2 : '') . "\" title=\"显示结果从 $pagenumbers[first]$pagenumbers[last] 共计 $total\"> $curpage </a>";
				}
			}
		}
        $pagenav = "
                <span class=\"disabled\">
                ".(($show['prev']) ? ("<a onclick=\"$option[onclick]\" class=\"smallfont\" href=\"$address".("/page:$prevpage".($address2 ? '?'.$address2 : ''))."\" title=\"上一页 - 结果从 $prevnumbers[first]$prevnumbers[last] 共计 $total\">上一页</a></span>") : ("上一页</span>"))."
 
                $pagenav
 
                ".(($show['next']) ? ("<span><a onclick=\"$option[onclick]\" class=\"smallfont\" href=\"$address/page:$nextpage".($address2 ? '?'.$address2 : '')."\" title=\"下一页 - 结果从 $nextnumbers[first]$nextnumbers[last] 共计 $total\">下一页</a></span>") : ("<span>下一页</span>"))."
 
				<span>第 $pagenumber / $totalpages 页,共 <strong>$total</strong>条记录</span>";
		return $pagenav;
	}
	/**
	* Returns an array so you can print 'Showing results $arr[first] to $arr[last] of $totalresults'
	*
	* @param	integer	Current page number
	* @param	integer	Results to show per-page
	* @param	integer	Total results found
	*
	* @return	array	In the format of - array('first' => x, 'last' => y)
	*/
	function fetch_start_end_total_array($pagenumber, $perpage, $total)
	{
		$first = $perpage * ($pagenumber - 1);
		$last = $first + $perpage;
 
		if ($last > $total)
		{
			$last = $total;
		}
		$first++;
 
		return array('first' => number_format($first), 'last' => number_format($last));
	}
}
?>
Tags : ,

[备忘]Mysql中explain的使用详解(0 位领导批示)

explain显示了mysql如何使用索引来处理select语句以及连接表.可以帮助选择更好的索引和写出更优化的查询语句.
使用方法:在select语句前加上explain. 如 EXPLAIN SELECT * FROM `users`

Explain 列的解释:
table 显示这一行的数据是关于哪张表的
type 这是重要的列,显示连接使用了何种类型。从最好到最差的连接类型为const、eq_reg、ref、range、indexhe和ALL
possible_keys 显示可能应用在这张表中的索引。如果为空,没有可能的索引。可以为相关的域从WHERE语句中选择一个合适的语句
key 实际使用的索引。如果为NULL,则没有使用索引。很少的情况下,MYSQL会选择优化不足的索引。这种情况下,可以在SELECT语句中使用USE INDEX(indexname)来强制使用一个索引或者用IGNORE INDEX(indexname)来强制MYSQL忽略索引
key_len 使用的索引的长度。在不损失精确性的情况下,长度越短越好
ref 显示索引的哪一列被使用了,如果可能的话,是一个常数
rows MYSQL认为必须检查的用来返回请求数据的行数
Extra 关于MYSQL如何解析查询的额外信息。这里可以看到的坏的例子是Using temporary和Using filesort,意思MYSQL根本不能使用索引,结果是检索会很慢

全文阅读 »

[备忘]PHP简单调试(1 位领导批示)

还是很方便的调试方式..记录一下备忘

1
2
ini_set("error_log", "E:\php_error.log");  
error_log($output,0);

php生成渐变图片(0 位领导批示)

问题链接:http://bbs.blueidea.com/thread-2957105-1-1.html

我是使用GD的 imagefilledrectangle 函数来实现的. 不知是否有更好的解法

1
2
3
4
5
6
7
8
9
10
11
12
<?php
$im = imagecreate(255, 255);
$bg = imagecolorallocate($im, 0, 0, 0);
for($i=255; $i>=0; $i--)
{
    $color = imagecolorallocate($im, $i, $i, $i);
    imagefilledrectangle($im, 0, $i, 255, 1, $color);
}
header("Content-type: image/png");
imagepng($im);
imagedestroy($im);
?>
Tags : , , ,

PHP处理简单图片的颜色填充(0 位领导批示)

原问题地址: http://bbs.blueidea.com/viewthread.php?tid=2956421


现在有一张背景色为纯蓝色(或者红色等) 并且与照片里人物有很明显的反差色彩 的一寸单人照照片,现想把,该图片中的蓝色背景用PHP处理为白色。即类似于PS中,用白色填充蓝色的效果。

使用GD库的imagecolorset函数可以修改简单的索引色. 不过只能对gif与png图片有效

1
2
3
4
5
6
7
8
<?php
$img = file_get_contents('http://home.blueidea.com/attachment/200911/9/207382_1257738485WdvH.gif');
$im = imagecreatefromstring($img);
$bg = imagecolorat($im, 0, 0);
imagecolorset($im, $bg, 0, 0, 255);
imagepng($im);
imagedestroy($im);
?>

[备忘]安装PHP扩展时需要同时安装的扩展列表(0 位领导批示)

仅备忘

php_curl.dll CURL, Client URL library functions Requires: libeay32.dll, ssleay32.dll (bundled)
php_domxml.dll DOM XML functions PHP <= 4.2.0 requires: libxml2.dll
(bundled) PHP >= 4.3.0 requires: iconv.dll (bundled)
php_fdf.dll FDF: Forms Data Format functions. Requires: fdftk.dll
gnu_gettext.dll (bundled), PHP >= 4.2.3 requires libintl-1.dll,
php_iconv.dll ICONV characterset conversion Requires: iconv-1.3.dll
php_ingres.dll Ingres II functions Requires: Ingres II libraries
php_interbase.dll InterBase functions Requires: gds32.dll (bundled)
php_java.dll Java functions PHP <= 4.0.6 requires: jvm.dll (bundled)
php_ldap.dll LDAP functions PHP <= 4.2.0 requires libsasl.dll(bundled),
PHP >= 4.3.0 requires libeay32.dll,ssleay32.dll (bundled)
php_mcrypt.dll Mcrypt Encryption functions Requires: libmcrypt.dll
php_mhash.dll Mhash functions PHP >= 4.3.0 requires: libmhash.dll (bundled)
php_mcrypt.dll Mcrypt Encryption functions Requires: libmcrypt.dll
php_mhash.dll Mhash functions PHP >= 4.3.0 requires: libmhash.dll (bundled)
php_msql.dll mSQL functions Requires: msql.dll (bundled)
php_mssql.dll MSSQL functions Requires: ntwdblib.dll (bundled)
php_mysql.dll MySQL functions PHP >= 5.0.0, requires libmysql.dll (bundled)
php_mysqli.dll MySQLi functions PHP >= 5.0.0, requires libmysqli.dll (bundled)
php_oci8.dll Oracle 8 functions Requires: Oracle 8.1+ client libraries
php_openssl.dll OpenSSL functions Requires: libeay32.dll (bundled)
php_oracle.dll Oracle functions Requires: Oracle 7 client libraries
php_sybase_ct.dll Sybase functions Requires: Sybase client libraries
php_xmlrpc.dll XML-RPC functions PHP >= 4.2.1 requires: iconv.dll (bundled)
php_xslt.dll XSLT functions PHP <= 4.2.0 requires sablot.dll, expat.dll (bundled).
PHP >= 4.2.1 requires sablot.dll, expat.dll, iconv.dll (bundled).

转换avatar头像函数(0 位领导批示)

仅备忘

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**
 * 获取 avatar 头像
 *
 * @param string $email
 * @param integer $size
 * @return string
 */
function getAvatar($email, $size = '24')
{
	if (!is_numeric($size))
	{
		$size = '24';
	}
	if (!empty($email))
	{
		$out = 'http://www.gravatar.com/avatar/' . md5(strtolower($email)) . '?s=' . $size;
		$avatar = "<img src='{$out}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' alt='{$email}' /> " . $email;
	}
	else
	{
		$avatar = $email;
	}
 
	return $avatar;
}

随机显示的10篇日志

评论最多的10篇日志

浏览最多的10篇日志