LyleCrumbstorm
LyleCrumbstorm

Reputation: 171

Add Table or Array into Row Per User

First, I apologize for posting such an enormous block of code. It's probably not even pertinent to the question but just in case... The code maintains a simple ToDo list that I would like to incorporate into an existing PHP Website that stores a lot of information for each user. In other words, I would like to add this to the user's row of information in the mySQL DB.

I am new to PHP but have come a long way by coming up with ideas and figuring out how to make them work. Can you point me in the direction of adding a feature like this, that stores information by way of adding & deleting rows of information, into the list of fields assigned to a user?

Another way of putting it: I would like to give my users a way to maintain their own ToDo list.

<?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
?>

Upvotes: 1

Views: 136

Answers (1)

Jose Areas
Jose Areas

Reputation: 719

Users todo list seems another table to me. You do not need to change any value from users info. Just add, delete or change order of tasks in another table. Something like the image below

enter image description here

Upvotes: 1

Related Questions