• 0

PHP to change link ranking in mysql database


Question

Hi.

I am making a page where I can edit my links.

I have created two buttons which when clicked moves the link up or down the list

by changing a ranking number in the mysql database.

At first, my code seemed to work just fine untill i added a few more links and just moved them up and down at random to check that everything works as it should. at some point some links started to get the

same ranking number, which messed up the whole list.

The code is:

if (isset($_POST['group_up']))
{
if ($_POST['upvalue']==1)
{
header( 'location:menu.php' );
}
else
{
$old = $_POST['upvalue'];
$up = $_POST['upvalue']-1;
$up_id = $_POST['group_id'];
$conn = mysql_connect("$db_host","$db_user","$db_pass");
if (!$conn) die ("Could not connect MySQL");
mysql_select_db($db_database,$conn) or die ("Could not open database");
$query = "UPDATE menu_groups SET ranking = $old WHERE ranking = $up;";
mysql_query($query) or die('Error, query failed');
$query = "UPDATE menu_groups SET ranking = $up WHERE group_id = $up_id;";
mysql_query($query) or die('Error, query failed');
}
}

And similar for the down button just it adds to the upvalue istead of subtract.

Annyone notice why this code bugs?

Also if you have suggestions on how to do this another way please tell me.

Thanks for your help

Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0

You always always need to check the endpoints of something like this, so you need two if statements to check whether the current position is already 0, which means you can go up, and the other site whcih will not allow you to move down the last item of the list.

Link to comment
Share on other sites

  • 0

Maybe someone else could help but I'd like to see the page and a screenshot of the database (phpmyadmin in browse mode would do).

Link to comment
Share on other sites

  • 0

Bah, wrote some ordering code the other week, but it's at work (internal devel server) and i'm not back till tuesday, if you're still stuck then i'll post it, cos i cba rewriting it :p In fact i'll probably post it anyway if i remeber it.

Link to comment
Share on other sites

  • 0

Bah, wrote some ordering code the other week, but it's at work (internal devel server) and i'm not back till tuesday, if you're still stuck then i'll post it, cos i cba rewriting it :p In fact i'll probably post it anyway if i remeber it.

What work do you do? btw, nice www.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.