Your Ad Here

Home » mysql, PHP Code »

3

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

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 ”

Hope that it will be useful.


You may like these posts

    How to implement graphs using fusion charts and phpHow to send mail using phpmailer classHow to create excel file with mysql data using php codeGrabbing thumbnail image from youtube using php code

 

3 Comments

  1. sumeet says:

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

  2. marwadi says:

    Thank you for providing the code…

Leave a Reply