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
Solved

simple link ordering

Posted on 2006-07-01
7
255 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
000webhost.com default error log 1 40
php help 34 58
Restricting the scope of variables to private in OOP 4 22
Inserting data into database 10 35
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.  …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
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.

840 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