hello
I'm a php developer but my javascript and my ajax experience is not good and i want a little help here is a simple code to re-arrange table rows sorting depending on the pos value which stored in mySQL db .. it works fine but if the user reload the page or pressed enter after the operation it will cause errors by duplicating the pos value .. any way please review my simple code and modify it to work fine
<?php
require_once('../../config.php');
require_once('../../inc/functions.php');
define('PAGE_NAME','view Categories');
include('../admin_header.php');
if (isset($_GET['editCC'])){
$sel_parent_info = sql_query('pos', $_GET['nPos'], 'categories');
$id = $_GET['editCC'];
$nPos = $_GET['nPos'];
$oPos = $_GET['oPos'];
$query = " UPDATE categories SET pos = {$nPos} WHERE id = {$id}";
$result = mysql_query($query, $connection);
if ($result) {
$query = " UPDATE categories SET pos = {$oPos} WHERE id = {$sel_parent_info['id']}";
$result = mysql_query($query, $connection);
if ($result){
$query = "SELECT * FROM categories ORDER BY pos ASC";
$result = mysql_query($query, $connection);
} else {
$query = " UPDATE categories SET pos = {$oPos} WHERE id = {$id}";
$result = mysql_query($query, $connection);
}
$count_results = mysql_num_rows($result);
} else {
$errors[] = "failed to re arrainge <br />" . mysql_error();
}
}
// get the posted cats
$query = "SELECT * FROM categories ORDER BY pos ASC";
$result = mysql_query($query, $connection);
$count_results = mysql_num_rows($result);
?>
<?php if (!empty($message)) { echo "<div class=\"message\">" . $message ."</div>"; }?>
<?php if (!empty($errors)) { display_errors($errors, count($errors)); }?>
<div align="center"><h1><?php echo PAGE_NAME." ({$count_results})"; ?></h1></div>
<?php
if (mysql_num_rows($result) == 0) {
echo "<div class=\"alert\">there is no categories</div>";
} else {
echo "
<h2>All Categories( {$count_results} ) </h2>
<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
<tr align=\"center\" style=\"color:#FFF\" bgcolor=\"#003366\">
<td>title</td>
<td>position</td>
<td>created</td>
<td>description</td>
<td>status</td>
<td>photo</td>
<td>sub categories</td>
<td>Actions</td>
</tr>";
if(!$result) {
die (mysql_error());
} else {
$color="1";
while($cat_info = mysql_fetch_array($result)) {
$id = $cat_info['id'];
$oPos = $cat_info['pos'];
$query1 = "SELECT * FROM sub_categories WHERE parent_id = {$id} ORDER BY pos ASC";
$result1 = mysql_query($query1, $connection);
confirm_query($result1);
if($color==1){
echo "<tr bgcolor='#999999' onMouseover=\"this.bgColor='#FFFFFF'\"onMouseout=\"this.bgColor='#999999'\">
<td>{$cat_info['name']}</td>
<td>{$cat_info['pos']}</td>
<td>{$cat_info['date']}</td>
<td>{$cat_info['comment']}</td>";
$status = set_type($cat_info['status']);
echo"
<td>{$status}</td>
<td align=\"center\"><img src=\"{$upload_url}{$cat_info['pic']}\" height='100' width='100' alt=\"قسم الـ\" /></td>
<td>";
if (mysql_num_rows($result1)>0){
while ($sub_cats = mysql_fetch_array($result1)){
echo " - ".$sub_cats['name']."<br />";
}
} else {
echo "No sub Categories";
}
echo "
</td>
<td>
<a href=\"{$_SERVER['PHP_SELF']}?editCC={$id}&oPos={$oPos}&nPos={$nPos}\"><img src=\"../images/delete.gif\" width=\"24\" height=\"24\"></a>
<a href=\"edit_cat.php?id={$id}\"><img src=\"../images/edit.gif\" width=\"24\" height=\"24\"></a>";
if ($cat_info['pos']<$count_results) {
$nPos = $cat_info['pos']+1;
echo "
<a href=\"{$_SERVER['PHP_SELF']}?editCC={$id}&oPos={$oPos}&nPos={$nPos}\"><img src=\"../images/down.gif\" width=\"24\" height=\"24\"></a>";
}
if ($cat_info['pos']>1) {
$nPos = $cat_info['pos']-1;
echo "
<a href=\"{$_SERVER['PHP_SELF']}?editCC={$id}&oPos={$oPos}&nPos={$nPos}\"><img src=\"../images/up.gif\" width=\"24\" height=\"24\"></a>";
}
echo"</td>";
$color="2";
} else {
echo "<tr bgcolor='#CCCCCC' onMouseover=\"this.bgColor='#FFFFFF'\"onMouseout=\"this.bgColor='#CCCCCC'\">
<td>{$cat_info['name']}</td>
<td>{$cat_info['pos']}</td>
<td>{$cat_info['date']}</td>
<td>{$cat_info['comment']}</td>";
$status = set_type($cat_info['status']);
echo"
<td>{$status}</td>
<td align=\"center\"><img src=\"{$upload_url}{$cat_info['pic']}\" height='100' width='100' alt=\"قسم الـ\" /></td>
<td>";
if (mysql_num_rows($result1)>0){
while ($sub_cats = mysql_fetch_array($result1)){
echo " - ".$sub_cats['name']."<br />";
}
} else {
echo "No sub Categories";
}
echo "
</td>
<td>
<a href=\"{$_SERVER['PHP_SELF']}?editCC={$id}&oPos={$oPos}&nPos={$nPos}\"><img src=\"../images/delete.gif\" width=\"24\" height=\"24\"></a>
<a href=\"edit_cat.php?id={$id}\"><img src=\"../images/edit.gif\" width=\"24\" height=\"24\"></a>";
if ($cat_info['pos']<$count_results) {
$nPos = $cat_info['pos']+1;
echo "
<a href=\"{$_SERVER['PHP_SELF']}?editCC={$id}&oPos={$oPos}&nPos={$nPos}\"><img src=\"../images/down.gif\" width=\"24\" height=\"24\"></a>";
}
if ($cat_info['pos']>1) {
$nPos = $cat_info['pos']-1;
echo "
<a href=\"{$_SERVER['PHP_SELF']}?editCC={$id}&oPos={$oPos}&nPos={$nPos}\"><img src=\"../images/up.gif\" width=\"24\" height=\"24\"></a>";
}
echo"</td>";
$color="1";
}
}
}
}
?>
</table>
<?php include('../admin_footer.php'); ?>