So this is one of those days... i feel like i want to beat my head into the desk repeatedly. I followed a tutorial on how to make a sortable list in PHP using a little touch of Ajax. It uses the Scriptalicious library I actually got it working nicely, the code is below:
The main sorting page
PHP Code:
<?php
require_once('database.php');
require_once('categories.php');
if (!dbConnect()) {
echo 'Error connecting to database';
exit;
}
$cats = getCategories();
?>
<html>
<head>
<script type="text/javascript" src="../scriptaculous-js-1.7.0/lib/prototype.js"></script>
<script type="text/javascript" src="../scriptaculous-js-1.7.0/src/scriptaculous.js"></script>
</head>
<body>
<br> To Sort items, simply drag and drop them in the proper order. When you're done, just click "back to manager" <br><br>
<h1>Categories</h1>
<ul id="category_list" class="sortable-list">
<?php foreach ($cats as $catid => $itemname) { ?>
<li id="category_<?= $catid ?>"><?= $itemname ?></li>
<?php } ?>
</ul>
<script type="text/javascript">
function updateOrder()
{
var options = {
method : 'post',
parameters : Sortable.serialize('category_list')
};
new Ajax.Request('processor.php', options);
}
Sortable.create('category_list', { onUpdate : updateOrder });
</script>
<br>
<a href="categorymanager.php">Back To Manager</a>
</body>
</html>
The categories.php you saw which defines the functions called here:
PHP Code:
<?php
function getCategories()
{
$query = 'select catid, itemname from pagecategories order by sort, lower(itemname)';
$result = mysql_query($query);
$cats = array();
while ($row = mysql_fetch_object($result)) {
$cats[$row->catid] = $row->itemname;
}
return $cats;
}
function processCatOrder($key)
{
if (!isset($_POST[$key]) || !is_array($_POST[$key]))
return;
$catlist = getCategories();
$queries = array();
$ranking = 1;
foreach ($_POST[$key] as $catid) {
if (!array_key_exists($catid, $catlist))
continue;
$query = sprintf('update pagecategories set sort = %d where catid = %d',
$sort,
$catid);
mysql_query($query);
$sort++;
}
}
?>
And finally the processor page
PHP Code:
<?php
require_once('database.php');
require_once('categories.php');
if (!dbConnect())
exit;
processCatOrder('category_list');
?>
So Like I said, this worked perfectly. In fact... it still does... when you rearrange the items via drag and drop, they automatically save the state. The stinker is when I tried to use this to sort pages that belong to these categories. I just made copies of the scripts and changed the field names, sql query, and added a variable to the getCategories() function... Here's what I got:
The main page
PHP Code:
<?php
require_once('database.php');
require_once('pages.php');
if (!dbConnect()) {
echo 'Error connecting to database';
exit;
}
$pages = getPages($category);
?>
<html>
<head>
<script type="text/javascript" src="../scriptaculous-js-1.7.0/lib/prototype.js"></script>
<script type="text/javascript" src="../scriptaculous-js-1.7.0/src/scriptaculous.js"></script>
</head>
<body>
<br> To Sort items, simply drag and drop the images in the proper order. When you're done, just click "back to manager" <br><br>
<h1>Pages</h1>
<ul id="category_list" class="sortable-list">
<?php foreach ($pages as $pageid => $itemname) { ?>
<li id="category_<?= $pageid ?>"><?= $itemname ?></li>
<?php } ?>
</ul>
<script type="text/javascript">
function updateOrder()
{
var options = {
method : 'post',
parameters : Sortable.serialize('category_list')
};
new Ajax.Request('pageprocessor.php', options);
}
Sortable.create('category_list', { onUpdate : updateOrder });
</script>
<br>
</body>
</html>
The pages.php page:
PHP Code:
<?php
function getPages($category)
{
$query = "select pageid, itemname from pages where linkheading=$category order by sort, lower(itemname)";
$result = mysql_query($query);
$cats = array();
while ($row = mysql_fetch_object($result)) {
$cats[$row->pageid] = $row->itemname;
}
return $cats;
}
function processPageOrder($key)
{
if (!isset($_POST[$key]) || !is_array($_POST[$key]))
return;
$catlist = getPages($category);
$queries = array();
$sort = 1;
foreach ($_POST[$key] as $pageid) {
if (!array_key_exists($pageid, $catlist))
continue;
$query = sprintf('update pages set sort = %d where pageid = %d',
$sort,
$pageid);
mysql_query($query);
$sort++;
}
}
?>
And the page processor
PHP Code:
<?php
require_once('database.php');
require_once('pages.php');
if (!dbConnect())
exit;
processPageOrder('category_list');
?>
The second example lists only the pages that have their category set as the same value as $category, so that part works. I can drag and drop the items and that works, however, when I refresh, nada... the results aren't being written.. Can anyone see what I'm missing here? I have a feeling it is stupid, but I just can't see it and after 6 hours, i'm at a loss. This is slightly more advanced that my current level of php programming for sure..
Any help will be appreciated and most likely get you big wet slobbery kisses (from my dog... who is very sweet)