Jump to content


Photo

Number Increment Next To MySQL Querys


  • Please log in to reply
4 replies to this topic

#1 CMellor

CMellor
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts
  • LocationLeeds, UK

Posted 29 July 2006 - 06:30 PM

Hello, I have a question about PHP and I hope I can get some help.

I have a simple bit of code that lists the members on my forum.

<?php
include("include/dbconnect.php");

$query = mysql_query("SELECT * FROM ibf_members") or die(mysql_error());
while($row = mysql_fetch_array($query)) {
  echo $row['members_display_name'] .'<br />';
}
?>

I am building a ranking page on my site, where they will be listed in order of most points (I have a points hack) and I wanted to list each of them with a number next to it, but not to use their ID because I want to list it from 1-10.

<?php
for($count = 1; $count <= 10; $count++) {
  echo $count .'<br />';
}
?>

I know this bit of code creates a 1-10 list and I was trying to use it in the other code I provided to list each DB query as a number. I hope I make sense, I'm not great at talking about coding.

Thanks for your time.

Chris

#2 shoz

shoz
  • Staff Alumni
  • Advanced Member
  • 600 posts

Posted 29 July 2006 - 06:56 PM

<?php
for($count = 1; $row = mysql_fetch_array($query); $count++) {
  echo $count.':'.$row['members_display_name'].'<br />';
}
?>


#3 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 29 July 2006 - 07:21 PM

I would also put a
mysql_free_result($query);
at the end

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 29 July 2006 - 07:47 PM

It would certainly make sense to SELECT what you need ... for example, to get only the 10 highest point holders ordered in descending order:

$query = "SELECT member_name from your_table_name ORDER by points DESC LIMIT 10";

Legend has it that reading the manual never killed anyone.
My site

#5 CMellor

CMellor
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts
  • LocationLeeds, UK

Posted 29 July 2006 - 10:08 PM

Thank you very much, that is just what I wanted :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users