PHP数组生成类

<? php
/*
+--------------------------------------------------------------------------
| 数组生成类
| ============================
| by Luoxiaoqiang  2006.5
+--------------------------------------------------------------------------
*/
// ++USER_DIARY生成数组++
//生成时间:2006-5-24 06:02:39
//writed by: luoxiaoqiang 2006.5
//$getary =  new getarray("USER_DIARY","user_diary","where uid =7201","./");
//$getary->output();

class  getarray
{
   
    
function  getarray( $array_name , $table_name , $condition_str , $path )
    {
        
$this -> array_name  =   $array_name ;
        
$this -> table_name  =   $table_name ;
        
$this -> condition_str  =   $condition_str ;
        
$this -> path  =   $path ;
    }
    
function  output()
    {   
        
global   $db ;
        
$str   =   "" ;
        
$array_name   =   $this -> array_name;
        
$table_name   =   $this -> table_name;
        
$condition_str   =   $this -> condition_str;
        
$path   =   $this -> path;
        
$sql   =   " select * from  " . $table_name . "     " . $condition_str ;
        
$ary   =   $db -> GetArray( $sql );
        
while ( list ( $key , $value =   each ( $ary ))
        {   
            
$i   =   0 ;
            
while ( list ( $key1 , $value1 =   each ( $value )){
                
if  ( $i   ==   0 $key_value   =   $value1 ;
                
$i ++ ;
                
$str   .=   ' $ ' . $array_name . ' [ ' . $key_value . ' ][" '   . $key1 .   ' "] = " ' . addslashes ( $value1 ) . ' "; ' . NL;         
            }
        }
       
$str   =   str_replace ( " <? " , "" , $str );
       
$str   =   str_replace ( " ?> " , "" , $str );
       
$body_str   =   ' <?php ' . NL . ' //++ ' . $array_name . ' 生成数组++ ' . NL . NL;
       
$body_str   .=   ' //生成时间: ' . date ( " Y-n-j H:i:s " ) . NL . NL;
       
$body_str   .=   ' //writed by: luoxiaoqiang 2006.5 ' . NL . NL;
       
$body_str   .=   $str . NL . ' ?> ' ;
       
$filename   =   $path . $table_name . ' .php ' ;
       
$fp   =   fopen ( $filename ,   " w " );
       
if  ( fwrite ( $fp ,   $body_str )) {
            
fclose ( $fp );
            @
chmod ( $filename ,   0755 );
       }   
    }
}
/*
+--------------------------------------------------------------------------
| 数组生成类
| ============================
| by Luoxiaoqiang  2006.5
+--------------------------------------------------------------------------
 user method: $test = new GetArray();
              $test->getArrayBody('liansuo_id,liansuo_name','liansuo','','');
cache file: test.php 
array name: TEST
$TEST[1]["liansuo_id"] = 1;
$TEST[1]["username"] = "aikede";
$TEST[1]["liansuo_name"] = "test";
*/
if  ( ! defined ( ' NL ' )) 
{
    
define ( ' NL ' ,   " " );
}
class  GetArray
{       
    
function  getArrayBody( $field_str , $table_name , $query_str , $path )
    {  
        
global   $db ;
        
if  ( ! isset ( $db ))  exit ( ' 数据库连接不正确 ' ); 
        
// 得到主键
         $primary_key_ary   =   $db -> MetaPrimaryKeys( $table_name );
        
if  ( isset ( $primary_key_ary [ 0 ])  &&   $primary_key_ary [ 0 !=   '' ) {
            
$primary_key   =   $primary_key_ary [ 0 ];
        } 
else  {
            
reset ( $table_columns );
            
$i   =   0 ;
            
while  ( list ( $key ,   $val =   each ( $table_columns )) {
                
if  ( $i   ==   0 ) {
                    
$primary_key   =   $val -> name;
                    
break ;
                }
            }
        }
        
$table_columns   =   $db -> MetaColumns( $table_name ); 
        
$field_ary   =   explode ( ' , ' , $field_str ); // 得到所需字段
         if  ( ! $rs   =   $db -> Execute( " SELECT * FROM  "   .   $table_name   .   "   "   .   $query_str )) {
            
echo   $db -> ErrorMsg();  
            
$db -> Close();
            
exit ();
        }
        
$array_str   =   ''
        
while ( ! $rs -> EOF) {
            
reset ( $table_columns );
             
while  (  list ( $key , $value =   each ( $table_columns )) {
                 
$field_value   =  ( $value -> type  ==   ' int ' ) ? $rs -> fields[ $value -> name] : ' " '   . $rs -> fields[ $value -> name] .   ' " ' ;
                 
if  ( isset ( $field_ary &&   count ( $field_ary >   1  ){
                     
if ( in_array ( $value -> name , $field_ary )){
                           
$array_str   .=   ' $ ' . strtoupper ( $table_name ) . ' [ ' . $rs -> fields[ $primary_key ] . ' ] ' . ' [" '   . $value -> name .   ' "] =  ' . $field_value . ' ; '   .  NL;
                     }
                     
                 } 
else  {
                    
$array_str   .=   ' $ ' . strtoupper ( $table_name ) . ' [ ' . $rs -> fields[ $primary_key ] . ' ] ' . ' [" '   . $value -> name .   ' "] =  ' . $field_value . ' ; '   .  NL;
                 }
             }          
           
$rs -> MoveNext();
       }
       
$body_str   =   ' <?php ' . NL . ' //++ ' . $table_name . ' 生成数组++ ' . NL . NL;
       
$body_str   .=   ' //生成时间: ' . date ( " Y-n-j H:i:s " ) . NL . NL;
       
$body_str   .=   ' //writed by: luoxiaoqiang 2006.5 ' . NL . NL;
       
$body_str   .=   $array_str . NL . ' ?> ' ;
       
$filename   =   $table_name . ' .php ' ;
       
$fp   =   fopen ( $path . $filename ,   " w " );
       
if  ( fwrite ( $fp ,   $body_str )) {
            
fclose ( $fp );
            @
chmod ( $filename ,   0755 );
       }       
    }
}    
// 输放数组分页代码
/*

+--------------------------------------------------------------------------
| 数组分页
| ============================
| by Luoxiaoqiang  2006.5
+--------------------------------------------------------------------------
*/
/*
//实例化数组分页
$pg = new Page($photo_ary); 
//设置pagename名称 默认为P
$pg->setPageName();
//设置url传递变量值 
$pg->setPageVar();
//设置分页记录数 默认为10
$pg->setNumPerPage();
//设置每分页显示页码数 默认为5
$pg->setOffSet();
//返回分页记录数 
$ary = $pg->getData();
返回分页页码记录
$pg->output();
//对数据库处理成数组
$sql = "SELECT * FROM photo where uid =106859";  
$rs = $db->Execute($sql);
while($results = $rs->FetchRow())
{
    $photo_ary[$results['id']] = $results;
}
//对生成数组文件处理数组
i.g.
$USER_PHOTO_ARY["350106"]["id"] = 350106;
$USER_PHOTO_ARY["350106"]["album_id"] = 84127;
$USER_PHOTO_ARY["350106"]["uid"] = 106859;
 if (isset($USER_PHOTO_ARY) && is_array($USER_PHOTO_ARY)) {
        reset($USER_PHOTO_ARY);
        krsort($USER_PHOTO_ARY);
        while (list($key, $val) = each($USER_PHOTO_ARY)) {
            $photo_ary[$key] = $val;
        }
}
//在显示页面对数组处理
$pg = new Page($photo_ary);
$ary = $pg->getData();
while(list($key,$value) = each($ary))
{  
    echo '<img src="http://home3.mshow.com.cn/user_photo/117/100x75/'.$value['pic_filename'].'"><br>';
}
echo $pg->output();
*/
class  Page
{
    
function  Page( $ary = array ())  
    {
        
$this -> data_ary  =   $ary ;
        
$this -> setPageVar();
        
$this -> setPageName();
        
$this -> setNumPerPage();
        
$this -> setOffSet();
    }
    
function  setNumPerPage( $num_per_page = 10 ) {
        
$this -> num_per_page  =   intval ( $num_per_page );
    }
    
function  setPageName( $page_name = " p " ) {
        
$this -> page_name  =   $page_name ;
    }
    
function  setPageVar( $page_var = array ()) {
        
$this -> page_var  =   $page_var ;
       
    }
    
function  setOffSet( $offset = 5 )
    {
        
$this -> offset  =   $offset ;
    }

    
function  RecordCount()
    {
        
$this -> record_nums  =   count ( $this -> data_ary);
        
return   $this -> record_nums;
    }
    
function  GetCurrentPage()
    {
        
$this -> current_page  =  ( is_numeric ( $_GET [ $this -> page_name])) ? $_GET [ $this -> page_name] : (( is_numeric ( $_POST [ $this -> page_name])) ? $_POST [ $this -> page_name] : 1 );
        
return   $this -> current_page;
    }
    
function  setpage()
    {   
        
        
if  ( $this -> RecordCount()  <   $this -> num_per_page)
        {
            
$this -> totalPages  =   1 ;
        } 
else   if  ( $this -> RecordCount()  %   $this -> num_per_page)
        {
              
$this -> totalPages  =   intval ( $this -> RecordCount()  /   $this -> num_per_page)  +   1 ;  
        } 
else  {
            
$this -> totalPages  =   intval ( $this -> RecordCount()  /   $this -> num_per_page);
        }
        
return   $this -> totalPages;
    }
    
function  output()
    {           
        
$totalpage   =   $this -> setpage();
        
$curpage    =   $this -> GetCurrentPage();
        
$star_page   =   $curpage - intval ( $this -> offset / 2 );
        
$end_page   =   $curpage + intval ( $this -> offset / 2 );
        
$star_page   = ( $star_page <= 0 ) ? 1 : $star_page ;
        
$end_page   =  ( $end_page   >=   $totalpage ) ? $totalpage : $end_page ;
        
$end_page   =  ( $end_page   <=   $this -> offset) ? $this -> offset : $end_page ;
        
$end_page   =  ( $totalpage   <=   $this -> offset) ? $totalpage : $end_page ;
        
$this -> file   =   $_SERVER [ ' PHP_SELF ' ];
       
        
$this -> output  =   ' ' . $this -> RecordCount() . ' 条记录 本页[ ' . $curpage . ' / ' . $totalpage . ' ' ;
        
$str   =   $this -> page_var ? ' & ' . http_build_query ( $this -> page_var) : '' ;
        
$page_ary   =   array ();  
        
for ( $i   =   $star_page ; $i   <=   $end_page $i ++ )
        {   
           
$page_ary []  =   ' <a href=" ' . $this -> file . ' ? ' . $this -> page_name . ' = ' . $i . $str . ' " ><font sytle="font-size:8pt;"> ' . (( $curpage   ==   $i ) ? ' <font color=red> ' . $i . ' </font> ' : $i ) . ' </a> ' ;  
        }

         
$str1   =   ' 转到第<select οnchange="location=' ' . $this -> file . ' ? ' . $this -> page_name . ' ='+this.options[this.selectedIndex].value+' ' . $str . ' '" > ' ;
        
for ( $i   =   1 ; $i   <=   $totalpage ; $i ++ )
        {  
            
$str1   .=   ' <option  ' . ( $i   ==   $curpage ? ' selected ' : '' ) . '  value= ' . $i . ' > ' . $i . ' </option> ' ;
        }
        
$str1   .=   ' </select>页 ' ;
        
return   $this -> output  .=   implode ( ' &nbsp;|&nbsp; ' , $page_ary ) . $str1 ;   
    }
    
function  getArray()
    {
        
reset ( $this -> data_ary);
        
ksort ( $this -> data_ary);
        
$i   =   1 ;
        
$ary   =   array ();
        
while  ( list ( $key ,   $val =   each ( $this -> data_ary)) 
        {   
            
if  ( $i   >=   $this -> GetCurrentPage()  &&   $i   <  ( $this -> GetCurrentPage()  +   $this -> num_per_page) )
            {
                
$ary [ $key =   $val ;
            }
            
$i ++ ;   
        }
        
return   $ary ;
    }
}
?>
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值