Solved

simple link ordering

Posted on 2006-07-01
7
249 Views
Last Modified: 2012-08-14
hi, i have a table links with
id,name,url,lorder

i need a simple up and down function, that will change the lorder, and according that i'll be able to sort them in my admin...
0
Comment
Question by:svsvsv
  • 3
  • 2
  • 2
7 Comments
 
LVL 10

Expert Comment

by:sakuya_su
ID: 17025026
lorder is numbers or characters?
0
 
LVL 10

Expert Comment

by:sakuya_su
ID: 17025028
anyway, load all the lorders into an array and use the functipon sort as described in  http://www.php.net/sort
0
 
LVL 11

Expert Comment

by:Joseph Melnick
ID: 17025260
Hello svsvsv,

assuming that you save your data in a database then you can output your links based on your
lorder field.

$query = "select id,name,url,lorder from linktable order by lorder";

Joseph Melnick
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:svsvsv
ID: 17025903
i need a "up","down" buttons to order them up in my db
0
 
LVL 11

Expert Comment

by:Joseph Melnick
ID: 17026695
Hello svsvsv,

your database can order ascending and decending.

$query = "select id,name,url,lorder from linktable order by lorder asc";
OR
$query = "select id,name,url,lorder from linktable order by lorder desc";

ASC or DESC can be passed with an up /down arrow.
list.php?direction=up
list.php?direction=down

example:

<?php
if(array_key_exists('direction',$_GET) and $_GET['direction'] == 'up' )
{
$query = "select id,name,url,lorder from linktable order by lorder asc";
}
elseif(array_key_exists('direction',$_GET) and $_GET['direction'] == 'down' )
{
$query = "select id,name,url,lorder from linktable order by lorder desc";
}
else
{
$query = "select id,name,url,lorder from linktable";
}
?>

If your intention is to use arrow buttons to manage the location of each item in your list than a more ajaxy solution might be in order.

Joseph Melnick
0
 

Author Comment

by:svsvsv
ID: 17027175
yes i was interested in using them as setting the location of each item
0
 
LVL 11

Accepted Solution

by:
Joseph Melnick earned 500 total points
ID: 17027909
Hello svsvsv,

if you want to reorder a list with lorder = 1,2,3,4,5,6,7,8,9...
and you want to click an arrow button to swap position in the list with the previous and current position OR next and current position. by simply clicking.

url on arrow with a url like the following examples:
// id is the row id, lorder is the links order and
// direction is the position you want that row to move        

....create these dynamically inside your table creation loop replacing 5 and 5 for row id, lorder from your mysql table.

<a href="showdatatable.php?id=5&lorder=5&direction=up" title="up">
<img src="/images/up.gif" alt="up">
</a>
<a href="showdatatable.php?id=5&lorder=5&direction=down title="down">
<img src="/images/down.gif" alt="down">
</a>


<?php
// this code assumes that you are connected to a mysql database prior to
// updating these tables.

if(array_key_exists('lorder',$_GET) and $_GET['direction'] == 'up')
{
  // change position with element above.
  $row_to_change = $_GET['id'];
  $new_lorder = $_GET['lorder'] - 1;
  // update previous record
  $query1 = "UPDATE linktable SET lorder=lorder+1 where lorder = " . $new_lorder;
  mysql_query($query1);
  $query2 = "UPDATE linktable SET lorder=".$new_lorder." where id = " . $row_to_change;
  mysql_query($query2);
}
elseif (array_key_exists('lorder',$_GET) and $_GET['direction'] == 'down')
{
  // change position with element below.
  $row_to_change = $_GET['id'];
  $new_lorder = $_GET['lorder'] + 1;
  // update previous record
  $query1 = "UPDATE linktable SET lorder=lorder-1 where lorder = " . $new_lorder;
  mysql_query($query1);
  $query2 = "UPDATE linktable SET lorder=".$new_lorder." where id = " . $row_to_change;
  mysql_query($query2);
}
else
{
  //do nothing
}
// build your table here

?>

Joseph Melnick



0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now