创建表:
CREATE TABLE `DBTEST` (
`id` INT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 50 ) ,
`sex` VARCHAR( 10 ) ,
PRIMARY KEY ( `id` )
);
以下主要是看样式,自己调节样式内的width 大小,想一行显示多少都可以。
至于分页什么的,都是在sql语句那加个 limit 0, 20 ; 再加个分页显示函数。
(其实这只是一般应用的方法,我自己是用模板来解决的,考虑到楼主可能没用过模板,
所以在此只说明思路,俱体的内容,就由楼主自己去做了)
----code 存为php文件-----------------------------------------------
include("include/db_mysql.php");
$db = new db_test();
$query = "select * from DBTEST";
$query = $db->query($query);
while($dbq_rec = $db->fetch_array($query)){
$t1 = $dbq_rec['name'];
$t2 = $dbq_rec['sex'];
?>
=$t1;?> |
=$t2;?> |
楼上的理解不对,人家是要把数据读取,然后在页面上横排
一般这样的情况我们都用表格来实现,我这里有个显示图片的函数,基本功能也是把图片横排,你可以参考下。
function listpic($page,$line,$colum,$nav){ //生成图片表格
global $imagedir,$pagesize;
$this->dir=$imagedir;
$this->size=$pagesize;
if ($this->gettotalnum()==0){
echo "当前没有上传图片!请稍候访问";
}else{
$this->i=1;
$this->k=1;
$this->endness=$this->start-$line*$colum+1;
$this->start=$page*$this->size;
$this->start=$this->gettotalnum()-($page-1)*$this->size;
$this->end=$this->start-$this->size;
if($this->end<=0) $this->end=1;
echo "
| \n";|||
SQL语句实现横排?
有一个表:
BH GZLB JE
----------------------
001 A 100
001 B 150
001 C 110
002 A 99
002 B 180
002 C 150
003 A 160
003 B 170
003 C 130
用SQL语句如何将上面的表变成下面横向的排列方式呢。
BH A B C
-------------------
001 100 150 110
002 99 180 150
003 160 170 130
(GZLB 里面的字段是动态的,不是只有A,B,C 可能还有A,B,C,D,E...)
---------------------------------------------------------------
if exists(select name from sysobjects where name='tblA' and xtype='U')
drop table tblA
create table tblA(
BH char(3) not null,
GZLB varchar(3) not null,
JE int not null
)
go
insert tblA values('001', 'A', 100)
insert tblA values('001', 'B', 150)
insert tblA values('001', 'C', 110)
insert tblA values('002', 'A', 99)
insert tblA values('002', 'B', 180)
insert tblA values('002', 'C', 150)
insert tblA values('003', 'A', 160)
insert tblA values('003', 'B', 170)
insert tblA values('003', 'C', 130)
declare @sql varchar(600)
set @sql = 'select'
select distinct GZLB as 'GZLB' into tmp from tblA
select @sql = @sql +' max(case GZLB when '''+ GZLB +''' then JE else null end) as '''+ GZLB +''',' from tmp
set @sql = substring(@sql, 1, len(@sql)-1) +' from tblA group by BH'
exec(@sql)
---------------------------------------------------------------
用游标
declare @sqlstr varchar(2000)
declare @gzlb
select sqlstr='select bh'
declare cgzlb cursor for select gzlb from table
create table #a
(bh char(3))
open cgzlb
fetch next from cgzlb into @gzlb
while (fetch_status<>-1)
begin
exec('alter table #a add '+@gzlb +' char(1)')
exec('insert into #a (bh,'+@gzlb+' select bh,je from table where gzlb='''+@gzlb+'''')
select @sqlstr=@sqlstr+',sum(isnull('+@gzlb+'))'
fetch next from cgzlb into @gzlb
end
close cgzlb
deallocate cgzlb
select @sqlstr=@sqlstr+' from #a group by bh order by bh
execute(@sqlstr)
---------------------------------------------------------------
declare @sql varchar(6000)
set @sql = 'select '
select @sql = @sql +' sum(case GZLB when '''+ GZLB +''' then JE else null end) ['+ GZLB +'],' from (select distinct GZLB as GZLB from tblA) tmp
set @sql = substring(@sql, 1, len(@sql)-1) +' from tblA group by BH'
exec(@sql)