Help with ranking code up/down?

Discussion in 'PHP' started by 123GoToAndPlay, Jun 27, 2006.

  1. #1
    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??
     
    123GoToAndPlay, Jun 27, 2006 IP
  2. pl4y3r

    pl4y3r Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    i can't manage to understand why you execute the second sql query...

    maybe that's the problem :p
     
    pl4y3r, Jun 27, 2006 IP
  3. goldensea80

    goldensea80 Well-Known Member

    Messages:
    422
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    128
    #3
    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
     
    goldensea80, Jun 27, 2006 IP