Pseudocode:
default display:
SELECT * FROM table ORDER BY order
id|order
1|10
2|20
3|30
4|40
move up #3:
UPDATE table SET order = order - 15 WHERE id = 3
30 - 15 = 15
reorder:
SELECT * FROM table ORDER BY order
1|10
3|15
2|20
4|40
while { UPDATE table SET order = i WHERE id = row_id; i+=10 }
new display:
1|10
3|20
2|30
4|40
move down #2:
UPDATE table SET order = order + 15 WHERE id = 2
30 + 15 = 45
reorder:
SELECT * FROM table ORDER BY order
1|10
3|20
4|40
2|45
while { UPDATE table SET order = i WHERE id = row_id; i+=10 }
new display:
1|10
3|20
4|30
2|40
Har prøvet ovenstående og det virker til dels også fint nok.
Min kode:
test.php
- <?php require_once('Connections/cms.php'); ?>
- <?php
- if (!function_exists("GetSQLValueString")) {
- function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
- {
- $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
-
- $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
-
- switch ($theType) {
- case "text":
- $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
- break;
- case "long":
- case "int":
- $theValue = ($theValue != "") ? intval($theValue) : "NULL";
- break;
- case "double":
- $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
- break;
- case "date":
- $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
- break;
- case "defined":
- $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
- break;
- }
- return $theValue;
- }
- }
-
- mysql_select_db($database_cms, $cms);
- $query_rsMenu = "SELECT * FROM menu ORDER BY sorting ASC";
- $rsMenu = mysql_query($query_rsMenu, $cms) or die(mysql_error());
- $row_rsMenu = mysql_fetch_assoc($rsMenu);
- $totalRows_rsMenu = mysql_num_rows($rsMenu);
-
-
- ?><html>
- <head>
- <title>SimpleWeb CMS - Administration</title>
- <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
- <link rel="StyleSheet" href="style.css" type="text/css">
- </head>
- <body>
- <?php do { ?>
-
- <?php echo $row_rsMenu['name']; ?>
- <?php echo $row_rsMenu['sorting']; ?>
- <a href="op.php?id=<?php echo $row_rsMenu['id']; ?>&sort=<?php echo $row_rsMenu['sorting']; ?>">Op</a>
- <a href="ned.php?id=<?php echo $row_rsMenu['id']; ?>&sort=<?php echo $row_rsMenu['sorting']; ?>">Ned</a><br>
-
- <?php } while ($row_rsMenu = mysql_fetch_assoc($rsMenu)); ?></body>
- <?php
- mysql_free_result($rsMenu);
- ?>
op.php
- <?php require_once('Connections/cms.php'); ?>
- <?php
-
- $id = $_GET['id'];
- $sorting = $_GET['sort'];
-
- $result = mysql_query("UPDATE menu SET sorting=sorting-15 WHERE id=$id")
- or die(mysql_error());
-
- header('Location:test.php');
-
- ?>
ned.php
- <?php require_once('Connections/cms.php'); ?>
- <?php
-
- $id = $_GET['id'];
- $sorting = $_GET['sort'];
-
- $result = mysql_query("UPDATE menu SET sorting=sorting+15 WHERE id=$id")
- or die(mysql_error());
-
- header('Location:test.php');
-
- ?>
Udskriver:
one 10
Op Nedtwo 20
Op Nedtree 30
Op Nedfour 40
Op NedEfter jeg flytter 3 op:
one 10
Op Nedtree 15
Op Nedtwo 20
Op Nedfour 40
Op NedMen dette lille while stykke irritere mig.:
while { UPDATE table SET order = i WHERE id = row_id; i+=10 }
Hvordan skrives det om til php kode??
Resultatet efter jeg har flyttet 3 op, skulle gerne se sådan ud:
one 10
Op Nedtree 20
Op Nedtwo 30
Op Nedfour 40
Op Ned
Indlæg senest redigeret d. 08.06.2008 15:34 af Bruger #8348