Jump to content


Photo

adding to mysql table and incrementing by 1 (SOLVED)


  • Please log in to reply
2 replies to this topic

#1 switchdoc

switchdoc
  • Members
  • PipPip
  • Member
  • 22 posts
  • LocationColorado

Posted 04 September 2006 - 11:57 PM

Hello,

  I am hoping you can give me a hand with this. It's basic but I can't get it to work.

I have a mysql table with a bunch of steps in it:

instruction1 "do this"
instruction2 "do that"
instruction3 "do the other thing".

What I am trying to do is allow a user to add a step in the middle and then re-write the id numbers so they are still in order.

i.e. the user would add instruction2 "do something different" and i would end up with:

instruction1 "do this"
instruction2 "do something different"
instruction3 "do that"
instruction4 "do the other thing".

I've tried doing this in a while loop: (frameid is the number I am trying to increment)(I am limiting at the frame number the user wants to add at, and 2000 because I know there will never be that many steps)

$update = "select * from tutorialfiles2 where tutorial='$tutorial' limit $frameidupdate, 2000";
$result = mysql_query($update) OR DIE ("There was an error" .mysql-error());
while ($line = mysql_fetch_array($result)){
$frameid = $line["frameid"];
$stepname = $line["stepname"];
$frameid2 = $frameid++;
echo "<br><br>$frameid - $stepname <br>";


$change = "update tutorialfiles2 set frameid='$frameid2' where frameid='$frameid'";
$done = mysql_query($change) OR DIE ("There was an error" .mysql_error());

but what I get is, for example:

instruction1 "do this"
instruction2 "do something different"
instruction4 "do that"
instruction4 "do the other thing".

its not incrementing, its just adding the highest possible step.

Any thoughts appreciated. I'll be around if you need more info as I realize this is clear as mud.

Thanks,

Switchdoc

#2 switchdoc

switchdoc
  • Members
  • PipPip
  • Member
  • 22 posts
  • LocationColorado

Posted 05 September 2006 - 02:20 AM

Well, I know why the while loop doesnt work.

I am incrementing what I already incremented.  I am SOOO dumb.

i.e. I say set step5 to 6, set step 6 to 7, etc and since it runs one at a time the I just set 5 to 6 so not I am setting that same step to 7 so eventually they all equal the highest possible number.

So, can't do this in a while loop..

Any thoughts?

-Switch

#3 switchdoc

switchdoc
  • Members
  • PipPip
  • Member
  • 22 posts
  • LocationColorado

Posted 05 September 2006 - 04:17 AM

I got it.

Trick is to do it in reverse.

Get the total number of files, start at the end and do the writes backwards. That way you aren't incrementing what you just changed.

Thanks anyway,

-Switch




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users