2011睡美人无删减图解:php是怎样实现的函数

来源:百度文库 编辑:高校问答 时间:2024/04/28 19:16:03
有谁知道类似http://www.ryp.cn/Products/
这个行业分类下面是怎么实现的呀.即产品库里面的分类

想要的是php技术函数
是怎样重复循环下来的.

最好有例子

这个你可以这样来想,每一个分类都是相同的,只是内容不同而已。先定义一个模板,这个模板就是一种表格的格式,定义显示几行几列,每一个分类就是内容不同,因此只在那些需要改变的地方加上PHP代码就行。
下面是一个例子:(用了我自己写的表格类,类的源代码你可以自己看看)
//////////////mysql_DB.inc的内容:///////////////
<?php
class mysql_DB
{
var $db;

function connectDB($server,$username,$userpwd,$dbname)
{
if(!empty($server) && !empty($username) && !empty($dbname))
{
$this->db=mysql_connect($server,$username,$userpwd);
mysql_select_db($dbname,$this->db);
return $this->db;
}
else
{
return 0;
die("数据库连接失败!请检查数据库配置或是否输入了错误的数据库信息!");
}
}

function db_query($query)
{
$result=mysql_query($query);
return $result;
}
}
?>
////////////////genTable.inc的内容://///////////
<?php
class ld_table
{
var $css; //CSS file name(stored as string)
var $css_name;
var $border; //Size of border of Table
var $bd_color; //Color of the border of Table
var $bg_color; //BG Color of Table
var $tbl_context; //Established Table(stored as string)
var $cellpadding; //Cellpadding of Table
var $cellspacing; //Cellspacing of Table
var $height; //Height of Table
var $width; //Width of Table

var $html_table;
var $html_tr;
var $html_td;

#variable about Database(Only on MySQL)
var $db;

#Constructor
function ld_table($height='30',$width='100',$cellpadding='1',$cellspacing='1',$border='1',$bd_color='',$bg_color='',$css='')
{
include "htmlTags.php";
$this->height=$height;
$this->width=$width;
$this->cellpadding=$cellpadding;
$this->cellspacing=$cellspacing;
$this->css=$css;
$this->border=$border;
$this->bd_color=$bd_color;
$this->bg_color=$bg_color;
$this->tbl_context='';

$this->html_table=$html_table;
$this->html_tr=$html_tr;
$this->html_td=$html_td;
}

#读进CSS并放入类成员中
function css_op($css_src)
{
$readed_css=@file("$css_src");
if(empty($readed_css))
;
else
{
$this->css='';//清空,另放css文件的内容
foreach($readed_css as $val)
{
$this->css.=$val;
}
$p1=strpos($this->css,'.');
$p2=strpos($this->css,'{');
$s=substr($this->css,$p1+1,$p2-$p1-1);
$this->css_name=trim($s);
}
}

#Param input: align=[center|left|right]
# css:input css file(Optional)
function initTable($align='center')//建立任何一个表格一开始均需调用
{
$this->css_op($this->css);
$this->tbl_context="<style type=\"text/css\">$this->css</style>";

$temp=ereg_replace('{CLASS}',$this->css_name,$this->html_table);
$temp=ereg_replace('{ALIGN}',$align,$temp);
$temp=ereg_replace('{BORDER}',$this->border,$temp);
$temp=ereg_replace('{BD_COLOR}',$this->bd_color,$temp);
$temp=ereg_replace('{BG_COLOR}',$this->bg_color,$temp);
$temp=ereg_replace('{C_PADDING}',$this->cellpadding,$temp);
$temp=ereg_replace('{C_SPAC}',$this->cellspacing,$temp);
$temp=ereg_replace('{WIDTH}',$this->width,$temp);
$temp=ereg_replace('{HEIGHT}',$this->height,$temp);

$this->tbl_context.=$temp;
}

#Atomic Operation
function add_row()
{
$this->tbl_context.=$this->html_tr;
}

#Atomic Operation
#Param $in_array:每行各个列的内容
# $col_options:列设置(输入一个数组,按照如下顺序)
# array('CSS源文件',
# '对齐方式',
# '列宽',
# '列高',
# '背景色')
# 该数组如果不设置,则采用默认设置
function add_cols($in_array,$col_options=array())
{
if(count($col_options)==0)
$col_options=array('','center','','','');
$temp=ereg_replace('{CLASS}',$col_options[0],$this->html_td);
$temp=ereg_replace('{ALIGN}',$col_options[1],$temp);
$temp=ereg_replace('{WIDTH}',$col_options[2],$temp);
$temp=ereg_replace('{HEIGHT}',$col_options[3],$temp);
$temp=ereg_replace('{BG_COLOR}',$col_options[4],$temp);

if(is_array($in_array))
{
foreach($in_array as $val)
{
$this->tbl_context.=$temp.$val;
}
}
else
{
$this->tbl_context.=$temp.$in_array;//输入非数组的情况
}
}

###########用来创建表头############
####参数$table_header可以是资源类型和数组类型
#### 若是资源类型则必须是mysql_query返回
#### 的资源类型.也可以调用mysql_DB类的db_query
#### 方法.
function create_table_header($table_header)
{
if (is_resource($table_header))
{
$header_arr=array();
for ($i=0;$i<mysql_num_fields($table_header);$i++)
array_push($header_arr,mysql_field_name($table_header,$i));
$this->add_row();
$this->add_cols($header_arr);
}
else if (is_array($table_header))
{
$this->add_row();
$this->add_cols($table_header);
}
else
echo ("传入了错误的参数!期望是数组或资源类型!<BR>");
}

#创建数据表
function createDBTable($result) //$result为返回的结果资源
{
while($row=mysql_fetch_array($result))
{
$this->add_row();
foreach($row as $key=>$val)
if(ereg('[0-9]',$key))//只要键为数字的值
$this->add_cols("$row[$key]");
}
}

function table_display()
{
echo "$this->tbl_context";
}
}//end of class
?>
////////////////htmlTags.php的内容://///////////
<?php
$html_table="<table class=\"{CLASS}\" align=\"{ALIGN}\" border=\"{BORDER}\" bordercolor=\"{BD_COLOR}\" ";
$html_table.="bgcolor=\"{BG_COLOR}\" cellpadding=\"{C_PADDING}\" cellspacing=\"{C_SPAC}\" ";
$html_table.="width=\"{WIDTH}\" height=\"{HEIGHT}\" />";

$html_tr="<tr />";

$html_td="<td class=\"{CLASS}\" align=\"{ALIGN}\" width=\"{WIDTH}\" height=\"{HEIGHT}\" bgcolor=\"{BG_COLOR}\" />";
?>

<?php
include "../DB/mysql_DB.inc";
include 'genTable.inc';
$db=new mysql_DB;
$db->connectDB('localhost','root','','goods');
$t=new ld_table('30','400','1','1','0','#000000','#0000ff','font.css');
$t->initTable();
$t->create_table_header($typeName.$typeQua)); //$typeName为产品类型名,$typeQua为该类产品数量。你可以用循环来每次改变这两个量,就构建出了不同的表头。(这里没有循环,所以只构建出一个表)这个方法是构造表头。
$t->createDBTable($db->db_query($query)); //$query是发给数据库的请求,就是请求这种商品的名称和URL。这个方法是构建一个表的表体。
$t->table_display();
?>

这个循环体你自己构造,应该不麻烦,思想就是上面说的。