image gallery re-order

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Da_Elf
Forum Commoner
Posts: 81
Joined: Mon Dec 29, 2008 12:31 pm

image gallery re-order

Post by Da_Elf »

OK this might seem really like pre-school php but i don't know the really complex ways to do stuff but im getting there bit by bit. This code does work. what i want to know is if there is a more efficient way to code this.
If the user is logged into the account I've got it so he can click on an image which brings it up in a popup div. inside there he can change the name of the image, change the order position and delete the image. with the deleting and re-ordering i needed to use a "while" for updating the database.
first off is i send the image ID number to the popup div. once its received the div is populated with the image thumbnail, its name and its current position. When change or delete is clicked this code is run

Code: Select all

$CurPos =$_POST['current'];
$NewPos =$_POST['new'];
$ImgID=$_POST['ID'];
//for delete
$queryA = "SELECT * FROM Test WHERE Position > $CurPos ORDER BY Position ";
$queryResourceA = mysql_query($queryA, $dbConn);

if(isset($_POST['delete'])){
$sqldelete = "Delete from Test Where testID = $ImgID";
mysql_query($sqldelete,$dbConn);
while ($row = mysql_fetch_array($queryResourceA , MYSQL_ASSOC)) {
$ToChng = $row['Position'];
$sqlposA="UPDATE Test 
         SET Position='$CurPos'
         WHERE Position = '$ToChng '
                         ";
mysql_query($sqlposA,$dbConn);
$CurPos++;
}} //closes the while and isset

//for update
if(isset($_POST['change'])){
if ($CurPos > $NewPos){
$Max = $CurPos;$Max--;
$queryB = "SELECT * FROM Test WHERE Position Between $NewPos AND $Max ORDER BY Position DESC ";
$queryResourceB = mysql_query($queryB, $dbConn);
else {
$Min = $CurPos;$Min++;
$queryB = "SELECT * FROM Test WHERE Position Between $Min AND $NewPos ORDER BY Position ";
$queryResourceB = mysql_query($queryB, $dbConn);
$sqlposB="UPDATE Test 
          SET Position='x'
          WHERE Position = '$CurPos '
          ";
mysql_query($sqlposB,$dbConn);
while ($rowB = mysql_fetch_array($queryResourceB, MYSQL_ASSOC)) {
$tomove = $rowB['Position'];
$moveto = $tomove;
if ($CurPos > $NewPos){$moveto++;}
else {$moveto--;}
$sqlposA="UPDATE Test 
          SET Position='$moveto'
          WHERE Position = '$tomove '
          ";
mysql_query($sqlposA,$dbConn);
}
$sqlposB="UPDATE Test 
          SET Position='$NewPos'
          WHERE Position = 'x '
          ";
mysql_query($sqlposB,$dbConn);
}
Post Reply