Hello, friends! In this post, I would like to explain how to change the order of placements using PHP code. This is a straightforward code that can be implemented. You can utilize this code in the admin section to modify the order and display content based on the specified order. Today, I’m excited to share this with all of you.

In the realm of web development, having the flexibility to customize the order in which posts are displayed is a powerful tool for content management. In this article, we’ll explore the process of changing the order of displaying posts using PHP. Whether you want to highlight specific content, showcase the most recent updates, or tailor the presentation of your posts to your unique needs, this guide will provide you with the skills and knowledge to achieve a dynamic and personalized display order on your website. Learn how to harness the potential of PHP to enhance your content management.

Database

Take table that

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

PHP Code

At the admin section: manageorder.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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:

1
2
3
4
5
6
7
8
9
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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 ”

In conclusion, the ability to change the order of displaying posts using PHP opens up a world of possibilities for web developers and content managers. This article has equipped you with the know-how to dynamically customize the presentation of your posts, ensuring that your content is presented in a way that best serves your objectives. Whether you’re running a blog, an e-commerce site, or any other type of website, the power to control the order of posts enhances user experience and content visibility. With these newfound skills, you can create a more engaging and tailored presentation of your posts, ultimately improving the impact and effectiveness of your online content.


4 Comments

sumeet · August 17, 2010 at 3:48 pm

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

marwadi · September 8, 2010 at 9:32 am

Thank you for providing the code…

kumaravel · August 7, 2013 at 4:59 am

didn’t get output…..

Anonymous · August 15, 2010 at 4:15 pm

[…] how to change order of display posts using php « ANIL KUMAR … […]

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *