2014-03-26 72 views
0

我刚刚在完美工作的项目中使用了此代码,但我注意到第一条记录始终不显示。例如。 SQL查询列出了15个结果,但输出仅显示14,缺少第一条记录。任何想法为什么?循环记录集PHP

<?php 
$result = mysql_query($sql); 

//first put all the results into an array so we can look backward and see previous items 
$resultSet = array(); 
while($record = mysql_fetch_array($rs2Dfiles)) { 
$resultSet[] = $record; 
} 

for ($i = 0 ; $i < count($resultSet) ; $i++) { 
if ($i == 0) { 
//for the first item, show the category name 
echo '<div class="Box"><div class="BoxHeader gfgreen"><h3>'.$resultSet[$i]['ftCatName'].'</h3> </div>'; 
} else if ($resultSet[$i]['ftCatName'] != $resultSet[$i-1]['ftCatName']) { 
//every time we encounter a new category, display a new line and show the category name 
echo '</div><div class="Box"><div class="BoxHeader gfgreen"><h3>'.$resultSet[$i] ['ftCatName'].'</h3></div>'; 
} 

echo '<p class="Document"><a title="View file details" href="download.php?id='.$resultSet[$i] ['DownloadID'].'">'.$resultSet[$i]['DownloadName'].'</a> ('.formatSize( filesize('DOCS/'.$resultSet[$i]['DownloadFilename'].'')).')</p>'; 
} 
echo '</div>'; 
?> 
+0

用查询给出整个代码.....! –

+1

注意:'mysql_ *'已弃用。你应该开始使用'mysqli_ *'或'PDO'。 –

回答

1

您可以使用一个foreach,可以让事情变得更简单吗?

$last = null; 

foreach($resultSet as $resultRow) 
{ 
    if ($resultRow != $last) 
    { 
     echo 'your html content goes here'; 
     echo $resultRow['propertyName']; 
    } 

    $last = $resultRow 
}