Jump to content

Strange DB updates


hellonoko

Recommended Posts

My below code updates a list of items when a UP or DOWN position arrow is clicked on an item.

If you click the second item in the list. It becomes the first and the first becomes the second and so on.

 

My data base currently contains 5 items ranked 1 2 3 4 5 and when I use the script on the top two it works fine. The switch places.

 

However when I choose the 4th item. My ranking in my db for the items is changed to 2 2 3 3 5 when it should ALWAYS stay 1 2 3 4 5 and just certain items should change.

 

Code is below. Maybe there is a better way to go about writing a ranking system? Any suggestions.

 

Thanks for your time

 

<?php

include 'dbconnect.php';

// ranking sytem for IDEAS

$id = (int)$HTTP_GET_VARS[id];

$movement = $HTTP_GET_VARS[direction];
// get rank of current item

/*$query = "SELECT rank FROM ideas WHERE id = $id";
$result = mysql_query($query);
$row = mysql_fetch_row($result);*/

$query = "SELECT rank FROM ideas WHERE id = '$id'";
$result = mysql_query($query) or die (mysql_error());
$row = mysql_fetch_object($result) or die (mysql_error());

$id_rank = $row->rank;

//echo $id_rank;
//exit();



/// move up or down

if ( $movement = "up" )
{
	// get ID of idea below one to be moved.
	$query = "SELECT id FROM ideas WHERE rank < $id_rank LIMIT 1" or die (mysql_error());;
	$result = mysql_query($query) or die (mysql_error());
	$row = mysql_fetch_assoc($result) or die (mysql_error());

	$moving_idea_id = $row['id'];

	// moves item rank DOWN one.
	//echo $moving_idea_id;
	//exit();

	$query = "UPDATE ideas SET rank=rank-1 WHERE id = $id" or die (mysql_error());
	mysql_query($query);

	// moves previous item rank UP one...
	$query = "UPDATE ideas SET rank=rank+1 WHERE id=$moving_idea_id" or die (mysql_error());
	mysql_query($query);

	echo "<meta http-equiv='REFRESH' content=0;url=display_ideas.php>";

}

if ( $movement = "down")
{

}

?>


 

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.