Jump to content

Archived

This topic is now archived and is closed to further replies.

switchdoc

adding to mysql table and incrementing by 1 (SOLVED)

Recommended Posts

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)

[code]
$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());
[/code]

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.