LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 564|回复: 2

一个可编辑可分页的PHP函数

[复制链接]
发表于 2005-6-4 11:35:08 | 显示全部楼层 |阅读模式
[php]
<?php

/*

  本程序由陈刚在John Lim的tohtml.inc.php基础上改写;

  V0.01 16 MAY 2005 (c)

  以下是tohtml.inc.php的版权信息,请您在使用或改写本程序时予以注明;

  V4.52 10 Aug 2004  (c)

   2000-2004 John Lim (jlim@natsoft.com.my). All rights reserved.

  Released under both BSD license and Lesser GPL library license.

  Whenever there is any discrepancy between the two licenses,

  the BSD license will take precedence.

  Some pretty-printing by Chris Oxenreider <oxenreid@state.net>

  关于本程序的版权申明

  本程序基于BSD协议。

  作者联系方式:cg19700105@yahoo.com.cn

*/

  

/// specific code for tohtml;

GLOBAL $gSQLMaxRows,$gSQLBlockRows;

         

$gSQLMaxRows = 1000; // max no of rows to download

$gSQLBlockRows=20; // max no of rows per table block

// RecordSet to HTML Table

function rs2html(&$rs,$edit,$askpage,$ppage,$ztabhtml=false,$zheaderarray=false,$htmlspecialchars=true,$echo = true)

{

        $s ='';$rows=0;$docnt = false;

        $lengthedit=($edit)?sizeof($edit):0;

        GLOBAL $gSQLMaxRows,$gSQLBlockRows;

        if (!$rs)

           {

                printf(ADODB_BAD_RS,'rs2html');

                return false;

           }

       

        if (! $ztabhtml) $ztabhtml = "BORDER='1' WIDTH='98%'";

        //else $docnt = true;

        $typearr = array();

        //$ncols = $rs->FieldCount();

        $ncolss = $rs->FieldCount();

        $ncols =$lengthedit+$ncolss-1;

        $hdr = "<form method=\"post\" enctype=\"multipart/form-data\">";
       

        $hdr .= "<TABLE COLS=$ncols $ztabhtml><tr>\n\n";

        for ($i=0; $i < $ncols+1; $i++)

                 {       

                  $field = $rs->FetchField($i);

                  if ($zheaderarray) $fname = $zheaderarray[$i];

                  else $fname = htmlspecialchars($field->name);       

                  $typearr[$i] = $rs->MetaType($field->type,$field->max_length);

                  //print " $field->name $field->type $typearr[$i] ";

                   if (strlen($fname)==0) $fname = '&nbsp;';

                  $hdr .= "<TH>$fname</TH>";

            }

        $hdr .= "\n</tr>";

        if ($echo) print $hdr."\n\n";

        else $html = $hdr;

       

       

       

       

        // smart algorithm - handles ADODB_FETCH_MODE's correctly by probing...

        $numoffset = isset($rs->fields[0]) ||isset($rs->fields[1]) || isset($rs->fields[2]);

        while (!$rs->EOF)

                        {$num=($askpage-1)*$ppage+$rows+1;

                        $s .= "<TR valign=top>\n";

                         $s .= "        <TD width=2 align=right><input size=2 type='checkbox' name=bz".$num." value=".$rs->fields[0] ." ></TD>\n";

                        for ($i=1; $i < $ncolss; $i++)

                                 {if ($i===0) $v=($numoffset) ? $rs->fields[0] : reset($rs->fields);

                                  else $v = ($numoffset) ? $rs->fields[$i] : next($rs->fields);

                             $type = $typearr[$i];

                             switch($type)

                               {case 'D':

                                     if (!strpos($v,':')) {

                                             $s .= "        <TD>".$rs->UserDate($v,"D d, M Y") ."&nbsp;</TD>\n";

                                             break;

                                        }

                                case 'T':

                                        $s .= "        <TD>".$rs->UserTimeStamp($v,"D d, M Y, h:i:s") ."&nbsp;</TD>\n";

                                 break;

                                case 'I':

                                case 'N':

                                     $s .= "        <TD align=right>".stripslashes((trim($v))) ."&nbsp;</TD>\n";

                                     break;

                                default:

                                if ($htmlspecialchars) $v = htmlspecialchars(trim($v));

                                     $v = trim($v);

                                     if (strlen($v) == 0) $v = '&nbsp;';

                                     $s .= "        <TD>". str_replace("\n",'<br>',stripslashes($v)) ."</TD>\n";

                                    

                                          }

                        } // for

               

                 $td="<td><input size=10 type='text' name=";   

                 $tdend=' ></td>';

               

                 for ($m=0;$m<$lengthedit; $m++)

                                    {$s.=$td.$edit[$m].$num.$tdend;}

               

                 $s .= "</TR>\n\n";

                          

                 $rows += 1;

                 if ($rows >= $gSQLMaxRows) {

                           $rows = "<p>Truncated at $gSQLMaxRows</p>";

                           break;

                      } // switch



                $rs->MoveNext();

       

        // additional EOF check to prevent a widow header

                if (!$rs->EOF && $rows % $gSQLBlockRows == 0) {

       

                //if (connection_aborted()) break;// not needed as PHP aborts script, unlike ASP

                        if ($echo) print $s . "</TABLE>\n\n";

                        else $html .= $s ."</TABLE>\n\n";

                        $s = $hdr;

                }

        } // while



        if ($echo) print $s."</TABLE>\n\n";

        else $html .= $s."</TABLE>\n\n";

       

        if ($docnt) if ($echo) print "<H2>".$rows." Rows</H2>";

       

        //return ($echo) ? $rows : $html;

}



