Jump to content

Archived

This topic is now archived and is closed to further replies.

RDKL PerFecT

php row? Problem?

Recommended Posts

Hi there, this is my code

// Perform MySQL query on only the current page number's results. The problem is it doesn't echo anything at all now, since I've tried to add this nrows thing to make each result numbered, any help would be greatly appreciated.

[code]
$sql = mysql_query("SELECT * FROM Players ORDER BY Points DESC LIMIT $from, $max_results");

for ($i=0;$i<$nrows;$i++)
    {
        $n = $i +1;        //add 1 so that numbers don't start with 0
        $row = mysql_fetch_array($sql);
        extract($sql);
        
    // Build your formatted results here.
    echo("<tr bgcolor=".$bgcolor."><td>");
    echo($row["Name"]);
    echo("</td><td>");
    echo($row["Nationality"]);
    echo("</td><td>");
    echo($row["Points"]);
    echo("</td></tr>");
    }
[/code]

Share this post


Link to post
Share on other sites
Where is $nrows being set? Based just off what you posted here, $nrows doesn't exist so the loop will never run. You would need to do something like ;
$nrows = mysql_fetch_rows($sql);

Or you could do;
[code]
$sql = mysql_query("SELECT * FROM Players ORDER BY Points DESC LIMIT $from, $max_results");
while($row = mysql_fetch_array($sql)){
    $n = $i +1;        //add 1 so that numbers don't start with 0
            
    // Build your formatted results here.
    echo("<tr bgcolor=".$bgcolor."><td>");
    echo($row["Name"]);
    echo("</td><td>");
    echo($row["Nationality"]);
    echo("</td><td>");
    echo($row["Points"]);
    echo("</td></tr>");
}
[/code]

With the above, the loop will run for each row returned by the query, and each row will be entered into $row for you. So there is no need for the for loop.

Share this post


Link to post
Share on other sites
[!--quoteo(post=362914:date=Apr 9 2006, 03:50 AM:name=IceHawk)--][div class=\'quotetop\']QUOTE(IceHawk @ Apr 9 2006, 03:50 AM) [snapback]362914[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Where is $nrows being set? Based just off what you posted here, $nrows doesn't exist so the loop will never run. You would need to do something like ;
$nrows = mysql_fetch_rows($sql);

Or you could do;
[code]
$sql = mysql_query("SELECT * FROM Players ORDER BY Points DESC LIMIT $from, $max_results");
while($row = mysql_fetch_array($sql)){
    $n = $i +1;        //add 1 so that numbers don't start with 0
            
    // Build your formatted results here.
    echo("<tr bgcolor=".$bgcolor."><td>");
    echo($row["Name"]);
    echo("</td><td>");
    echo($row["Nationality"]);
    echo("</td><td>");
    echo($row["Points"]);
    echo("</td></tr>");
}
[/code]

With the above, the loop will run for each row returned by the query, and each row will be entered into $row for you. So there is no need for the for loop.
[/quote]

I've found this code from another script of how to number things, which sets $nrows like you said. I now get it to each "$n.", which just shows up as 1. for each row instead of 1,2,3,4 etc. Any way I can fix this?

[code]$query = "SELECT url,linkname,id,description,hits FROM Links WHERE ok='1' ORDER by url desc";
$result = mysql_query($query)
    or die ("Couldn't execute query.");
$nrows = mysql_num_rows($result);[/code]

Share this post


Link to post
Share on other sites
If you're using my while loop, there's a slight mistake I just noticed. If you replace
$n = $i + 1;
with;
$n = $i++ + 1;
Then you should be good to go.

Share this post


Link to post
Share on other sites
[!--quoteo(post=363174:date=Apr 10 2006, 04:07 AM:name=IceHawk)--][div class=\'quotetop\']QUOTE(IceHawk @ Apr 10 2006, 04:07 AM) [snapback]363174[/snapback][/div][div class=\'quotemain\'][!--quotec--]
If you're using my while loop, there's a slight mistake I just noticed. If you replace
$n = $i + 1;
with;
$n = $i++ + 1;
Then you should be good to go.
[/quote]
Thank you.

Ok, That's sorted, I then have one final problem. When a new page starts (e.g. board(1).php?page=2) it re-starts the counter from 1, instead of what effectively should be 16. Check
[a href=\"http://www.rdkleague.com/marathon/board(1).php\" target=\"_blank\"]This Page[/a] to see what the hell I'm on about. Again, thanks for any help you can offer.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.