1

首先,对于发布如此庞大的代码块,我深表歉意。它甚至可能与问题无关,但以防万一......代码维护一个简单的待办事项列表,我想将其合并到现有的 PHP 网站中,该网站为每个用户存储大量信息。换句话说,我想将此添加到 mySQL DB 中用户的信息行中。

我是 PHP 新手,但通过提出想法并弄清楚如何使它们发挥作用已经走了很长一段路。您能否指出添加这样一个功能的方向,该功能通过添加和删除信息行来存储信息到分配给用户的字段列表中?

另一种说法:我想给我的用户一种维护他们自己的 ToDo 列表的方法。

<?php 
$conn = mysql_connect('server, 'db', 'password') or die(mysql_error());
$db = mysql_select_db('db',$conn) or die(mysql_error());
// if an arrow link was clicked...
if ($_GET['dir'] && $_GET['id']) {
   // make GET vars easier to handle
   $dir = $_GET['dir'];
   // cast as int and couple with switch for sql injection prevention for $id
   $id = (int) $_GET['id'];
   // decide what row we're swapping based on $dir
   switch ($dir) {
      // if we're going up, swap is 1 less than id
      case 'up': 
         // make sure that there's a row above to swap
         $swap = ($id > 1)? $id-- : 1;
         break;
      // if we're going down, swap is 1 more than id
      case 'down':
         // find out what the highest row is
         $sql = "SELECT count(*) FROM info";
         $result = mysql_query($sql, $conn) or die(mysql_error());
         $r = mysql_fetch_row($result);
         $max = $r[0];
         // make sure that there's a row below to swap with
         $swap = ($id < $max)? $id++ : $max;
         break;
      // default value (sql injection prevention for $dir)
      default:
         $swap = $id;
   } // end switch $dir
   // swap the rows. Basic idea is to make $id=$swap and $swap=$id 
   $sql = "UPDATE info SET usort = CASE usort WHEN $id THEN $swap WHEN $swap THEN $id END WHERE usort IN ($id, $swap)";
   $result = mysql_query($sql, $conn) or die(mysql_error());
} // end if GET  
// set a result order with a default (sql infection prevention for $sortby)
$sortby = ($_GET['sortby'] == 'name')? $_GET['sortby'] : 'usort';
// pull the info from the table
$sql = "SELECT usort, name FROM info ORDER BY $sortby";
$result = mysql_query($sql, $conn) or die(mysql_error());
// display table
echo "<table border = '1'>";
echo "<tr>";
// make column names links, passing sortby
echo "<td><a href='{$_SERVER['PHP_SELF']}?sortby=usort'>usort</a></td>";
echo "<td><a href='{$_SERVER['PHP_SELF']}?sortby=name'>name</a></td>";
echo "</tr>";
// delete from table
if ($_GET['del'] == 'true') {
   // cast id as int for security
   $id = (int) $_GET['id'];
   // delete row from table
   $sql = "DELETE FROM info WHERE usort = '$id'";
   $result = mysql_query($sql, $conn) or die(mysql_error());
   // select the info, ordering by usort
   $sql = "SELECT usort, name FROM info ORDER BY usort";
   $result = mysql_query($sql, $conn) or die(mysql_error());
   // initialize a counter for rewriting usort
   $usort = 1;
   // while there is info to be fetched...
   while ($r = mysql_fetch_assoc($result)) {
      $name = $r['name'];
      // update the usort number to the one in the next number
      $sql = "UPDATE info SET usort = '$usort' WHERE name = '$name'";
      $update = mysql_query($sql, $conn) or die(mysql_error());
      // inc to next avail number
      $usort++;
   } // end while
} // end if del
// display data 1 row at a time
while ($r = mysql_fetch_assoc($result)) {
   echo "<tr>";
   // make the links to change custom order, passing direction and the custom sort id
   echo "<td align = 'center'><a href='{$_SERVER['PHP_SELF']}?dir=up&id={$r['usort']}'>/\</a> ";
   echo "<a href='{$_SERVER['PHP_SELF']}?dir=down&id={$r['usort']}'>\/</a></td>";
   echo "<td>{$r['name']}</td>";
   echo    "<td><a href='{$_SERVER['PHP_SELF']}?del=true&id={$r['usort']}'>delete</a></td>";
   echo "</tr>";
} // end while $r
echo "</table>";
// end display table
?>
4

1 回答 1

1

用户待办事项列表对我来说似乎是另一个表。您不需要更改用户信息中的任何值。只需在另一个表中添加、删除或更改任务顺序。类似于下图的东西

在此处输入图像描述

于 2013-04-30T19:27:14.073 回答