Move row up or down and change its position in database also

I want to move images up or down and its listed according to the position in database and i want to change that position in database, and it can refresh the page so the updated position can be displayed.

database structure:

image_id (pk)
property_id (fk)
description
position
status (front image of that property)

could anyone help me out its urgent.

heres the code

(these functions are in function file)

function image_positionup($image_id, $property_id, $position)
{
$sql = “select * from property_images where property_id=$property_id and position=$position and image_id=$image_id”;
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
{
$rowz = mysql_fetch_array($result);
$posi = $rowz[‘position’];
if($posi>1)
{
$posi=$posi - 1;
$sqlu = “update property_images set position=’$posi’ where image_id=’$image_id’ and property_id=’$property_id’”;
mysql_query($sqlu);
$sql = “select * from property_images where property_id=$property_id and position=$posi and image_id<>$image_id”;
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
{
$rowz = mysql_fetch_array($result);
$posi = $rowz[‘position’];
$id = $rowz[‘image_id’];
if($posi<mysql_num_rows($result))
{
$posi=$posi + 1;
$sqlu = “update property_images set position=’$posi’ where image_id=’$id’ and property_id=’$property_id’”;
mysql_query($sqlu);
$re = 5;
}
//$msg = “The Picture has been moved upwards”;
return $re;
}
}
else
{
//$msg = “The picture cannot be moved up then this”;
return 0;
}
}
}

function image_positiondown($image_id, $property_id, $position, $total)
{
$sqldo = “select * from property_images where property_id=$property_id and position=$position and image_id=$image_id”;
$resultdo = mysql_query($sqldo);
if(mysql_num_rows($resultdo) > 0)
{
$rowz = mysql_fetch_array($resultdo);
$posi = $rowz[‘position’];
if($posi<$total)
{
$posi=$posi + 1;
$sqld = “update property_images set position=’$posi’ where image_id=’$image_id’ and property_id=’$property_id’”;
mysql_query($sqld);
$sql = “select * from property_images where property_id=$property_id and position=$posi and image_id<>$image_id”;
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
{
$rowz = mysql_fetch_array($result);
$posi = $rowz[‘position’];
$id = $rowz[‘image_id’];
if($posi>1)
{
$posi=$posi - 1;
$sqlu = “update property_images set position=’$posi’ where image_id=’$id’ and property_id=’$property_id’”;
mysql_query($sqlu);
$re = 5;
}
//$msg = “The Picture has been moved downwards”;
return $re;
}
}
else
{
//$msg = “The picture cannot be moved down then this”;
return 0;
}
}
}

this is in the main file

<?php if($row['position']>1) { ?>
      <img src="images/up.png" width="30px" height="30px" onclick="<?php $retu = image_positionup($image_id, $property_id, $row['position']); ?>" />
      <?php
      /*if($retu==5)
      {
      echo "<meta http-equiv='refresh' CONTENT='0;URL=property_images.php?property_id=".md5($property_id)."'>";
      */echo "<br />";
      //}
      }
      echo "<br />";
      if($row['position']<$totalrows)
      {
      ?>
      <img src="images/down.png" width="30px" height="30px" onclick="<?php $retd = image_positiondown($image_id, $property_id, $row['position'], $totalrows); ?>" />
      <?php
      /*if($retd==5)
      {
      echo "<meta http-equiv='refresh' CONTENT='0;URL=property_images.php?property_id=".md5($property_id)."'>";
      }*/
      }
      echo "</td></tr>";
    }
?>

You cannot necessarily change it’s actual position in the database. The database’s job is to store the data in the most efficient manner, that does not always mean that A comes before B which comes before C.

If you want to “SORT” the data (prior to outputting, then add a SORT BY clause in your query.

i.e.

[b]SELECT * FROM property_images WHERE property_id=$property_id AND position=$position AND image_id=$image_id ORDER BY image_id[/b]
Sponsor our Newsletter | Privacy Policy | Terms of Service