I am pretty new to PHP so please be kind :).
I have been working on a system for a client to add products to a page, delete and edit thos products.
Now I am working on the positioning so I created a mysql entry “position”.
My table has an auto-incremated id, title, description and position.
I have managed to set the positioning to the latest upload, for example if I have 3 entrys, the latest upload will get the position 4. Here is the upload script:
//UPLOAD SCRIPT
if (isset($_POST['upload'])) {
$dirupload = "../images/";
$dirupload = $dirupload . basename( $_FILES['image-upload']['name']);
$titleupload = $_POST['title-upload'];
$descriptionupload = $_POST['desc-upload'];
$imageupload = ($_FILES['image-upload']['name']);
$uploadedfileupload = $_FILES['image-upload']['tmp_name'];
$uploadedfiletypeupload = $_FILES['image-upload']['type'];
$query="SELECT * FROM promotions ORDER BY position";
$result=mysql_query($query);
$num=mysql_num_rows($result);
$add=$num+1;
if (!($uploadedfiletypeupload =="image/pjpeg" OR $uploadedfiletypeupload =="image/jpeg" OR $uploadedfiletypeupload =="image/jpg")){
echo "L'image doit être en .jpg ou .jpeg";
}else if (move_uploaded_file($_FILES['image-upload']['tmp_name'], $dirupload)){
$sql="INSERT INTO promotions (title, description, position) VALUES ('$titleupload','$descriptionupload','$add')";
if (!mysql_query($sql,$con)){
die('Error: ' . mysql_error());
}
$newnameupload = "../images/" . mysql_insert_id() . ".jpg";
rename ("../images/$imageupload","$newnameupload");
$orig_imageupload = imagecreatefromjpeg("../image/$newnameupload");
$sm_imageupload = imagecreatetruecolor(96,96);
imagecopyresampled($sm_imageupload,$orig_imageupload,0,0,0,0,96,96,imagesx($orig_imageupload),imagesy($orig_imageupload));
imagejpeg($sm_imageupload, $newnameupload, 100);
}else{
echo "Problem";
}
}
Now my problem is with the “up” and “down” entry to change the positions, I manage to add or substract 1 to the current position, the problem is I need to do the opposite to the next or prev one, here is the “up” script:
if (isset ($_POST['up'])){
$id=$_POST['id'];
$position=$_POST['position'];
$newPos=$position-1;
mysql_query("UPDATE promotions SET position='$newPos' WHERE id='$id'");
}
So I need to find the previous row and add 1, for example: If i have the second entry i want to put to first, click the “up” button, it gets moved up (that works) but i need to find the old first position and move it down once (that’s the problem).
Hopefully I was clear enough, and thanks in advance for any help !
Think this should work placed before the update to your current product