很多时候,希望调用多个模型下的最新内容,但是因为V9的模型使用分表储存,使用GET来IN catid却要连表查询十分麻烦,所以使用下面的办法即可搞定:
第一步:
phpcms\libs\functions\extention.func.php
里面增加
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
function
news(
$limit
=
""
,
$modelid
=
""
,
$where
=
""
)
{
$db
=pc_base::load_model(
'content_model'
);
if
((
strpos
(
$modelid
,
","
)>=0))
{
$modelid
=
explode
(
","
,
$modelid
);
$midarr
=
$dot
=
''
;
foreach
(
$modelid
as
$mid
)
{
$midarr
=
$midarr
.
$dot
.
$mid
;
$dot
=
','
;
}
$sq
=
"`modelid` IN ({$midarr})"
;
}
else
{
$sq
=
"`modelid`={$modelid}"
;
}
$db
->table_name=
'v9_model'
;
$models
=
$db
->select(
$sq
,
"tablename"
);
$sql
=
''
;
$lianhe
=
''
;
foreach
(
$models
as
$name
)
{
$sql
=
$sql
.
$lianhe
.
"SELECT id,catid,title,url,inputtime FROM v9_{$name['tablename']}"
;
$lianhe
=
' UNION ALL '
;
}
$time
=time();
$sql
=
$sql
.
" "
.
$where
.
" order by inputtime desc limit "
.
$limit
;
$allnews
=
$db
->query(
$sql
);
while
(
$r
=
$db
->fetch_array(
$allnews
))
{
if
(
$keyfield
)
{
$key
=
$r
[
$keyfield
];
$array
[
$key
] =
$r
;
}
else
{
$array
[] =
$r
;
}
}
return
$array
[0];
}
|
以上代码需要注意的是里面表前缀,这里是V9,使用的时候改成你自己的表前缀,默认是V9
第二步:
调用的方式为:
1
2
3
4
5
|
<?php
$recent
=news(“0,8“,”1,12“);?>
{loop
$recent
$r
}
<li><a href=”{
$r
[‘url’]}” target=”_blank” title=”{
$r
[‘title’]}”>{str_cut(
$r
[title],54,”)}</a></li>
{/loop}
{/pc}
|
解释:
红色数字是调用条数,从0开始调用8条
蓝色数字模型ID,不是栏目的ID,多个模型用,分开
本文转自 gutaotao1989 51CTO博客,原文链接:http://blog.51cto.com/taoyouth/1609769