jessnoonyes
08-14-2009, 01:23 AM
I'm just starting out with learning mySQL and I'm struggling with understanding what is probably a basic concept, which is passing information from PHP or Javascript into the database. I was hoping you guys could help me with one thing I'm attempting, and maybe explain a couple of things I'm unclear of.
I'm able to query the database and display a list of one of the tables. Now I'm following an example here http://www.wil-linssen.com/extending-the-jquery-sortable-with-ajax-mysql/ to make the list sortable. The problem I'm having is with updating the database with the new order. Here's my database query (if that's what it's called)
<?php include("config/config.php");
$query = "SELECT * FROM clientList ORDER BY id";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo "<li id='listItem_".$row['id']."'>
<img src='images/arrow.png' alt='move' width='16' height='16' class='handle' />
<h2>".$row['Name']."</h2>
....etc
That works fine. The javascript that makes it sortable is:
$(document).ready(function() {
$("#test-list").sortable({
handle : '.handle',
update : function () {
var order = $('#test-list').sortable('serialize');
$("#info").load("config/sort.php?"+order);
}
});
});
It's supposed to look at sort.php and send the new order to the database I believe. Now here is where I think I'm stuck, which is probably the way I have sort.php set up:
<?php include("config.php");
$con = mysql_connect("localhost","$username","$password");
if (!$con) {
die('Could not connect: ' .mysql_error());
}
mysql_select_db("micro_todolist", $con);
foreach ($_GET['listItem'] as $position => $item) :
$sql[] = "UPDATE clientList SET 'position' = $position WHERE 'id' = $item";
endforeach;
print_r ($sql);
?>
I assume this is where I'm going wrong because the list will sort on my page, just not update on the server. The "foreach" part down is an example I got off of the other site and I'm not totally sure what it's doing. For example, where is the ['listItem'] coming from? And the "$item"? Is that saying something like "Update the clientList table and set 'position', where 'position' is the ID of item?
I'm just trying to understand what this part is actually trying to do, so I know how to write it myself in the future. And figure out how to pass the sorted ID's of my list to be changed and updated in the database...I'd really appreciate any guidance! Sorry for being such a noob.
I'm able to query the database and display a list of one of the tables. Now I'm following an example here http://www.wil-linssen.com/extending-the-jquery-sortable-with-ajax-mysql/ to make the list sortable. The problem I'm having is with updating the database with the new order. Here's my database query (if that's what it's called)
<?php include("config/config.php");
$query = "SELECT * FROM clientList ORDER BY id";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo "<li id='listItem_".$row['id']."'>
<img src='images/arrow.png' alt='move' width='16' height='16' class='handle' />
<h2>".$row['Name']."</h2>
....etc
That works fine. The javascript that makes it sortable is:
$(document).ready(function() {
$("#test-list").sortable({
handle : '.handle',
update : function () {
var order = $('#test-list').sortable('serialize');
$("#info").load("config/sort.php?"+order);
}
});
});
It's supposed to look at sort.php and send the new order to the database I believe. Now here is where I think I'm stuck, which is probably the way I have sort.php set up:
<?php include("config.php");
$con = mysql_connect("localhost","$username","$password");
if (!$con) {
die('Could not connect: ' .mysql_error());
}
mysql_select_db("micro_todolist", $con);
foreach ($_GET['listItem'] as $position => $item) :
$sql[] = "UPDATE clientList SET 'position' = $position WHERE 'id' = $item";
endforeach;
print_r ($sql);
?>
I assume this is where I'm going wrong because the list will sort on my page, just not update on the server. The "foreach" part down is an example I got off of the other site and I'm not totally sure what it's doing. For example, where is the ['listItem'] coming from? And the "$item"? Is that saying something like "Update the clientList table and set 'position', where 'position' is the ID of item?
I'm just trying to understand what this part is actually trying to do, so I know how to write it myself in the future. And figure out how to pass the sorted ID's of my list to be changed and updated in the database...I'd really appreciate any guidance! Sorry for being such a noob.