// pass in 2 dimensional array

function arr2html(&$arr,$ztabhtml='',$zheaderarray='')

{

        if (!$ztabhtml) $ztabhtml = 'BORDER=1';

       

        $s = "<TABLE $ztabhtml>";//';print_r($arr);



        if ($zheaderarray) {

                $s .= '<TR>';

                for ($i=0; $i<sizeof($zheaderarray); $i++) {

                        $s .= "        <TH>{$zheaderarray[$i]}</TH>\n";

                }

                $s .= "\n</TR>";

        }

       

        for ($i=0; $i<sizeof($arr); $i++) {

                $s .= '<TR>';

                $a = &$arr[$i];

                if (is_array($a))

                        for ($j=0; $j<sizeof($a); $j++) {

                                $val = $a[$j];

                                if (empty($val)) $val = '&nbsp;';

                                $s .= "        <TD>$val</TD>\n";

                        }

                else if ($a) {

                        $s .=  '        <TD>'.$a."</TD>\n";

                } else $s .= "        <TD>&nbsp;</TD>\n";

                $s .= "\n</TR>\n";

        }

        $s .= '</TABLE>';

        print $s;

}



?>

[/php]
 楼主| 发表于 2005-6-4 11:46:32 | 显示全部楼层

一个实利

[php]
<?php
        Session_Start();
        include_once('dblink/mylink.php');
        $sql='select cpdmmx,cpdh,cpmc,cptz from cpjcsjcx';
        $rs = $db->Execute($sql);
        $fd=$db->GetAll($sql);
        $bb=$rs->RecordCount();
        $ppage=8;
        $tpage=floor($bb/$ppage)+1;
        $pagem=$bb % $ppage;
        //$db->close();
        //echo $pagem;
        //echo $tpage;
$htmm='<html>';
$htmm.='<head>';
$htmm.="<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>";
$htmm.='<title>产品包装数据</title>';
$htmm.='</head>';
$htmm.='<body>';
$htmm.="<u><div align='center'>产品包装数据输入</div></u>";
$htmm.="<hr align='center' size='2'>";
$htmm.='</body></html>';
echo $htmm;

include_once('editedtohtml.inc.php');
$fieldheader=array('标志','产品代号','产品名称','包装方式','单位','包装数量','单台重量','包装总重量','包装物单价');
$edit=array('dw','bzfs','dtzl','bzzzl','bzwdj');
$mydim="border=2 cellpadding=3 bgcolor='#F1F1F1'";
$askpage=1;
$ppage=4;
$num_of_rows_per_page=4;
if (isset($_GET['next_page'])) $curr_page = $_GET['next_page'];
if (empty($curr_page)) $curr_page = 1; ## at first page
$askpage=$curr_page;
$ppage=$num_of_rows_per_page;
$rs = $db->ageExecute($sql, $num_of_rows_per_page, $curr_page);
if (!$rs) die('Query Failed');
if (!$rs->EOF && (!$rs->AtFirstPage() || !$rs->AtLastPage()))
   {rs2html(&$rs,$edit,$askpage,$ppage,$mydim,$fieldheader);
    echo "<hr align='left' size='3' width='100%' noshade>";
    if (!$rs->AtFirstPage())
       {$myxx="<a href=";
        $myxx.=$PHPSELF.'?next_page='.($rs->AbsolutePage()-1);
        $myxx.=">上一页</a>";
        echo $myxx;
        }
     if (!$rs->AtLastPage())
        {$myxx="<a href=";
         $myxx.=$PHPSELF.'?next_page='.($rs->AbsolutePage()+1);
         $myxx.=">下一页</a>";
         echo $myxx;
         }
     };
$htmn='</table>';
$htmn.="<hr align='left' size='3' width='100%' noshade>";
$htmn.="<input type='submit' name=\"tj\" value='提交'>";
$htmn.="<hr align='left' size='3' width='100%' noshade>";
$htmn.="</form></body></html>";
echo "$htmn\n\n";

echo $_POST["tj"];
print_r($_POST);
//session_write_close();
//$db->close();
?>
[/php]
回复 支持 反对

使用道具 举报

发表于 2005-6-17 15:19:27 | 显示全部楼层
很烂的说....
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表