0

I have a user editable list like bullet points. Points are being deleted and added(to the end or middle of the list) frequently. Is there any more efficient way of maintaining this list's ORDER than having each bullet have an id and keeping a SQL entry of the order split by commas? Example:

$result = mysql_query("SELECT order FROM users WHERE user = '$userId'"); //Get order for logged in user. returns id1,id2,id3,id4
$row = mysql_fetch_array($result);
$order = explode(',', $row['order']); //make the order an array
$result = mysql_query("SELECT id, data FROM bullets"); //get all the bullets
$bullets = array();
while($row = mysql_fetch_array($result)){
    $project[$row['id']] = $row['data'];
}

Then run:

foreach($order as $k => $v){
    echo '<li id="'.$k.'">'.$v.'</li>';
}

1 Answer 1

2

You can add an ordinal field to the bullets table.

Then your select looks like this:

SELECT id, data FROM bullets ORDER BY ordinal

Also, you shouldn't store denormalized data like you're doing with order in your users table.

Also, you shouldn't name a field order because it is a reserved word.

4
  • So you're saying for each bullet add a value of it's position as an int? I was thinking of doing this but ran into a problem. If a user only accessed part of the data the order would be messed up. For example if the full order was id1,id2,id3,id4 a user accessed id2,id3 then added id5 to the end for him it would look like id2,id3,id5 but in the bigger scheme of things it should be id1,id2,id3,id5,id4 Apr 18, 2012 at 4:49
  • 1
    don't simply add to the number in you code. let the db handle this with a sequence or do updates where you add the MAX(ordinal)
    – David Chan
    Apr 18, 2012 at 4:54
  • Maybe I'm not understanding how to use a ordinal field..? Apr 18, 2012 at 5:07
  • it's just an integer. ordinal has no special meaning, it's just english for ordered and countable.
    – David Chan
    Apr 18, 2012 at 5:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.