small query problem
Posted: Thu Dec 23, 2004 3:33 pm
I'm trying to create a small links section where I can modify the order of links through a series of MOVE UP and MOVE DOWN links.
for example,
now, when one of those links are clicked it will run
Now heres where my problems come in, I'm the farthest thing from a mysql guru and have been unable to create the update query to set 2 different rows with different conditions. Any help would be appreciated there.
My question is, how can I succesfuly accomplish this move position business if one of the rows is deleted by an admin. It will attempt to update a row that does not exist, because the position has been deleted.
my table looks like this
THanks in advance 
for example,
Code: Select all
<?php
$sql = "SELECT * FROM `links` ORDER BY `position`";
$result = $db->sql($sql);
if (mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result))
{
if ($_SESSION['loggedin'] == 1 && isset($_GET['admin']))
{
$template->svar(array('LINKS_ADMIN' => '<a href="?goto=links&admin=1&move=up&position='.$row['position'].'">Move Up</a> <a href="?goto=links&admin=1&move=down&position='.$row['position'].'">Move Down</a>'));
}
$template->loadfile('links');
}
}
?>Code: Select all
<?php
if ($_SESSION['loggedin'] == 1 && !empty($_GET['admin']) && !empty($_GET['move']) && !empty($_GET['position']))
{
switch ($_GET['move'])
{
case 'up' :
$sql = "UPDATE `links` SET `position` = (`position` + 1) WHERE `position` = '".$_GET['position']."', SET `position` = (`position` - 1) WHERE `position` = '".($_GET['position'] + 1)."'";
break;
case 'down' :
$sql = "UPDATE `links` SET `position` = (`position` - 1) WHERE `position` = '".$_GET['position']."', SET `position` = (`position` + 1) WHERE `position` = '".($_GET['position'] - 1)."'";
break;
}
$db->sql($sql);
}
?>My question is, how can I succesfuly accomplish this move position business if one of the rows is deleted by an admin. It will attempt to update a row that does not exist, because the position has been deleted.
my table looks like this
Code: Select all
ID (auto-incremented)
POSITION
TITLE
etc.