Forum Moderators: coopster

Message Too Old, No Replies

WHILE Loop driving me insane.

         

skankyrich

5:24 pm on May 30, 2009 (gmt 0)

10+ Year Member



Hi there,

I've been trying to work out what's wrong with my WHILE loop all afternoon.

It recognises that there are three results to scroll through and does so; but it only seems to pull the top row of results, giving me the page you see here:

<snip>

I've printed the results from the query, and the array is correct, so the problem must be in the loop. But I can't seem to find the flaw, and I had exactly the same problem when I tried to use the results as an array.

Can anyone help?

Rich

***

$i=0;
while ($i < $num)
{
$date=mysql_result($result,$i,"date");
$opponents=mysql_result($result,$i,"opponents");
$home=mysql_result($result, $i, "home");
$venue=mysql_result($result,$i,"venue");
$details=mysql_result($result,$i,"details");
$time=mysql_result($result,$i,"time");
$teamone=mysql_result($result,$i,"team_one_score");
$teamtwo=mysql_result($result,$i,"team_two_score");
$result=mysql_result($result,$i,"result");
$matchreport=mysql_result($result,$i,"result_url");
$photos=mysql_result($result,$i,"photo_url");
$notes=mysql_result($result,$i,"notes");

echo "<h4>";
echo date('l, j<\s\up>S</\s\up> F Y', strtotime($date));
if ($home=="0")
{
echo "</h4> <h4>" . $opponents . " v Cavendish Cavaliers at " . $venue . "</h4><p>" . $details . " ";
}
else
{
echo "</h4> <h4>Cavendish Cavaliers v " . $opponents . " at " . $venue . "</h4><p>" . $details . " ";
}

echo date('g.ia', strtotime($time));
echo "</p><p>" . $teamone . "</p><p>" . $teamtwo . "</p><p><i>" . $result . "</i></p>";

if ($matchreport=="")
{
echo "<p>" . $notes . "</p><hr width=&#34;25%&#34;/>";
}
else
{
echo '<p><a href="' . $matchreport . '">Full match report</a>';
if ($photos=="")
{
echo ".</p>";
}
else
{
echo ' and <a href="' . $photos . '">photos</a>.</p>';
}
}
echo "<p>" . $notes . "</p>";

$i++;
}

[edited by: eelixduppy at 6:11 pm (utc) on May 30, 2009]
[edit reason] no URLs, please [/edit]

kaidok

5:43 pm on May 30, 2009 (gmt 0)

10+ Year Member



It seems like mysql_result is not working correctly, anyway you could do it with mysql_fetch_row, getting from the returned array the values you want, or you could even work around the problem with SELECT queries. ¿Right?

Hope this helps.

skankyrich

6:00 pm on May 30, 2009 (gmt 0)

10+ Year Member



'It seems like mysql_result is not working correctly...'

Yes, you're right. I've had problems with it before, actually, when I was trying to get a page to show based on user inputs. The data is all there in the array (see <snip>p for confirmation); I just don't seem to be able to access it properly.

I've tried fetch_row and fetch_assoc, but with no luck.

I'll have a think about SELECT queries, but I don't see how I'll be able to use a query to get the results I want.

Infuriating.

[edited by: eelixduppy at 6:12 pm (utc) on May 30, 2009]
[edit reason] no URLs, please [/edit]

idfer

9:56 pm on May 30, 2009 (gmt 0)

10+ Year Member



I assume you have a call to mysql_query at some point before the loop? Something like:

$result = mysql_query(...);

Anyways, you're re-assigning the query handle ($result) half-way through your calls to mysql_result(), 4th from bottom actually. That would cause problems. :-)

Also as the documentation on mysql_result() suggests, you should consider using mysql_fetch_array() instead. Hope this helps.

skankyrich

11:05 pm on May 30, 2009 (gmt 0)

10+ Year Member



'Anyways, you're re-assigning the query handle ($result) half-way through your calls to mysql_result(), 4th from bottom actually. That would cause problems. :-)'

idfer - that's a cracking spot. When I pulled the results from the _array, I assigned them in the same way, which is why I got the same results when I tried it that way.

Needless to say, it works now. If you ever fancy playing for us, you're very welcome ;)

I'm going to go back and revisit the old admin code I wrote to see if I did something similar when I was trying to work out my problems with that section. I'm immensely grateful for your help, particularly as I'm just learning php, and no doubt I'll be troubling you guys again in the future!

Thanks again,

Rich