Nik Top Posted September 1, 2011 Share Posted September 1, 2011 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. Quote Link to comment Share on other sites More sharing options...
voip03 Posted September 1, 2011 Share Posted September 1, 2011 $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/>"; } Quote Link to comment Share on other sites More sharing options...
Nik Top Posted September 1, 2011 Author Share Posted September 1, 2011 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? Quote Link to comment Share on other sites More sharing options...
WebStyles Posted September 1, 2011 Share Posted September 1, 2011 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) ? Quote Link to comment Share on other sites More sharing options...
Nik Top Posted September 1, 2011 Author Share Posted September 1, 2011 Thank you for you reply, and the advice but it seems not working. it gave me this: user 120 = rank: Noob I guess I am going the wrong direction, Is there a function for this? and how to start? Please help Quote Link to comment Share on other sites More sharing options...
voip03 Posted September 1, 2011 Share Posted September 1, 2011 120 is bigger/greater than 100 , the result will be gold. unless if you use WebStyles method Quote Link to comment Share on other sites More sharing options...
jcbones Posted September 1, 2011 Share Posted September 1, 2011 Using the OP table: $sql = "SELECT rank FROM rank WHERE post<='$user_post' ORDER BY post DESC LIMIT 1"; $query_rank=mysql_query($sql) or die ("Error"); Quote Link to comment Share on other sites More sharing options...
Nik Top Posted September 1, 2011 Author Share Posted September 1, 2011 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? Quote Link to comment Share on other sites More sharing options...
jcbones Posted September 1, 2011 Share Posted September 1, 2011 <?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. Quote Link to comment Share on other sites More sharing options...
jcbones Posted September 1, 2011 Share Posted September 1, 2011 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 Quote Link to comment Share on other sites More sharing options...
Nik Top Posted September 1, 2011 Author Share Posted September 1, 2011 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 Quote Link to comment Share on other sites More sharing options...
Nik Top Posted September 1, 2011 Author Share Posted September 1, 2011 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 Quote Link to comment Share on other sites More sharing options...
jcbones Posted September 1, 2011 Share Posted September 1, 2011 Because you are storing post as a varchar, and it should be stored as an integer. If you look at the table I showed you, you would see that it is set as an integer. Quote Link to comment Share on other sites More sharing options...
Nik Top Posted September 1, 2011 Author Share Posted September 1, 2011 I see thank you thank you. problem SOLVED !!! :D :D Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.