我已经为我的网站开发了一个排名系统,其中为每个项目分配排名以沿着选择(向上和向下)移动它。它工作正常。
但问题是如果删除了一条记录,则旁边的记录将不会再次排名。 但其他人工作正常。
实施例
id | cat name | Rank
1 | Jackets | 1
2 | T-shirts | 2
3 | Gloves | 3*
4 | Scarf | 4
如果我删除第三行(哪个是手套)上排(夹克和T恤工作正常) 同样下来的话。 但围巾的排名功能现在将停止......
任何人都可以提供简单的解决方案来解决这个问题。
以下是代码........
//============================ UP ================================//
if(isset($_REQUEST["up"])) {
$a=$_REQUEST["up"];
//echo $a; die();
if($a>1){
$b=$a - 1 ;
$id=$_REQUEST["id"];
//echo $id; die();
$sql =mysql_query("SELECT * FROM scat WHERE rank ='".$b."'");
while ($rc= mysql_fetch_array($sql)){
$c=$rc["sid"];
$count=mysql_num_rows($sql);
if($count == 1){
// update item with already placed id
$d=mysql_query("update scat set rank='".$a."' where sid='".$c."'");
// Update up Item
$qry=mysql_query("update scat set rank='".$b."' where sid='".$id."' ");
//header('location:main.php'); break;
}
}
}
}
//============================ Down ================================//
if(isset($_REQUEST["dwn"])) {
$a=$_REQUEST["dwn"];
//echo $a;
if($a>0) {
$b=$a + 1 ;
$id=$_REQUEST["id"];
//echo $id;die();
$sql =mysql_query("SELECT * FROM scat WHERE rank='".$b."'");
while ($rc= mysql_fetch_array($sql)){
$c=$rc["sid"];
$count=mysql_num_rows($sql);
if($count == 1){
// update item with already placed id
$d=mysql_query("update scat set rank='".$b."' where sid='".$id."'");
// Update up Item
$qry=mysql_query("update scat set rank='".$a."' where sid='".$c."' ");
//header('location:main.php'); break;
}
}
}
}
桌子到了......
<table width="600" border="0" cellspacing="1" cellpadding="0" bgcolor="#007ba4">
<tr>
<td width="50" align="center" valign="top" bgcolor="#FFF" class="ver_nav">Id</td>
<td width="350" align="center" valign="top" bgcolor="#FFF" class="ver_nav">Sub Cataagery name</td>
<td colspan="2" align="center" valign="top" bgcolor="#FFF" class="ver_nav">Rank</td>
<td width="75" align="center" valign="top" bgcolor="#FFF" class="ver_nav">Feature</td>
<td width="75" align="center" valign="top" bgcolor="#FFF" class="ver_nav">Edit</td>
<td width="75" align="center" valign="top" bgcolor="#FFF" class="ver_nav">Del</td>
</tr>
<?php $qry=mysql_query( "SELECT * FROM scat WHERE fid='".$_REQUEST[ "fid"]. "' order by rank "); while ($rc=mysql_fetch_array($qry)){ ?>
<tr>
<td width="50" align="center" valign="top" bgcolor="#FFF" class="cat">
<?php echo $rc[ "rank"];?>
</td>
<td width="350" align="center" valign="top" bgcolor="#FFF"><a href="add_pimg.php?fid=<?php echo $rc[" fid "];?>&sid=<?php echo $rc["sid "];?>" class="cat"><?php echo $rc["scat"];?></a></td>
<td width="75" align="center" valign="top" bgcolor="#FFF"> <a href="add_second_cat.php?fid=<?php echo $rc[" fid "];?>&up=<?php echo $rc["rank "];?>&id=<?php echo $rc["sid "];?>" class="cat">Up</a></td>
<td width="75" align="center" valign="top" bgcolor="#FFF"> <a href="add_second_cat.php?fid=<?php echo $rc[" fid "];?>&dwn=<?php echo $rc["rank "];?>&id=<?php echo $rc["sid "];?>"class="cat">Down</a></td>
<td width="75" align="center" valign="top" bgcolor="#FFF"> <a href="add_second_cat.php?fid=<?php echo $rc[" fid "];?>&feature=<?php echo $rc["rank "];?>&id=<?php echo $rc["sid "];?>" class="cat"><?php echo $rc["feature"];?></a></td>
<td width="75" align="center" valign="top" bgcolor="#FFF"><a href="edit_scat.php?fid=<?php echo $rc[" fid "];?>&sid=<?php echo $rc["sid "];?>" class="cat">Edit</a><a href="dell_scat.php?fid=<?php echo $rc[" fid "];?>&sid=<?php echo $rc["sid "];?>" class="cat"></a></td>
<td width="75" align="center" valign="top" bgcolor="#FFF"><a href="dell_scat.php?fid=<?php echo $rc[" fid "];?>&sid=<?php echo $rc["sid "];?>" class="cat">Dell</a></td>
</tr>
<?php }?>
</table>
答案 0 :(得分:0)
您需要选择要交换的ID,而不是计算
这是我可怕的旧项目的代码。现在看起来很有趣
if (isset($_POST['move'])) {
$id=intval($_POST['move']);
$place=db("SELECT place FROM $table WHERE id=$id");
if (!$id OR !$place) die("id or place is not set");
if (isset($_POST['up'])) {
$sort=db("SELECT sort FROM $table WHERE id=$id");
$sort2=db("SELECT max(sort) as msort FROM $table WHERE del=0 AND place=$place AND sort < $sort");
if ($sort2) $id2=db("SELECT id FROM $table WHERE del=0 AND place=$place AND sort = $sort2");
}
if (isset($_POST['down'])) {
$sort=db("SELECT sort FROM $table WHERE id=$id");
$sort2=db("SELECT min(sort) as msort FROM $table WHERE del=0 AND place=$place AND sort > $sort");
if ($sort2) $id2=db("SELECT id FROM $table WHERE del=0 AND place=$place AND sort = $sort2");
}
if ($sort2) {
$q1="UPDATE $table SET sort=$sort2 WHERE id=$id";
$q2="UPDATE $table SET sort=$sort WHERE id=$id2";
mysql_query($q1) or trigger_error(mysql_error());
mysql_query($q2) or trigger_error(mysql_error());
}
header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']."?place=$place");
exit;
}