Page 1 of 1
slider data input
Posted: Thu Feb 27, 2003 11:45 am
by kindley
I am looking for a way to let users reorganize items in a list. I have in mind something like a slider that lets me move an item up or down on the list and then saves this information in the database. For instance, if I had a list like:
Apples
Oranges
Bananas
And I wanted to move Bananas between Apples and Oranges.
Is there something like a slider or some kind of "move up" or "move down" class or technique for doing this that would be simple and robust?
Posted: Thu Feb 27, 2003 7:21 pm
by McGruff
This is a copy of a function I use to change the order of forums and boards. You'd need to adapt it to your own needs but it should give you an idea.
You'll need a weight column in your database table and ORDER BY weight when you output to the browser. When you add new rows, do something like this to make sure they have a unique weight (you can't make the weight column unique with this function, as you'll see):
Code: Select all
$mysql = "SELECT MAX(weight) AS weight FROM forums";
$query = mysql_query($mysql) or die("Cannot query the database.<br>" . mysql_error());
$result = mysql_fetch_array($query);
$weight = $result['weight'];
$weight++;
VARS:
$this - f or b depending on whether it's a board or forum, ie lets you use the function on more than one table. You can strip this stuff out if you just want to change weights in a single table.
$id - this will be the id number for the rows in your table. Add an auto-increment integere column if you don't have one.
$case - u for move up, d for move down
Code: Select all
function changeWeight($this, $id, $case) {
IF ($this == f) {
$col = 'fid';
$table = $from = 'forums';
} ELSE {
$col = 'bid';
$fid = $this;
$table = 'forum_boards';
$from = $table . " WHERE fid='" . $fid . "'"; // add a WHERE clause - see notes above
}
IF ($case == u) { // move item up
$x = -1;
} ELSEIF ($case == d) { // move item down
$x = 1;
}
$mysql = "SELECT $col, weight FROM $from ORDER BY weight ASC";
echo "select query: " . $mysql . "<br>";
$query = mysql_query($mysql) or die("Cannot query the database.<br>" . mysql_error());
$rows = mysql_num_rows($query);
// While loop: loops through rows looking for the one to edit then..
// .. steps back ($x = -1) or forward ($x = 1) to get..
// .. info about previous or following row
// !isset($id_2) exits the loop once we've found the row
// .. we're looking for and set $id_2 & $weight_2
// $x < $rows always true for move up case;
// .. but stops us trying to move the last row down in the
// .. move down case (it's already the lowest))
// First IF step:
// $rows > 1 - don't proceed if there is only one board
// Second IF step:
// $result[$col] == $id - the row we want to move
// $x <> -1 - don't try to move first board up (it's already the highest: ..
// .. we'd get a dataseek offset error)
IF ($rows > 1) {
while ($result = mysql_fetch_array($query) AND !isset($id_2) AND $x < $rows ) {
echo "result.col/xid = " . $result[$col] . " id= " . $id . "<br>";
IF ($result[$col] == $id AND $x <> -1) { // $bid/$fid was passed to fn as the argument $id
echo "have match where id=" . $id . "<br>";
$weight_1 = $result['weight'];
echo $x . "<br>";
mysql_data_seek($query, $x);
$result2 = mysql_fetch_array($query); //note we define a second result array from the
$id_2 = $result2[$col]; //...same query
$weight_2 = $result2['weight'];
$mysql = "UPDATE $table SET weight='$weight_2' WHERE $col='$id'";
$query2 = mysql_query($mysql) or die("Cannot query the database.<br>" . mysql_error());
echo "1st update query: " . $mysql . "<br>";
$mysql = "UPDATE $table SET weight='$weight_1' WHERE $col='$id_2'";
echo "2nd update query: " . $mysql . "<br>";
$query2 = mysql_query($mysql) or die("Cannot query the database.<br>" . mysql_error());
}
$x++;
}
}
main(1); // ie whatever function you want to call for the next page
}
PS: I just finished testing this out: the echo's are in there for debugging only - can remove
slider data input
Posted: Thu Feb 27, 2003 7:45 pm
by kindley
Thanks! I'll look it over!