Up/Down order mySQL table entries doesn't work
Posted: Tue Mar 02, 2010 5:45 am
I am trying to make an ordering system for an article system
It will look like this:
article title | move up - move down
move up and down would re-sort the article order by swapping the row_order values, at least that was my idea.
For some reason the current row updates but the next (or previous) doesn't.
It will look like this:
article title | move up - move down
move up and down would re-sort the article order by swapping the row_order values, at least that was my idea.
For some reason the current row updates but the next (or previous) doesn't.
Code: Select all
<?php
/* include the class file for the database connection */
require_once('sql-database-class.php');
$db = new sqlDatabase('mysql','utf8','localhost','test');
$db->sqlConnect('root','');
if ($_GET['action']=='sort')
{
$row_id = $_GET['row_id'];
$row_order = $_GET['row_order'];
$go = $_GET['go'];
if ($go == 'up')
{
//get previous item order
$row_order2 = $row_order-1;
$db_result3 = $db->sqlQuery("SELECT * FROM `articles` WHERE row_order='$row_order2'");
while ($row = $db->sqlFetch($db_result3))
{
$row_order2 = $row['row_order'];
}
//DEBUG
echo 'current item order was '.$row_order.' and now will be '.$row_order2.'<br/>
the previous item order was '.$row_order2.' and it will be '.$row_order.'<br/>';
//swap orders
$db->sqlQuery("UPDATE `articles` SET `row_order`='$row_order' WHERE `row_order`='$row_order2' ");
$db->sqlQuery("UPDATE `articles` SET `row_order`='$row_order2' WHERE `row_order`='$row_order' ");
echo 'sorted<br/><br/>'.$db->sqlError();
}
elseif ($go == 'down')
{
//get next item order
$row_order2 = $row_order+1;
$db_result3 = $db->sqlQuery("SELECT * FROM `articles` WHERE row_order='$row_order2'");
while ($row = $db->sqlFetch($db_result3))
{
$row_order2 = $row['row_order'];
}
//DEBUG
echo 'current item order was '.$row_order.' and now will be '.$row_order2.'<br/>
the next item order was '.$row_order2.' and it will be '.$row_order.'<br/>';
//swap orders
$db->sqlQuery("UPDATE `articles` SET `row_order`='$row_order' WHERE `row_order`='$row_order2' ");
$db->sqlQuery("UPDATE `articles` SET `row_order`='$row_order2' WHERE `row_order`='$row_order' ");
echo 'sorted<br/><br/>'.$db->sqlError();
}
}
$db_result1 = $db->sqlQuery("SELECT * FROM `articles` ORDER BY `row_order`");
/* return field values sorted by row_order */
while ($row = $db->sqlFetch($db_result1))
{
echo $row['row_title'].' ['.$row['row_order'].']
<form style="display:inline" action="index.php" method="get">
<input type="hidden" name="action" value="sort" />
<input type="hidden" name="go" value="up" />
<input type="hidden" name="row_id" value="'.$row['row_id'].'" />
<input type="hidden" name="row_order" value="'.$row['row_order'].'" />
<input type="submit" value="up" />
</form>
<form style="display:inline" action="index.php" method="get">
<input type="hidden" name="action" value="sort" />
<input type="hidden" name="go" value="down" />
<input type="hidden" name="row_id" value="'.$row['row_id'].'" />
<input type="hidden" name="row_order" value="'.$row['row_order'].'" />
<input type="submit" value="down" />
</form>
<br/>
';
}
/* close connection to the database */
$db->sqlDisconnect();
?>