How to change order of display posts using php

in mysql, PHP Code / 4 Comments

Hi friends, In this post i would like to explain about how to change the order of placements using php code. It is simple code to implement. We can use this code in admin section to change the order and display those content depends on order. Today i want to share with you all.

Database

Take table that

CREATE TABLE tblOrder (
  `post_id` int(11) ,
  `sortorder` int(11) NOT NULL default '0',
)

PHP Code

At the admin section: manageorder.php

<?php
$query   = "SELECT * FROM tblOrder order by sortorder";
$result = mysql_query($query);
$num = mysql_num_rows($result);
$i=0;
while($resFetch =mysql_fetch_assoc($result)){
$i++;
 if($i==1){?>
<img src="images/down-arrow.png" title="Downwards" onClick="changeOrder('down',<?php echo $resFetch['sortorder'];?>,<?php echo $resFetch['post_id'];?>)" /><?php }
 else if($i==$no_rows){?><img src="images/up-arrow.png" title="Upwards" onClick="changeOrder('up',<?php echo $resFetch['sortorder'];?>,<?php echo $resFetch['post_id'];?>)" /><?php }
 else {?>
  <img src="images/up-arrow.png" title="Upwards" onClick="changeOrder('up',<?php echo $resFetch['sortorder'];?>,<?php echo $resFetch['post_id'];?>)" /><img src="images/down-arrow.png" title="Downwards" onClick="changeOrder('down',<?php echo $resFetch['sortorder'];?>,<?php echo $resFetch['post_id'];?>)" />
 <?php } ?>

<?php } ?>

Javascript:

function changeOrder(to,order,id)
{
    if(to == 'up'){
        document.location.href="manageorder.php?act=changeup&sortorder="+parseInt(order)+"&aid="+id;
    }else if(to == 'down'){
        document.location.href="manageorder.php?act=changedown&sortorder="+parseInt(order)+"&aid="+id;
    }  
   
}

PHP Code:

Main functionality to change the order using below code

if( isset($act) && $act == 'changeup' ){
    $current = $HTTP_GET_VARS['sortorder'];
    $res = mysql_query("SELECT sortorder,posts_id FROM tblOrder WHERE sortorder < ".(int)$current ." ORDER BY sortorder DESC LIMIT 1");
     $prev_aid = @mysql_result($res,0,'post_id');
     $prev = @mysql_result($res,0,'sortorder');
    mysql_query("update tblOrder set sortorder = '".$current."' where post_id = '" . (int)$prev_aid . "'");
    mysql_query("update tblOrder set sortorder = '".$prev."' where post_id = '" . (int)$aid . "'");
     header("Location:manageorder.php");
      exit;
}
if( isset($act) && $act == 'changedown' ){
     $current = $HTTP_GET_VARS['sortorder'];
    $res = mysql_query("SELECT sortorder,post_id FROM tblOrder  WHERE sortorder > ".(int)$current ." ORDER BY sortorder ASC LIMIT 1");
      $next = @mysql_result($res,0,'sortorder');
      $next_aid = @mysql_result($res,0,'post_id');

    mysql_query("update tblOrder  set sortorder = '".$current."' where post_id = '" . (int)$next_aid . "'");
    mysql_query("update tblOrder  set sortorder  = '".$next."' where post_id = '" . (int)$aid . "'");

     header("Location:manageorder.php");
     exit;
}

Its all about Admin section . Now at front end we can just have the query that append query with ” order by sortorder ”

Hope that it will be useful.

With more than 9 years experience in PHP and Founder of Anil Labs, a blog for PHP and related posts. contributed posts regarding CodeIgniter, CakePHP and Learn PHP online ...Contact : [email protected]

Discussion 4 Comments

  1. sumeet

    Thanx Anil, It’s a great post. i was about to ask you about this.
    It helps me a lot.
    Thank you very much.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>