hi all, I am having problems with my ranking code, it doesn't work proper This is for changing ranks just one up or down [<a href="?act=up&faqRank=<?php echo $row_rsFAQ['faqRank']; ?>&id=<?php echo $row_rsFAQ['faqID']; ?>">up</a>] of [<a href="?act=down&faqRank=<?php echo $row_rsFAQ['faqRank']; ?>&id=<?php echo $row_rsFAQ['faqID']; ?>">down</a>] Code (markup): Then I call the function if(($act == "up") or ($act =="down")){ $res = moveRank($id, $act, $faqRank); } } Code (markup): The moveRank function is function moveRank($id, $act, $faqRank) { global $database_connDB,$connDB; mysql_select_db($database_connDB, $connDB); $num = ($act == "up")? 1 : (-1); // decides whether we should add or remove 1 from faqRank $result1 = mysql_query("UPDATE tblFAQ SET faqRank = $faqRank + $num WHERE faqID = '$id'", $connDB); // moves the record to the wanted direction $num2 = $faqRank + ((-1)*$num); $num3 = $faqRank + $num ; $sql = "UPDATE tblFAQ SET faqRank = '$num2' WHERE faqID != '$id' AND faqRank='$num3'";// Now there are two records with the same faqRank, so we have to move the other record to the opposite direction $result2 = mysql_query($sql, $connDB); if($result2) { $check = 0; } else { $check =1; } return $check; } Code (markup): It sometimes works and other times not Any suggestions??
I think you get $faqRank from database? If you just want to update the rank, instead of Use But maybe the difficult part is where you swap the faqrank, for exampe: you have: 1,2,3...10 and wnat 3 to "up" -> 3->4 and 4->3, SO maybe this code is you quite right: (because num2=2 (should be 3)) And you may need to check the minimum (assume = 0) and maximm faqRank also. Good luck