Jump to content


Photo

php row? Problem?


  • Please log in to reply
4 replies to this topic

#1 RDKL PerFecT

RDKL PerFecT
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 08 April 2006 - 09:29 PM

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.

$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>");
    }


#2 IceHawk

IceHawk
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 09 April 2006 - 02:50 AM

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;
$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>");
}

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.
[a href="http://www.devtext.com" target="_blank"]DevText a community for all languages[/a]

#3 RDKL PerFecT

RDKL PerFecT
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 09 April 2006 - 10:39 AM

[!--quoteo(post=362914:date=Apr 9 2006, 03:50 AM:name=IceHawk)--][div class=\'quotetop\']QUOTE(IceHawk @ Apr 9 2006, 03:50 AM) View Post[/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;
$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>");
}

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?

$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);


#4 IceHawk

IceHawk
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 10 April 2006 - 03:07 AM

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.
[a href="http://www.devtext.com" target="_blank"]DevText a community for all languages[/a]

#5 RDKL PerFecT

RDKL PerFecT
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 10 April 2006 - 10:59 AM

[!--quoteo(post=363174:date=Apr 10 2006, 04:07 AM:name=IceHawk)--][div class=\'quotetop\']QUOTE(IceHawk @ Apr 10 2006, 04:07 AM) View Post[/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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users