循环记录集PHP


looping recordset PHP

我刚刚在一个完美工作的项目中使用了这段代码,但我注意到第一个记录总是不显示。如。SQL查询列出了15个结果,但是输出只显示了14个,缺少了第一个记录。知道为什么吗?

<?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>';
?>

您可以使用foreach,可能会使事情更容易?

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