Jump to content

Strange Loop Result


Sops21

Recommended Posts

I am writing a php script the stores contact info in a database and then ranks the by the rank that i give them. I am having a problem when it comes to changing their ranks. Let me give you an example:
Joe Smith has a rank of 1
John Doing has a rank of 2
Beth Franks has a Rank of 3
Jim Johnson has a Rank of 4

Now what should happen is that if i want Jim to have a rank of 2 it should bump down everyone elses score. Instead when i run it, it gives Jim a score of 2 and everyone else a score of 4. For the life of me i can't figure out why.


[code]
<?
$ud_id=$_POST['ud_id'];
$ud_name=$_POST['ud_name'];
$ud_address=$_POST['ud_address'];
$ud_city=$_POST['ud_city'];
$ud_state=$_POST['ud_state'];
$ud_phone=$_POST['ud_phone'];
$ud_rank=$_POST['ud_rank'];
$rank=$ud_rank; // copies the ud_rank to rank, rank  is used in the while loop

$username="********";
    
$password="*********";
    
$database="*********";
    
mysql_connect(localhost,$username,$password);
    @mysql_select_db($database) or die("Unable to Connect to database");
    $query="SELECT * FROM spots ORDER BY rank";
    $result=mysql_query($query);
    $num=mysql_numrows($result); //gets the number of rows in the table
    
    $newrank=$rank; //copies the current records rank to newrank
    $runs= ($num - $rank); //tells how many times the while loop needs to run
    
    while ($runs > 0){
     echo "Rank = $rank <br>";
    
    $newrank=($newrank + 1); //adds one to the new rank
    echo "New Rank =$newrank <br><br>";
    
    $query="UPDATE spots SET rank='$newrank' WHERE rank='$rank'"; //stores the new values
    @mysql_select_db($database) or die ("Unable to connect to Database");
    mysql_query($query);
    $rank=($rank + 1); //addes 1 to the newrank
    
    $runs=($runs -1); //subtracts one to limit the rumbers of runs...
    
    }

// tell mysql to update the changes
$query="UPDATE spots SET name='$ud_name', address='$ud_address', city='$ud_city', state='$ud_state', phone='$ud_phone', rank='$ud_rank' WHERE id='$ud_id'";
//do not forget this it is needed to connect to the data base
@mysql_select_db($database) or die ("Unable to conect to Database");
mysql_query($query);
echo "Record Updated";

mysql_close();
?>
[/code]


Any thoughts? For life of me i can't figure it out... if i need to i can post the previous page where the updated information is given....
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.