本帖最后由 liuzhly 于 2010-01-15 11:33 编辑
狗的数据节省了10多次循环,然后再看农场装饰吧!!新手勿动,修改地方比较多,只是简单说明。
农场装饰字段:目前是 72种装饰存进数据表中,当用户加载时,需要循环72次 !修改代码,压缩字段,让循环次数减少到4-8次!
说明:4次循环是用户没有买装饰 ,8次是用户购买一套装饰
如下两次加载好友和自己代码:
$decorativearr = ( array )json_decode( $list[0][decorative] );
foreach ( $decorativearr as $itemtype => $value )
{
foreach ( $value as $key => $value1 )
{
if ( $value1->status == 1 )
{
if ( $_SGLOBAL['timestamp'] < $value1->validtime || $value1->validtime == 0 )
{
$decorativearr_srt[] = "".$itemtype."\":{\"itemId\":".$key."}";
}
else if ( ( $value1->validtime < $_SGLOBAL['timestamp'] ) || ( $value1->validtime != 0 ) )
{
foreach ( $value as $key2 => $value2 )
{
if ( $itemtype == $key2 )
{
$value2->status = 1;
}
else
{
$value2->status = 0;
}
}
$decorativearrsql = json_encode( $decorativearr );
$_SGLOBAL['db']->query( "UPDATE ".tname( "plug_newfarm" )." set decorative='".$decorativearrsql."' where uid=".$_SGLOBAL['supe_uid'] );
$decorativearr_srt[] = "".$itemtype."\":{\"itemId\":".$itemtype."}";
}
}
}
}
$decorative_srt = json_encode( $decorativearr_srt );
修改成如下:
$decorativesql = 0;
foreach ( $decorativearr as $itemtype => $value ){
foreach ( $value as $key => $value1 ){
//一下代码判断:如果是系统默认装饰,保留,购买时间没到期的,保留,剩下的,全部清除掉!节省数据库空间,提高程序运行效率!
if ( $value1->status == 1 ){
if ( $_SGLOBAL['timestamp'] < $value1->validtime || $value1->validtime == 0 ){
$decorativearr_srt[] = "".$itemtype."\":{\"itemId\":".$key."}";
}else{
unset($decorativearr->$itemtype->$key);
$decorativesql = 1;
$value->$itemtype->status = 1;
$decorativearr_srt[] = "".$itemtype."\":{\"itemId\":".$itemtype."}";
}
}else{
if($value1->validtime != 0 && $_SGLOBAL['timestamp'] >= $value1->validtime){
unset($decorativearr->$itemtype->$key);
$decorativesql = 1;
}
}
}
}
//将数据库更改在这里,一次更新就行了,上面代码如果4个场景过期,要更新4次!
$decorativesql == 1 && $_SGLOBAL['db']->query( "UPDATE ".tname( "plug_newfarm" )." set decorative='".json_encode( $decorativearr )."' where uid=".intval( $_REQUEST['ownerId'] ) );
这样,用户访问的时候,自动压缩了数据库!!!!
另外,还需要修改两个地方,
1、用户购买装饰!
$decorativearr = json_decode( $list[0][decorative] );
$kk = 0;
$decorativesql = 0;
for ( $a=0; $a<count($ids); $a++ ) {
foreach ( $decorativearr as $itemtype => $value ){
foreach ( $value as $key => $value1 ){
if ( $key == $ids[$a] ){
$decorativesql = 1;
$value1->validtime = $_SGLOBAL['timestamp'] + $decorative[$key][itemValidTime];
$decorative[$key][itemName] = str_replace("\\u","\\\\u",$decorative[$key][itemName]);
$item_Name = $decorative[$key][itemName];
if ( $kk == 0 ) {
$decorativearr_srt = "{\"id\":".$key.",\"itemId\":".$key.",\"itemType\":".$itemtype.",\"itemName\":\"".$item_Name."\",\"validTime\":".$value1->validtime.",\"status\":".$value1->status."}";
$kk++;
} else {
$decorativearr_srt .= ",{\"id\":".$key.",\"itemId\":".$key.",\"itemType\":".$itemtype.",\"itemName\":\"".$item_Name."\",\"validTime\":".$value1->validtime.",\"status\":".$value1->status."}";
}
}
}
}
if($decorativesql == 0){
$ids_id = $ids[$a];
$keyid = $decorative[$ids_id][itemType];
$item_Name = str_replace("\\u","\\\\u",$decorative[$ids_id][itemName]);
$decorativearr->$keyid->$ids_id->validtime = $_SGLOBAL['timestamp'] + $decorative[$key][itemValidTime];
$decorativearr->$keyid->$ids_id->status = 0;
if ( $kk == 0 ) {
$decorativearr_srt = "{\"id\":".$ids_id.",\"itemId\":".$ids_id.",\"itemType\":".$keyid.",\"itemName\":\"".$item_Name."\",\"validTime\":".$decorativearr->$keyid->$ids_id->validtime.",\"status\":0}";
$kk++;
} else {
$decorativearr_srt .= ",{\"id\":".$ids_id.",\"itemId\":".$ids_id.",\"itemType\":".$keyid.",\"itemName\":\"".$item_Name."\",\"validTime\":".$decorativearr->$keyid->$ids_id->validtime.",\"status\":0}";
}
}
}
$decorativearr = json_encode( $decorativearr );
$decorativearr = json_decode( $list[0][decorative] );
$decorativesql = 0;
foreach ( $decorativearr as $itemtype => $value ){
foreach ( $value as $key => $value1 ){
if ( $key == $_REQUEST['id'] ){
$decorativesql = 1;
$value1->validtime = $_SGLOBAL['timestamp'] + $decorative[$_REQUEST['id']][itemValidTime];
$decorative[$key][itemName] = str_replace("\\u","\\\\u",$decorative[$key][itemName]);
$decorativearr_srt = "{\"id\":".$key.",\"itemId\":".$key.",\"itemType\":".$itemtype.",\"itemName\":\"".$decorative[$key][itemName]."\",\"validTime\":".$value1->validtime.",\"status\":".$value1->status."}";
}
}
}
if($decorativesql == 0){
$ids_id = $_REQUEST['id'];
$keyid = $decorative[$ids_id][itemType];
$decorativearr->$keyid->$ids_id->validtime = $_SGLOBAL['timestamp'] + $decorative[$key][itemValidTime];
$item_Name = str_replace("\\u","\\\\u",$decorative[$ids_id][itemName]);
$decorativearr->$keyid->$ids_id->status = 0;
$decorativearr_srt = "{\"id\":".$ids_id.",\"itemId\":".$ids_id.",\"itemType\":".$keyid.",\"itemName\":\"".$item_Name."\",\"validTime\":".$decorativearr->$keyid->$ids_id->validtime.",\"status\":0}";
}
$decorativearr = json_encode( $decorativearr ); |