0

I have just used this code in a project which works perfectly, but I have noticed that the first record is always not displayed. Eg. the SQL query lists 15 results, but the output only displays 14, missing the first record. Any ideas why?

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

?>
3
Contributors
7
Replies
29
Views
3 Years
Discussion Span
Last Post by diafol
0

Before the loop try to see what you're actually getting from the database with this:

echo '<pre>';
var_dump($resultSet);
echo '</pre>';

In the loop you can try this to see what each $i block is giving you:

echo '<pre>';
var_dump($resultSet[$i]);
echo '</pre>';

If that doesn't show you the problem, try posting the result of one of those var_dumps in here so we can have a look. There doesn't seem to be any mistakes in there for the intended result.

Also, I've seen somewhere on stackoverflow that doing this:

for ( $i = 0 ; $i < count($resultSet) ; $i++ )

is slower than doing this:

$myCount = count($resultSet);
for ( $i = 0 ; $i < $myCount; $i++ )

Don't ask me why, if I remember correctly they iterated a few 1000 times and the result proved their theory.

0

You seem to have over-egged this. I'm not sure that you need to place all this into an array. You're looping over the same data again.

0

Hi diafol, I tried your code but it would not work for some reason but someone else suggested this code that seemed to work straight away, apart from this issue of missing the first record.

0

Hi diafol, I tried your code

WHat code???

If your SQL is ordered by category, this should work:

<?php
function newCat($category)
{
    return "<div class='Box'><div class='BoxHeader gfgreen'><h3>$category</h3></div>";       
}

function endCat()
{
    return '</div>'; 
}

function addRecord($data)
{
    return "<p class='Document'><a title='View file details' href='download.php?id={$data['DownloadID']}'>{$data['DownloadName']}</a> (".
    formatSize( filesize('DOCS/'. $data['DownloadFilename']) ) . ")</p>";
}

//connection and sql build here first
$result = mysql_query($sql);
$cat = '';
$output = '';
if(mysql_num_rows($result))
{
    while($r = mysql_fetch_array($result)) {
        if($r['ftCatName'] != $cat)
        {
            if($cat !='') $output .= endCat();
            $output .= newCat($r['ftCatName']);
        }
        $output .= addRecord($r);
        $cat = $r['ftCatName'];
    }
    $output .= endCat();
}
echo $output;
?>
1

If for some reason you can't order by category, then the following should serve, just as well:

while($r = mysql_fetch_array($result)) {
    $arr[$r['ftCatName']] = $r;
}

foreach($arr as $key=>$records)
{
    $output .= newCat($key);
    foreach($records as $record)
    {
        $output .= addRecord($record);
    }
    $output .= endCat();
}

echo $output;
0

This is the var_dump($resultSet)

array(1) {
  [0]=>
  array(10) {
    [0]=>
    string(3) "230"
    ["DownloadID"]=>
    string(3) "230"
    [1]=>
    string(5) "test2"
    ["DownloadName"]=>
    string(5) "test2"
    [2]=>
    string(0) ""
    ["DownloadFilename"]=>
    string(0) ""
    [3]=>
    string(3) "101"
    ["DownloadFiletype"]=>
    string(3) "101"
    [4]=>
    string(4) "MP50"
    ["ftCatName"]=>
    string(4) "MP50"
  }

But if I echo $totalRows_rs2Dfiles; it shows 2x records

0

someone else suggested this code that seemed to work straight away

Ask them about it in that thread? In the same way, if you tried my code (I don't know from where), you could have asked about it in that thread.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.