boblee Posted February 11, 2009 Share Posted February 11, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/144799-changing-order-of-list/ Share on other sites More sharing options...
premiso Posted February 11, 2009 Share Posted February 11, 2009 Without a page reload, you will need Javascript to do this. I would suggest looking down that alley, as it seems to be what you want. Quote Link to comment https://forums.phpfreaks.com/topic/144799-changing-order-of-list/#findComment-759831 Share on other sites More sharing options...
boblee Posted February 11, 2009 Author Share Posted February 11, 2009 So, I should look at event handelers? -Thanks Quote Link to comment https://forums.phpfreaks.com/topic/144799-changing-order-of-list/#findComment-759843 Share on other sites More sharing options...
dennismonsewicz Posted February 11, 2009 Share Posted February 11, 2009 look into Ajax or using the javascript library of jQuery... it has amazing tools to do stuff like this Quote Link to comment https://forums.phpfreaks.com/topic/144799-changing-order-of-list/#findComment-759846 Share on other sites More sharing options...
premiso Posted February 11, 2009 Share Posted February 11, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/144799-changing-order-of-list/#findComment-759852 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.