Jump to content

User Post = Rank


Nik Top

Recommended Posts

I am having trouble on user post = rank

 

This is the rank table:

-----------------

post  | rank

-----------------

0        | Noob

10      | Member

50      | Metal

100    | Silver

200    | Gold

 

 

$user_post="120";

 

$query_rank=mysql_query("SELECT * FROM rank WHERE post<='$user_post' OR post>='$user_post'") or die ("Error");

while ($row=mysql_fetch_assoc($query_rank)){

$rank=$row['rank'];

}

 

echo "User Rank: $rank";

 

The above code output:

User Rank: Gold

 

The right output should be:

User Rank: Silver

 

 

Please help

It will be better if someone willing to share a better ranking code.

Link to comment
Share on other sites

$user_post="120";

then you will get only one answer.

$user_post="100";
	$sql = "SELECT * FROM rank WHERE  post>='$user_post'";
	$res = mysql_query($sql);
	if(!$res){ 	die(" Could not query the database ORDERS - functions.php -32 : <br/>". mysql_error() ); }
	$num =  mysql_num_rows($res);

	while($row = mysql_fetch_assoc($res)){

	echo $row['rank']."<br/>";
	}

 

 

Link to comment
Share on other sites

Thank you for your reply

but thats not the answer I want

 

this is what I want

if user_post = 120 then rank = Silver

 

but some how I am having trouble making it to the right rank.

 

anyway thank you for the help. please if anyone got a better solution for this?

Link to comment
Share on other sites

it would be easier if you define start and end for each rank, so the database would look like this:

 

start  | end | rank

-----------------

0      |  9            | Noob

10    |  49          | Member

50    |  99          | Metal

100  |  199        | Silver

200    |  9999999 | Gold

 

then all you need is:

 

$user_post="120";
$query_rank = mysql_query("select `rank` from `rank` where `start` <= '$user_post' and `end` >= '$user_post' limit 1") or die ("Error");
$row=mysql_fetch_assoc($query_rank);
echo $row['rank'];

 

the only problem is you'll need to define an ending number for Gold users, but you can use an absurdly large number that no one will ever reach.

 

p.s. having a field named `rank` in a table also named `rank` can cause confusion. Since the table holds all ranks, why not call it `ranks` (plural) ?

Link to comment
Share on other sites

thank you for the reply

 

I dont know how to use the WebStyles method.

 

for the mysql table i try the order by in many different ways but seems not working at all.

 

I see that on this site there is a user post = rank system. thats what I wanted

 

I don't think just using mysql table will solve this.

 

Is there a different solution such as using a function or something else?

 

Link to comment
Share on other sites

<?php

function userRank($userRank) {
$rank = array(0=>'Noob',10=>'Member',50=>'Metal',100=>'Silver',200=>'Gold');
foreach($rank as $k => $v) {
   if($userRank > $k) {
     $ranking = $v;
   }
   else {
     break;
   }
}
return $ranking;
}

 

Although mysql will work.

Link to comment
Share on other sites

I set up a test database, with your values entered in, and ran it, coming up with the correct values.

 

db table

--
-- Table structure for table `rank`
--

CREATE TABLE IF NOT EXISTS `rank` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `post` int(11) NOT NULL,
  `rank` varchar(20) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

--
-- Dumping data for table `rank`
--

INSERT INTO `rank` (`id`, `post`, `rank`) VALUES
(1, 0, 'Noob'),
(2, 10, 'Member'),
(3, 50, 'Metal'),
(4, 100, 'Silver'),
(5, 200, 'Gold');

 

Query

SELECT rank FROM `rank` WHERE post <= 55 ORDER BY post DESC LIMIT 1

Link to comment
Share on other sites

Thank you Thank you

both of them work but function not working right

if i put 50 post for function = member, if i put 100 = metal, if i put 101=silver

Not a big issue i know where it came from so i can fix it.

 

anyway the mysql works great!!!

 

I found out why i making it wrong because I did not put the " DESC " in the query.

 

Here is my Table: ( I change table name to rank_sys because "WebStyles" advice

 

CREATE TABLE rank_sys (

id int NOT NULL auto_increment,

post varchar( 30 ) NOT NULL,

rank varchar( 50 ) NOT NULL,

PRIMARY KEY  (id)

);

 

INSERT INTO rank_sys  (post, rank) VALUES ('0','Noob');

INSERT INTO rank_sys  (post, rank) VALUES ('10','Member');

INSERT INTO rank_sys  (post, rank) VALUES ('50','Metal');

INSERT INTO rank_sys  (post, rank) VALUES ('100','Silver');

INSERT INTO rank_sys  (post, rank) VALUES ('200','Gold');

 

 

---------------------------------- rank.php

 

$user_post="120";

echo "User = $user_post Posts<br><br>";

 

// ---- function error not fix yet

function userRank($userRank) {

$rank=array(0=>'Noob',10=>'Member',50=>'Metal',100=>'Silver',200=>'Gold');

 

foreach($rank as $k=>$v) {

if ($userRank > $k) {

$ranking=$v;

}else {

break;

}

}

return $ranking;

}

 

// ------- function rank

echo "User Rank: ".userRank($user_post);

 

echo "<br><br>";

 

$query_rank=mysql_query("SELECT post,rank FROM rank_sys WHERE post<='$user_post' ORDER BY post DESC LIMIT 1") or die ("Error");

while ($row=mysql_fetch_array($query_rank)){

$rank=$row['rank'];

}

 

// -------- MySQL rank

echo "User Rank: $rank";

 

 

Great Great Thanks

 

Link to comment
Share on other sites

Oh wait there is new issue coming up.

 

i change my table post to

 

0=Noob

50=Member

150 = Metal

1200 = Silver

70000 = Gold

 

function is doing good because I add " = " into the code " if ($userRank >= $k) { "

 

mysql is being stupid if I put 1600 = Metal but 1260 = silver but still a big issue

 

now I want to focus on function but i tried and having difficulty puting mysql in it. please help

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.