0

I wrote my own CMS solution and I have always had problems with changing the order of pages. On my database I have a column named order which takes the automatic id of the page. For example if pageid = 16 then order = 16.

function shift_page_up($pageid, $pageorder, $db, $database_db){

    mysql_select_db($database_db, $db);
    $sql_previouspage = "SELECT order from pages WHERE `order` < $pageorder ORDER by `order` DESC LIMIT 1  ";
    $previouspage_result = mysql_query($sql_previouspage) or display_error(mysql_error());
    $previouspage_row = mysql_fetch_assoc($previouspage_result);

    $newpreviouspage_pageorder = $pageorder;

    $newpageorder = $previouspage_row['order'];

    $sql_update_currentpageorder = "UPDATE pages SET `order` = $newpageorder WHERE `pageid` = $pageid";

    $update_currentpageorder = mysql_query($sql_update_currentpageorder) or display_error(mysql_error());

    $sql_update_previouspageorder = "UPDATE pages SET `order` = $newpreviouspage_pageorder WHERE `pageid` = ".$previouspage_row['pageid']."";
    $update_previouspageorder = mysql_query($sql_update_previouspageorder) or display_error(mysql_error());
    }

function shift_page_down($pageid, $pageorder, $db, $database_db){
    mysql_select_db($database_db, $db);
    $sql_nextpage = "SELECT * from pages WHERE `pageid` = $pageid AND `order` > $pageorder ORDER by `order` ASC LIMIT 1 ";
    $nextpage_result = mysql_query($sql_nextpage) or display_error(mysql_error());
    $nextpage_row = mysql_fetch_assoc($nextpage_result);

    $newnextpage_pageorder = $pageorder;

    $newpageorder = $nextpage_row['order'];

    $sql_update_currentpageorder = "UPDATE pages SET `order` = $newpageorder WHERE `pageid` = $subpageid";

    $update_currentpageorder = mysql_query($sql_update_currentpageorder) or display_error(mysql_error());

    $sql_update_nexpageorder = "UPDATE pages SET `order` = $newnextpage_pageorder WHERE `pageid` = ".$nextpage_row['pageid']."";
    $update_nexpageorder = mysql_query($sql_update_nexpageorder) or display_error(mysql_error());
}

So the main idea is to swap the order of the pages on the ORDER column duplicated from their page id's which are automatically generated. I have already coded mechanisms to avoid swapping pages with the highest and lowest order using SELECT MIN(order) and SELECT MAX(order) respectively.

To shift the order up the anchor link <a href="order_up.php?pageid=<?php echo $row_get_pages['pageid']; ?>&pageorder=<? echo $row_get_pages['order']; ?>&swapped=<?php echo str_replace( " ", "-", $row_get_pages['pagename']); ?>"> <img onclick="bounce_page('<?php echo $row_get_pages['pagename']; ?>');" src="images/ico/arrow-090.png" width="16" height="16" align="right" style="cursor:pointer;" /></a> is clicked

To shift the order down the anchor link <a href="order_down.php?pageid=<?php echo $row_get_pages['pageid']; ?>&amp;pageorder=<? echo $row_get_pages['order']; ?>&swapped=<?php echo str_replace( " ", "-", $row_get_pages['pagename']); ?>"><img onclick="bounce_page('<?php echo $row_get_pages['pagename']; ?>');" src="images/ico/arrow-270.png" width="16" height="16" style="cursor:pointer;" /> </a> is clicked

This beautifully works at times especially when the pages table is truncated and the page id's start at 0. But an emptied table or a page deleted somewhere always bring issues. When I click on any up or down link no values are swapped

4

0 回答 0