Jump to content

php row? Problem?


RDKL PerFecT

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]
Link to comment
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.
Link to comment
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]
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.