php,怎么输出mysql表中每组10条数据?
<?$tr_list=array();$query="select*frommemberwherefenleiin(1,2,3,4)orderbyiddesc";$res...
<?
$tr_list = array();
$query="select * from member where fenlei in (1,2,3,4) order by id desc";
$result=mysql_query($query,$db);
while ($arr=mysql_fetch_array($result))
{
$tr_list[$arr['fenlei']] .="$arr[name]";
}
echo "$tr_list[1]";
echo "$tr_list[2]";
echo "$tr_list[3]";
echo "$tr_list[4]";
?>
以上代码会把四组所有数据全部输出,如何写每组数据都能输出10条?
用 $query="select * from member where fenlei in (1,2,3,4) order by id desc Limit 0,10";
只输出最后一组10条,
用 $query="select * from member where fenlei in (1,2,3,4) order by id desc Limit 0,40";
也会只输出最后一组40条,
怎么写才能每组都输出10条?
fenlei下有若干条,fenlei值等于1、2、3、4,就是想fenlei=1的输出显示10条,fenlei=2 的显示10条,fenlei=3的显示10条,fenlei=4的显示10条 展开
$tr_list = array();
$query="select * from member where fenlei in (1,2,3,4) order by id desc";
$result=mysql_query($query,$db);
while ($arr=mysql_fetch_array($result))
{
$tr_list[$arr['fenlei']] .="$arr[name]";
}
echo "$tr_list[1]";
echo "$tr_list[2]";
echo "$tr_list[3]";
echo "$tr_list[4]";
?>
以上代码会把四组所有数据全部输出,如何写每组数据都能输出10条?
用 $query="select * from member where fenlei in (1,2,3,4) order by id desc Limit 0,10";
只输出最后一组10条,
用 $query="select * from member where fenlei in (1,2,3,4) order by id desc Limit 0,40";
也会只输出最后一组40条,
怎么写才能每组都输出10条?
fenlei下有若干条,fenlei值等于1、2、3、4,就是想fenlei=1的输出显示10条,fenlei=2 的显示10条,fenlei=3的显示10条,fenlei=4的显示10条 展开
4个回答
展开全部
下面是字符串
$query="select * from member where fenlei in (1,2,3,4) order by id desc";
$result=mysql_query($query,$db);
$len=1;
$i = 0;
while ($res=mysql_fetch_array($result))
{
$arraylist[$len].=$res['fenlei'].'-'.$res[name].'<br>';
$i++;
if(is_int($i/10))
{
$len++;
$i = 0;
}
}
echo $arraylist[1];
echo $arraylist[2];
echo $arraylist[3];
echo $arraylist[4];
下面是数组,
$query="select * from member where fenlei in (1,2,3,4) order by id desc";
$result=mysql_query($query,$db);
$len=1;
$i = 0;
while ($res=mysql_fetch_array($result))
{
$arraylist[$i] = $res[name];
$i++;
$tr_list[$len] = $arraylist;
if(is_int($i/10))
{
$len++;
$i = 0;
}
}
print_r($tr_list[1]);
print_r($tr_list[2]);
print_r($tr_list[3]);
print_r($tr_list[4]);
$query="select * from member where fenlei in (1,2,3,4) order by id desc";
$result=mysql_query($query,$db);
$len=1;
$i = 0;
while ($res=mysql_fetch_array($result))
{
$arraylist[$len].=$res['fenlei'].'-'.$res[name].'<br>';
$i++;
if(is_int($i/10))
{
$len++;
$i = 0;
}
}
echo $arraylist[1];
echo $arraylist[2];
echo $arraylist[3];
echo $arraylist[4];
下面是数组,
$query="select * from member where fenlei in (1,2,3,4) order by id desc";
$result=mysql_query($query,$db);
$len=1;
$i = 0;
while ($res=mysql_fetch_array($result))
{
$arraylist[$i] = $res[name];
$i++;
$tr_list[$len] = $arraylist;
if(is_int($i/10))
{
$len++;
$i = 0;
}
}
print_r($tr_list[1]);
print_r($tr_list[2]);
print_r($tr_list[3]);
print_r($tr_list[4]);
更多追问追答
追问
谢谢您的回复,刚测试了一下,只输出了“Array”这样的字样
追答
不会呀,我给了你两种方法,一种是数组的,一种是字符串的呀,
我这边测试都是好的,你用的哪种方法
展开全部
这样写就可以了,不会受每个分类的记录数影响
$tr_list = array();
$fenlei = array(1,2,3,4);
foreach($fenlei as $id){
echo 'fenlei'.$id.':<br>';
$tr_list[$id] = '';
$query="select * from member where fenlei='".$id."' order by id desc Limit 0,10";
$result=mysql_query($query,$db);
while($arr=mysql_fetch_array($result)){
$tr_list[$id] .= $arr['name'];
}
echo $tr_list[$id].'<br>';
}
$tr_list = array();
$fenlei = array(1,2,3,4);
foreach($fenlei as $id){
echo 'fenlei'.$id.':<br>';
$tr_list[$id] = '';
$query="select * from member where fenlei='".$id."' order by id desc Limit 0,10";
$result=mysql_query($query,$db);
while($arr=mysql_fetch_array($result)){
$tr_list[$id] .= $arr['name'];
}
echo $tr_list[$id].'<br>';
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<?
$tr_list = array();
$query="select * from member where fenlei in (1,2,3,4) order by id desc";
$result=mysql_query($query,$db);
$temp=0;
while ($arr=mysql_fetch_array($result))
{
$arraylist.=$arr['fenlei'].'-'.$arr[name].'<br>';
$temp++;
if($temp>=3){
$tr_list[] =$arraylist;
$temp=0;
}
}
echo "$tr_list[1]";
echo "$tr_list[2]";
echo "$tr_list[3]";
echo "$tr_list[4]";
?>
$tr_list = array();
$query="select * from member where fenlei in (1,2,3,4) order by id desc";
$result=mysql_query($query,$db);
$temp=0;
while ($arr=mysql_fetch_array($result))
{
$arraylist.=$arr['fenlei'].'-'.$arr[name].'<br>';
$temp++;
if($temp>=3){
$tr_list[] =$arraylist;
$temp=0;
}
}
echo "$tr_list[1]";
echo "$tr_list[2]";
echo "$tr_list[3]";
echo "$tr_list[4]";
?>
追问
谢谢您的回复,但是输出的结果还是不正确,不知道哪里有问题?而且也没有看到10的字样?如何会输出10条?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个不就是分页功能嘛,limit 0,10 limit 10,20 这样就是每组十个啦。。。想要分页类,代码太多不方便写。自己网上找。。。
追问
不是分页,你看清楚问题介绍哦
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |