Solved

simple link ordering

Posted on 2006-07-01
7
258 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

719 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