Jump to content

changing order of list


boblee

Recommended Posts

Hello everyone, What I'm trying to do is change the order of items in a list, the a database table I have three fields: question, answer, and number. when a button is clicked (up or down) the question and answer are swapped with their counterparts directly above or below them.

 

Now I'm a php noob so I'm sure the way I approached this is totally retarded, but here it is:

 

<?php
$query = mysql_connect("*************", "*********", "********") or die(mysql_error());
mysql_select_db('********', $query) or die(mysql_error());

if(isset($_POST['UP'])){
$number_old = ($_POST['UP']);
$number_imput = $number_old - 1 ;
$question = mysql_query("SELECT question FROM q_a WHERE number = '&number_old'");
$answer = mysql_query("SELECT answer FROM q_a WHERE question = '&question'");
$relpace_q = mysql_query("SELECT question FROM q_a WHERE number = '&number_imput'");
$replace_a = mysql_query("SELECT answer FROM q_a WHERE question = '$relpace_q'");
@mysql_query("UPDATE `q_a` (question, answer) VALUES ('$question','$answer') WHERE number = '$number_imput'"); 
@mysql_query("UPDATE `q_a` (question, answer) VALUES ('$relpace_q,'$relpace_a') WHERE number = '$number_old'"); 
}
else if(isset($_POST['Down'])){
$number_old = ($_POST['down']);
$number_imput = $number_old + 1 ;
$question = mysql_query("SELECT question FROM q_a WHERE number = '&number_old'");
$answer = mysql_query("SELECT answer FROM q_a WHERE question = '&question'");
$relpace_q = mysql_query("SELECT question FROM q_a WHERE number = '&number_imput'");
$replace_a = mysql_query("SELECT answer FROM q_a WHERE question = '$relpace_q'");
@mysql_query("UPDATE `q_a` (question, answer) VALUES ('$question','$answer') WHERE number = '$number_imput'"); 
@mysql_query("UPDATE `q_a` (question, answer) VALUES ('$relpace_q,'$relpace_a') WHERE number = '$number_old'");
}

$conn = "SELECT question FROM `q_a` ORDER BY number";
$result = mysql_query($conn, $query);

while ($row = mysql_fetch_assoc($result)) {
  $sql = mysql_query("SELECT number FROM q_a WHERE question = '" . $row['question'] . "'");
  $r = mysql_fetch_assoc($sql);
  $number_base = $r['number'];
  echo  $row['question'];
  echo "<form method='post' action='Q_A_edit.php'>";
  echo "<input type='submit' value='$number_base' name='UP'/>";
  echo "<input type='submit' value='$number_base' name='Down'/>";
  echo "</form>";
  echo "<br/>";
}
?>

 

Thanks for any help.

Link to comment
https://forums.phpfreaks.com/topic/144799-changing-order-of-list/
Share on other sites

look into Ajax

 

or using the javascript library of jQuery... it has amazing tools to do stuff like this

 

For this simple of a deal, ajax would really be over kill. You would look into onClick in Javascript to doing this, alternatively here is a site with some cool JS code that maybe right up your alley.

 

http://www.walterzorn.com/dragdrop/demos/dnd_snapandsort.htm

 

Pretty cool jscript there.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.