Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.