Jump to content

Increment cell values after THIS cell


SlickAU

Recommended Posts

Hey all,

 

I am trying to create a query which will increment ALL values in the 'frame' column of my table AFTER the current frame number, the current frame number is passed through via a form; here is an example:

 

BEFORE TABLE:

 

====================

id == frame ==

0 ==    0    == Already has content in this frame

1 ==    1    == Already has content in this frame

2 ==    2  == Already has content in this frame

3 ==    3  == Already has content in this frame

====================

 

Say i wanted to insert a new blank frame AFTER frame 1...the output should look like this:

 

$frameno = 1 ;

 

====================

id == frame ==

0 ==    0    == Content unchanged

1 ==    1    == Content unchanged

2 ==    2  == (no content in this frame...because this is a new field)

3 ==    3  == This contains the original content from frame 2

4 ==    4  == This contains the original content from frame 3

 

====================

 

So basically all i am trying to acheive is to increment all the frame fields after the chosen frame number and add a blank field after this chosen one.

 

Anyone have any ideas?

 

Thanks,

Slick ;D

 

 

Link to comment
https://forums.phpfreaks.com/topic/64760-increment-cell-values-after-this-cell/
Share on other sites

Assuming all the frame numbers are unique, this should work:

 

$qry = mysql_query("SELECT * FROM table WHERE frame > 1");
while ($row = mysql_fetch_array($qry))
{
mysql_query("UPDATE table SET frame = " . ($row["frame"]+1) . " WHERE frame = " . $row["frame"])
}

Hey lemm,

 

I tried your suggestion, but to no avail...all that comes up now is a blank page when the script is run  >:(... Here is what i am using at the moment:

 

$frameno = $_GET['frameno'];

$refname = $_GET['refname']; /* Refname is the name of the course in which the frame is being edited.... e.g. - workplace inductiion */

 

//////// MYSQL CONNECTION INFO + SELECT DATABASE HERE ///////

 

$qry = mysql_query("SELECT * FROM $refname WHERE frame > $frameno");

while ($row = mysql_fetch_array($qry))

{

mysql_query("UPDATE $refname SET frame = " . ($row["frameno"]+1) . " WHERE frame = " . $row["frameno"])

echo "Success";

}

 

Any more suggestions?

Hey lemm,

 

I tried your suggestion, but to no avail...all that comes up now is a blank page when the script is run  >:(... Here is what i am using at the moment:

 

$frameno = $_GET['frameno'];

$refname = $_GET['refname']; /* Refname is the name of the course in which the frame is being edited.... e.g. - workplace inductiion */

 

//////// MYSQL CONNECTION INFO + SELECT DATABASE HERE ///////

 

$qry = mysql_query("SELECT * FROM $refname WHERE frame > $frameno");

while ($row = mysql_fetch_array($qry))

{

mysql_query("UPDATE $refname SET frame = " . ($row["frameno"]+1) . " WHERE frame = " . $row["frameno"])

echo "Success";

}

 

Any more suggestions?

Did you remeber mysql_connect() and mysql_select_db()?

Archived

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

×
×
  • Create New...

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.