Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Help with PHP, MYSQL, and AJAX sorting...
Old 12-22-2007, 08:00 PM Help with PHP, MYSQL, and AJAX sorting...
milomedia's Avatar
Super Talker

Posts: 133
Location: durango, colorado
Trades: 0
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)
__________________
_______________________________
Feeling down? listen to
Please login or register to view this content. Registration is FREE

Last edited by milomedia; 12-22-2007 at 08:02 PM..
milomedia is offline
Reply With Quote
View Public Profile Visit milomedia's homepage!
 
 
Register now for full access!
Reply     « Reply to Help with PHP, MYSQL, and AJAX sorting...
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.08658 seconds with 12 queries