php与Open Flash Chart多报表切换例子
将生成好的JSON数据传递给前端,前端通过JS来实现切换效果
例子。
<?php // // This is the MODEL section: // include '../php-ofc-library/open-flash-chart.php'; $title = new title( date("D M d Y") ); $bar = new bar(); $bar->set_values( array(9,8,7,6,5,4,3,2,1) ); $chart_1 = new open_flash_chart(); $chart_1->set_title( $title ); $chart_1->add_element( $bar ); // generate some random data srand((double)microtime()*1000000); $tmp = array(); for( $i=0; $i<9; $i++ ) $tmp[] = rand(1,10); $bar_2 = new bar(); $bar_2->set_values( $tmp ); $chart_2 = new open_flash_chart(); $chart_2->set_title( new title( "Chart 2 :-)" ) ); $chart_2->add_element( $bar_2 ); // // This is the VIEW section: // ?> <html> <head> <script type="text/javascript" src="js/json/json2.js"></script> <script type="text/javascript" src="js/swfobject.js"></script> <script type="text/javascript"> swfobject.embedSWF("open-flash-chart.swf", "my_chart", "350", "200", "9.0.0"); </script> <script type="text/javascript"> function ofc_ready() { alert('ofc_ready'); } function open_flash_chart_data() { alert( 'reading data' ); return JSON.stringify(data_1); } function load_1() { tmp = findSWF("my_chart"); x = tmp.load( JSON.stringify(data_1) ); } function load_2() { alert("loading data_2"); tmp = findSWF("my_chart"); x = tmp.load( JSON.stringify(data_2) ); } function findSWF(movieName) { if (navigator.appName.indexOf("Microsoft")!= -1) { return window[movieName]; } else { return document[movieName]; } } var data_1 = <?php echo $chart_1->toPrettyString(); ?>; var data_2 = <?php echo $chart_2->toPrettyString(); ?>; </script> </head> <body> <p>Open Flash Chart</p> <div id="my_chart"></div> <br> <a href="javascript:load_1()">display data_1</a> || <a href="javascript:load_2()">display data_2</a> <p> Don't forget to 'view source' to see how the Javascript JSON data is loaded. </p> </body> </html>有时我们为了网站安全考虑,我们不允许直接跨域提交form表单数据,如果我们自己有这个需求呢?下面我们来介绍两种跨域的方法解决直接跨域问题。
下面我们来看看两种php跨域提交form的方法
一,通过php curl
function curlPost($url,$params)
{
$postData = '';
foreach($params as $k => $v)
{
$postData .= $k . '='.$v.'&';
}
rtrim($postData, '&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, count($postData));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$output=curl_exec($ch);
curl_close($ch);
return $output;
}
echo curlPost("http://test.com",array('name'=>"tank"));
以前很多人用curl来抓,邮箱的通讯录,不过现在已经不可以了。哈哈。
二,利用jquery form,ajax提交
1,下载jquery.form.js
2,js代码
$('#testform').submit(function() {
$(this).ajaxSubmit({
type: 'post', // 提交方式 get/post
dataType:"json",//数据类型
url: 'your url', // 需要提交的 url
success: function(data) { // data 保存提交后返回的数据,一般为 json 数据
// 此处可对 data 作相关处理
alert('提交成功!');
}
$(this).resetForm(); // 提交后重置表单
});
return false; // 阻止表单自动提交事件
});
3,php代码
header("Access-Control-Allow-Origin:*"); //跨域权限设置,允许所有
header("Access-Control-Allow-Origin:http://www.test.com"); //只允许test.com跨域提交数据
php memcache独立用php实现,是老客户端,从我们实践中已发现有多个问题,而且功能少,属性也可设置的少;
php memcached是基于原生的c的libmemcached的扩展,更加完善,建议替换为php memcached。
1. Php memcache的问题
1.1 分布式问题
php memcache默认会自动切换实例,所以有时取到老数据,并且value飘忽不定。
网友分享的问题:
这几天做某个产品的时候遇到一个小问题,现象比较诡异,产品用了两台分布式的memcached服务器。某一个计数器取回来的数偶尔会不对,最后定位在php memcache client的failover机制上面。
我们知道,在memcached分布式环境下,某一个key是通过hash计算,分配到某一个memcached上面的。
如果php.ini里面 memcache.allow_failover = 1的时候,在分布式环境下,某一台memcached出问题的话,会自动到其他的memcached尝试,就会出现上面的问题。所以要设置 allow_failover = 0 那么取不到时就直接返回失败而不会从其它mc上取,这样以避免网络异常或server端异常时,经常切换实例,会取到老数据。
1.2 高并发下稳定性问题
新浪微博提到的教训:
php memcache换成php memcached,在高并发下稳定下极大提高;
另外功能更多,出错码更精确。
Twitter的缓存经验
多层次Cache,减轻某些cache节点宕掉后的影响,读写都cache;
将memcached api统一换为libmemcached(方便多语言访问memcached,让分布式等各种规则都一致。)
1.3 1秒超时间隔没法修改问题
php memcache客户端有个1秒超时间隔没法修改问题:
bool Memcache::connect ( string $host [, int $port [, int $timeout ]] )
第三个参数本来可设置timeout,单位秒,但无法修改。
测试了以下三种修改timeout的方法都无效:
1.3.1. 用memcache api Memcache::setServerParams不能修改;
1.3.2. 改memcache 源代码vi php_memcache.h宏定义不能修改;
1.3.3. php.ini内这个配置:default_socket_timeout = 60对本timeout无效。
2. memcache和memcached对比
Php memcache这个老客户端在属性设置方面可设置的很少;
出错码粒度很粗,出错后难以定位;
而且功能欠缺一些:
There are primarily two clients used with PHP. One is the older, more widespread pecl/memcache and the other is the newer, less used, more feature rich pecl/memcached.
Both support the basics such as multiple servers, setting vaules, getting values, increment, decrement and getting stats.
Here are some more advanced features and information.
项目 pecl/memcache pecl/memcached
First Release Date 2004-06-08 2009-01-29 (beta)
Actively Developed Yes Yes
External Dependency None libmemcached
Automatic Key Fixup1 Yes No
Append/Prepend No Yes
Automatic Serialzation2 Yes Yes
Binary Protocol No Optional
CAS No Yes
Compression Yes Yes
Communication Timeout Connect Only Various Options
Consistent Hashing Yes Yes
Delayed Get No Yes
Multi-Get Yes Yes
Session Support Yes Yes
Set/Get to a specific server No Yes
Stores Numerics Converted to Strings Yes
注释:
1 pecl/memcache will convert an invalid key into a valid key for you. pecl/memcached will return false when trying to set/get a key that is not valid.
2 You do not have to serialize your objects or arrays before sending them to the set commands. Both clients will do
Memcache 查看列出所有key方法
今天在做一个Memcache的session测试,但是在测试的过程中,发现Memcache没有一个比较简单的方法可以直接象redis那样keys *列出所有的Session key,并根据key get对应的session内容,于是,我开始查找资料,翻出来的大部分是一些memcache常用命令等,但是对列出key的办法,讲解却不多,于是来到google,找到了一个国外的资料
具体的内容我套用我的测试环境中,操作如下
1. cmd上登录memcache
> telnet 127.0.0.1 11211
2. 列出所有keys
stats items // 这条是命令
STAT items:7:number 1
STAT items:7:age 188
END
3. 通过itemid获取key
接下来基于列出的items id,本例中为7,第2个参数为列出的长度,0为全部列出
stats cachedump 7 0 // 这条是命令
ITEM Sess_sidsvpc1473t1np08qnkvhf6j2 [183 b; 1394527347 s]
END
4. 通过get获取key值
上面的stats cachedump命令列出了我的session key,接下来就用get命令查找对应的session值
get Sess_sidsvpc1473t1np08qnkvhf6j2 //这条是命令
VALUE
Sess_sidsvpc1473t1np08qnkvhf6j2 1440 1
83
Sess_|a:5:{s:6:"verify";s:32:"e70981fd305170c41a5632b2a24bbcaa";s:3:"uid";s:1:"1
";s:8:"username";s:5:"admin";s:9:"logintime";s:19:"2014-03-11 16:24:25";s:7:"log
inip";s:9:"127.0.0.1";}
memkeys实时查看memcached key使用情况
memkeys
memkeys是tumblr开源的类似top的工具,可用于实时查看memcached的key使用情况.
memkeys安装
安装autoconf(要求版本2.68以上):
# wget -c http://ftp.gnu.org/gnu/autoconf/autoconf-latest.tar.gz
# tar zxvf autoconf-latest.tar.gz
# cd autoconf-2.69
# ./configure
# make && make install
安装其它依赖:
# yum install libpcap-devel pcre-devel ncurses-devel
安装memkeys:
# git clone https://github.com/tumblr/memkeys.git
# cd memkeys
# ./autogen.sh
# ./configure
# make && make install
memkeys使用
# memkeys -h
Usage: memkeys -i NIC [options]
-d, --discard=THRESH Discard keys where req/s rate is below THRESH
-i, --interface=NIC Network interface to capture traffic on (required)
-p, --port=PORT Network port to capture memcache traffic on (default 11211)
-r, --refresh=INTERVAL Refresh the stats display every INTERVAL ms (default 500)
-l, --logfile=FILE Output logs to FILE
-R, --report=REPORT Output data in REPORT format (CSV or curses, default curses)
-h, --help This help
-v, --verbose Increase verbosity. May be used multiple times.
-V, --version Show program info and exit.
例子1:
# memkeys -i eth0 -l /tmp/memkeys.log
例子2:
# memkeys -i eth0 -d 10.0 -l /tmp/memkeys.log
做了一个QQ聊天交友网站,想加个聊天的功能,于是做完用PHP做了简单又强大的聊天室
1. 创建mysql数据库表:create table chat( id bigint AUTO_INCREMENT,username varchar(20), chatdate datetime,msg varchar(500), primary key(id));
2.编写建议连接数据库函数:
dbconnect.php
<?php
function db_connect()
{
date_default_timezone_set("Asia/Shanghai");
$link = mysql_connect("xxx.xxx.xxx.xxx", "databasename", "password")
or die('无法连接: ' . mysql_error());
mysql_select_db("databasename") or die('没有你找到指定数据库');
return true;
}
function quote($strText)
{
$Mstr = addslashes($strText);
return "'" . $Mstr . "'";
}
function isdate($d)
{
$ret = true;
try
{
$x = date("d",$d);
}
catch (Exception $e)
{
$ret = false;
}
echo $x;
return $ret;
}
?>
3. 编写ajax发送和接收函数:
ajax发送函数chat_send_ajax.php
<?php
require_once('dbconnect.php');
db_connect();
$msg = iconv("UTF-8","GB2312",$_GET["msg"]);
$dt = date("Y-m-d H:i:s");
$user = iconv("UTF-8","GB2312",$_GET["name"]);
$sql="INSERT INTO chat(USERNAME,CHATDATE,MSG) " .
"values(" . quote($user) . "," . quote($dt) . "," . quote($msg) . ");";
echo $sql;
$result = mysql_query($sql);
if(!$result)
{
throw new Exception('Query failed: ' . mysql_error());
exit();
}
?>
ajax接收函数chat_recv_ajax.php
<?php
header("Content-Type:text/html;charset=gb2312");
header("Expires: Thu, 01 Jan 1970 00:00:01 GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
require_once('dbconnect.php');
db_connect();
$sql = "SELECT *, date_format(chatdate,'%Y年%m月%d日 %r') as cdt from chat order by ID desc limit 200";
$sql = "SELECT * FROM (" . $sql . ") as ch order by ID";
$result = mysql_query($sql) or die('Query failed: ' . mysql_error());
// Update Row Information
$msg="<table border='0' style='font-size: 10pt; color: white; font-family: verdana, arial;'>";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
$msg = $msg . "<tr><td>" . $line["cdt"] . " </td>" .
"<td>" . $line["username"] . ": </td>" .
"<td>" . $line["msg"] . "</td></tr>";
}
$msg=$msg . "</table>";
echo $msg;
?>
4.聊天室页面:
chat.html
<!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=gb2312" />
<title>聊天页面</title>
<script type="text/javascript">
var t = setInterval(function(){get_chat_msg()},5000);
//
// General Ajax Call
//
var oxmlHttp;
var oxmlHttpSend;
function get_chat_msg()
{
if(typeof XMLHttpRequest != "undefined")
{
oxmlHttp = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
oxmlHttp = new ActiveXObject("Microsoft.XMLHttp");
}
if(oxmlHttp == null)
{
alert("浏览器不支持XML Http Request!");
return;
}
oxmlHttp.onreadystatechange = get_chat_msg_result;
oxmlHttp.open("GET",encodeURI("chat_recv_ajax.php"),true);
oxmlHttp.send(null);
}
function get_chat_msg_result()
{
if(oxmlHttp.readyState==4 || oxmlHttp.readyState=="complete")
{
if (document.getElementById("DIV_CHAT") != null)
{
document.getElementById("DIV_CHAT").innerHTML = oxmlHttp.responseText;
oxmlHttp = null;
}
var scrollDiv = document.getElementById("DIV_CHAT");
scrollDiv.scrollTop = scrollDiv.scrollHeight;
}
}
function set_chat_msg()
{
if(typeof XMLHttpRequest != "undefined")
{
oxmlHttpSend = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
oxmlHttpSend = new ActiveXObject("Microsoft.XMLHttp");
}
if(oxmlHttpSend == null)
{
alert("浏览器不支持XML Http Request!");
return;
}
var url = "chat_send_ajax.php";
var strname="noname";
var strmsg="";
if (document.getElementById("txtname") != null)
{
strname = document.getElementById("txtname").value;
document.getElementById("txtname").readOnly=true;
}
if (document.getElementById("txtmsg") != null)
{
strmsg = document.getElementById("txtmsg").value;
document.getElementById("txtmsg").value = "";
}
url += "?name=" + strname + "&msg=" + strmsg;
oxmlHttpSend.open("GET",encodeURI(url),true);
oxmlHttpSend.send(null);
}
function clickBtn(e)
{
if(window.event.keyCode==13)
{
var id=e.id;
switch(id)
{
case "txtmsg":
document.getElementById("Submit2").click();
window.event.returnValue=false;
break;
}
}
}
function fRandomBy(under, over){
switch(arguments.length){
case 1: return parseInt(Math.random()*under+1);
case 2: return parseInt(Math.random()*(over-under+1) + under);
default: return 0;
}
}
function SetTxtName(){
var i=fRandomBy(10);
if(i==0)document.getElementById('txtname').value='无敌战神';
if(i==1)document.getElementById('txtname').value='令狐冲';
if(i==2)document.getElementById('txtname').value='西门吹雪';
if(i==3)document.getElementById('txtname').value='超级玛丽';
if(i==4)document.getElementById('txtname').value='奥巴马';
if(i==5)document.getElementById('txtname').value='恐怖分子';
if(i==6)document.getElementById('txtname').value='聊斋奇女子';
if(i==7)document.getElementById('txtname').value='天朝?潘?;
if(i==8)document.getElementById('txtname').value='中500万了';
if(i==9)document.getElementById('txtname').value='神级奇葩';
if(i==10)document.getElementById('txtname').value='爱你不是两三天';
}
</script>
</head>
<body onload="SetTxtName();">
<div style="border-right: black thin solid; border-top: black thin solid;
border-left: black thin solid; border-bottom: black thin solid;
background:#fff url('http://www.ihaonet.com/chat/blue.jpg') repeat-x left top;
height: 450px;width: 500px; ">
<table style="width:100%; height:100%">
<tr>
<td colspan="2" style="font-weight: bold; font-size: 16pt; color: white; font-family: verdana, arial;
text-align: center">
聊天窗口--全球最大QQ聊天交友网站</td>
</tr>
<tr>
<td colspan="2" style="font-weight: bold; font-size: 16pt; color: white; font-family: verdana, arial;
text-align: left">
<table style="font-size: 12pt; color: white; font-family: Verdana, Arial;border: white thin solid; ">
<tr>
<td style="width: 100px">
名字:</td>
<td style="width: 100px"><input id="txtname" style="width: 150px" type="text" name="name" maxlength="15" value="匿名" /></td>
</tr>
</table>
</td>
</tr>
<tr>
<td style="vertical-align: middle;" valign="middle" colspan="2">
<div style="width: 480px; height: 300px; border-right: white thin solid; border-top: white thin solid; font-size: 10pt; border-left: white thin solid; border-bottom: white thin solid; font-family: verdana, arial; overflow:scroll; text-align: left;" id="DIV_CHAT">
</div>
</td>
</tr>
<tr>
<td style="width: 310px">
<input id="txtmsg" style="width: 350px" type="text" name="msg" onkeydown="return clickBtn(this)"/></td>
<td style="width: 85px">
<input id="Submit2" style="font-family: verdana, arial" type="button" value="发送" onclick="set_chat_msg()"/></td>
</tr>
<tr>
<td colspan="1" style="font-family: verdana, arial; text-align: center; width: 350px;">
</td>
<td colspan="1" style="width: 85px; font-family: verdana, arial; text-align: center">
</td>
</tr>
</table>
</div>
</body>
</html>
相关文章
- 下面小编就为大家带来一篇利用JS实现点击按钮后图片自动切换的简单方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2016-10-25
- 这篇文章主要介绍了js组件SlotMachine实现图片切换效果制作抽奖系统的相关资料,需要的朋友可以参考下...2016-04-19
- 最近项目不多忙,于是抽点时间巩固下切换窗口问题,感兴趣的朋友跟着小编一起学习吧...2020-06-25
- 下面小编就为大家带来一篇javascript的列表切换【实现代码】。小编觉得挺不错的,现在分享给大家,也给大家做个参考。...2016-05-05
- 复制代码 代码如下: <!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> <title...2013-10-13
- 这篇文章主要介绍了Vue鼠标滚轮滚动切换路由效果的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-08-04
- 这篇文章主要介绍了Pytorch如何切换 cpu和gpu的使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-01
- 这篇文章主要介绍了C#在Winform开发中使用Grid++报表,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
- 这篇文章主要为大家详细介绍了jquery实现有过渡效果的tab切换,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-07-17
- 这篇文章主要介绍了javascript实现点击图片切换,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-04-05
- 本文实例讲述了js实现索引图片切换效果的代码。分享给大家供大家参考。具体如下: 运行效果截图如下:具体代码如下html代码: <div id="slideshowHolder"> <img src="img/1.jpg" /> <img src="img/2.jpg" /> <im...2015-11-24
- 这篇文章主要介绍了JavaScript实现图片滑动切换的代码示例分享,能够控制包括滑动时间和切换数量等,需要的朋友可以参考下...2016-03-09
- jQuery菜单式图片滑动切换是一款天猫官方网站的鼠标滑过图片切换导航菜单特效。复制代码 代码如下: $(function(){ // floorCon-slide $(".floorCon-slide .floorConSlideImgNav li span").css({opacity:0.95...2015-03-15
jQuery和hwSlider实现内容响应式可触控滑动切换效果附源码下载(二)
这篇文章主要介绍了jQuery和hwSlider实现内容响应式可触控滑动切换效果附源码下载(二)的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下...2016-06-24- 这篇文章主要介绍了基于jQuery实现多标签页切换的效果(web前端开发)的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下...2016-07-29
JavaScript快速切换繁体中文和简体中文的方法及网站支持简繁体切换的绝招
这篇文章主要介绍了JavaScript快速切换繁体中文和简体中文方法的相关资料,需要的朋友可以参考下...2016-03-09- 如图所示。 该图片切换特效实现很简单,而且兼容性很好。 html页面如下 复制代码 代码如下: <div class="wrapper"> <div id="focus"> <ul> <li><a href="http://www.lanrentuku.com/" target="_blank"><img src="i...2014-05-31
- 这篇文章主要为大家详细介绍了Vue实现菜单切换功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-11-08
- 这篇文章主要为大家详细介绍了Vue实现简单图片切换效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-06-25
- 这篇文章主要为大家详细介绍了vue使用swiper实现左右滑动切换图片,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-10-17