PHP遍历的问题?麻烦大家帮看一下。

luobutou0827 2012-02-10 03:16:32
我有个方法从mysql中取出一组数据,可是用while,for,foreach遍历出来的数据都多了,麻烦各位给看一下
1:取结果的方法

function queryUserReport ( $userid ) {

$resUserReportList = "" ;

$queryUserReport = "Select reportId, userid, reportCheckId, email, createtime, constitutiontype From userreport Where userid = '".$userid."' " ;
//echo "query:".$queryConstiutContent."<br>";

$resUserReportList = $this->user_db->querySQL ( $queryUserReport ) ;

if ($resUserReportList){
$resUserReportList = $this->user_db->get_Array($resUserReportList);
//print_r($resConstiutContent);
return $resUserReportList ;
} else {
return $this->user_db->print_last_error(TRUE);
}

$this->user_db->Close() ;

}

2:引用mysql的类

function querySQL($sql){
$this->last_query=$sql;

$result=mysql_query($sql);
if (!$result){
$this->last_error=mysql_error();
return FALSE;
}else {
$this->row_count=mysql_num_rows($result);
return $result;
}
}
function get_Array($result){
if (!$result){
$this->last_error="Invalid resource identifier passed to get_Array() function.";
return FALSE;
}else {
$row=mysql_fetch_array($result);
if ($row==FALSE){
$this->last_error=mysql_error();
return FALSE;
}else {
return $row;
}
}
}

3:取出的结果

Array
(
[0] => 3
[reportId] => 3
[1] => 414
[userid] => 414
[2] => 201209021468
[reportCheckId] => 201209021468
[3] =>
[email] =>
[4] => 2012-02-09 11:30:54
[createtime] => 2012-02-09 11:30:54
[5] => 1
[constitutiontype] => 1
)


4:页面获取数据代码

//获取数据
$arrayUserReportList = $dbConstituModel->queryUserReport($_SESSION['userid']);

while ( $arrayUserReportList = $arrayUserReportList ){
echo "<tr>";
echo "<td>".$arrayUserReportList['reportId']." </td>";
echo "<td>".$arrayUserReportList['createtime']." </td>";
echo "<td><a href=\"/constest/constitutionreport/myreport_".$arrayUserReportList['reportCheckId'].".html\" target=\"_blcnk\">查看报告</a> </td>";
echo "</tr>";
}
...全文
143 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuzuning 2012-02-10
  • 打赏
  • 举报
回复
$arrayUserReportList = $dbConstituModel->queryUserReport($_SESSION['userid']);

echo "<tr>";
echo "<td>".$arrayUserReportList['reportId']." </td>";
echo "<td>".$arrayUserReportList['createtime']." </td>";
echo "<td><a href=\"/constest/constitutionreport/myreport_".$arrayUserReportList['reportCheckId'].".html\" target=\"_blcnk\">查看报告</a> </td>";
echo "</tr>";
luobutou0827 2012-02-10
  • 打赏
  • 举报
回复
用foreach出来了6条记录,可是我返回只有一条记录啊,打印的记录也在上面,怎么回事啊。郁闷。
[Quote=引用 6 楼 heyli 的回复:]
你的get_Array 返回的是一个一维数组 改成下面 返回一个二维的
while($rs=mysql_fetch_array($result)){
$row[]=$rs;
}


foreach($arrayUserReportList as $val){
echo "<tr>";
echo "<td>".$val['reportId']."&nbsp;</td>……
[/Quote]
黄袍披身 2012-02-10
  • 打赏
  • 举报
回复
mysql_fetch_array 改成用mysql_fetch_assoc()
heyli 2012-02-10
  • 打赏
  • 举报
回复
你的get_Array 返回的是一个一维数组 改成下面 返回一个二维的
while($rs=mysql_fetch_array($result)){
$row[]=$rs;
}


foreach($arrayUserReportList as $val){
echo "<tr>";
echo "<td>".$val['reportId']." </td>";
echo "<td>".$val['createtime']." </td>";
echo "<td><a href=\"/constest/constitutionreport/myreport_".$val['reportCheckId'].".html\" target=\"_blcnk\">查看报告</a> </td>";
echo "</tr>";
}

大概这样
luobutou0827 2012-02-10
  • 打赏
  • 举报
回复
用for遍历出了12条,正好输出的就是12条,while不是while ($row = mysql_fetch_array())吗,我的 $arrayUserReportList这个变量就是fetch输出的结果啊,foreach怎么写呢,我用foreach出来的结果一样。

Array
(
[0] => 3
[reportId] => 3
[1] => 414
[userid] => 414
[2] => 201209021468
[reportCheckId] => 201209021468
[3] =>
[email] =>
[4] => 2012-02-09 11:30:54
[createtime] => 2012-02-09 11:30:54
[5] => 1
[constitutiontype] => 1
)



[Quote=引用 4 楼 heyli 的回复:]
你不觉得你 while ( $arrayUserReportList = $arrayUserReportList ){ 这一段有问题吗?
改成 for 或 foreach吧
[/Quote]
heyli 2012-02-10
  • 打赏
  • 举报
回复
你不觉得你 while ( $arrayUserReportList = $arrayUserReportList ){ 这一段有问题吗?
改成 for 或 foreach吧
luobutou0827 2012-02-10
  • 打赏
  • 举报
回复
还是不行啊,我这用WHILE,数据库只有1条数据,但是遍历出了N多条记录。

<?
if ( isset($arrayUserReportList) && !empty($arrayUserReportList) ) {
//for($i=0; $i<count($arrayUserReportList); $i++){
//for($i=0; $i<2; $i++){
//foreach( $arrayUserReportList as $key=>$value ) {
while ( $arrayUserReportList = $arrayUserReportList ){
echo "<tr>";
echo "<td>".$arrayUserReportList['reportId']." </td>";
echo "<td>".$arrayUserReportList['createtime']." </td>";
echo "<td><a href=\"/constest/constitutionreport/myreport_".$arrayUserReportList['reportCheckId'].".html\" target=\"_blcnk\">查看报告</a> </td>";
echo "</tr>";
}
} else {
echo "<tr><td colspan=\"3\">您还没有体质测评报告!</td></tr>";
}

?>
whxblue 2012-02-10
  • 打赏
  • 举报
回复
$row=mysql_fetch_array($result);
这条语句稍作修改即可:
$row=mysql_fetch_array($result,MYSQL_NUM);//或者MYSQL_ASSOC
详细意思参考PHP API文档。

heyli 2012-02-10
  • 打赏
  • 举报
回复
遍历出来的数据都多了 这是什么意思

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