Jump to content

Count and rows code

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
newphpcoder

newphpcoder

    Programming Professional

  • Members
  • PipPipPipPipPipPip
  • 479 posts
What is the explanation or meaning of this code:


$totalarr = count($opname) - 1;


and


$totalrows = mysql_num_rows($result);

        $trows = $totalrows - 1;


Thank you

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
If you are going through a loop, all arrays start and index zero not one. Therefor we need to subtract one from the count to work in the loop, for example:

$names =  array('John', 'Cassy', 'Sven');
$ncount = count($names) - 1;
for($i = 0; $i <= $ncount; $i++) {
    print $names[$i];
}

as you see, $i will be zero instead of one (to access the first key in the array) and end at $names[2] instead of $names[3] which does not exist.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.