Re: I'm missing something
| From: | Matijn Woudt | Date: | Sat, 05 May 2012 22:46:39 +0000 |
| Subject: | Re: I'm missing something | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-317806@lists.php.net to get a copy of this message | ||
On Sun, May 6, 2012 at 12:38 AM, Jim Giner <jim.giner@albanyhandball.com> wrote:
> I have a discrepancy in the number of elements in my arrays and can't see
> why.
>
> Here is some code:
> Note the lines with the ***
>
> *** $plyrs = 0;
> unset($plyrnames_ar);
> unset($js_names);
> unset($js_seeds);
> *** $rows = mysql_num_rows($qrslts);
> ***echo "in mysql there are $rows rows<br>";
> while ($row = mysql_fetch_array($qrslts))
> { // build the name value here
> $mi = ($row['MI']=='') ? '' : "
> ".$row['MI'];
> $nm = $row['LastName'].", ".$row['FirstName'].$mi;
> if ($row['srtdbls']=='X')
> {
> $mi = ($row['partMI']=='') ? '' : "
> ".$row['partMI'];
> $nm .= "/".$row['partLN'].", ".$row['partFN'].$mi;
> }
> *** $plyrs++;
> $plyrnames_ar[$nm] = $row['Draw_pos'];
> }
> *** echo "before sort there are ".count($plyrnames_ar)." in
> plyrsnames_ar<br>";
> ksort($plyrnames_ar);
> foreach ($plyrnames_ar as $nm=>$sd)
> {
> $js_names[] = $nm;
> $js_seeds[] = $sd;
> }
> *** echo "plyrs is $plyrs and there are ".count($js_names)." entries in
> js_rnames and ".count
> ***($plyrnames_ar)." entries in plyrnames_ar and ".count($js_seeds)." in
> js_seeds";
> exit();
>
> The problem is that my $plyrs field comes up 18, while in truth there are
> only 17 rows of data in my database.
> All of the other array-size counts echo out as being only 17, but my $rows
> and $plyrs fields come up as 18. I was having problems with my javascript
> showing an undefined array element and took a bit o time to determine what
> it was and where it was happening.
>
> Questions - does mysql_num_rows return a extra row that somehow doesn't get
> processed in the while loop? And if it does how come the counter ($plyrs)
> that I put into the loop comes up higher than it should.?
>
>
My guess would be that you end up with 2 rows having the same $nm,
overwriting the value that's already in $plyrnames_ar.
- Matijn