This code will allow a user interface for people to manually re-order records in a database. It is currently coded to work in a table (The first
holding the arrows for moving the records up or down). Please email me and Let me know how it works. Also, if you are interested in an addon that will allow for limiting records per page (e.g. Showing 1-20 of 54 records) and previous and next links please let me know at bgintz@novvia.com. Thanks
By : novvia
<? ############################################################################## #This Library Allows for Items to be ordered by using arrowed buttons next #to the Record. ############################################################################## #Name: updown.php3 #Version: 1.0 #Written By: Bryan Gintz [bgintz@novvia.com] #Copyrighted by Novvia and Bryan Gintz #NOTE!: If you use this script or make improvments, please contact me, I would like to know :). #Thanks ##############################################################################
############################################################################## #Edit these Variables when first setting this up ############################################################################## $up_image = "images/up.gif"; $down_image = "images/down.gif"; ##############################################################################
############################################################################## #This next section should be placed in the first column of the table #that holds the data. Make sure that you put any additional needed hidden #fields that hold the data that you need so you see the correct listings #(in a new order). ############################################################################## #print "<td width=23 nowrap><form name=updown action=NAME_OF_SCRIPT method=POST>"; #print "<input type=hidden name=crn value='RECORD_CRN'>"; #print "<input type=hidden name=action value='move'>"; #print "<input type=hidden name=order value='RECORD_PORDER'>"; #if($x) # print "<input type=image src=$up_image name=up value=up border=0>"; #if(($x+1) != NUMBER_OF_RECORDS) # print "<input type=image src=$down_image name=down value=down border=0>"; #print "</td></form>"; #$x++; ############################################################################## #At the start of your script have a conditional clause that checks to see if #$action == "move" (on line 25 above). If so then call the function with #the indicated arguements: ############################################################################## #if($up_x || $up_y) # move(TABLE_NAME, RECORD_P_ORDER, RECORD_CRN, 1); #else # move(TABLE_NAME, RECORD_P_ORDER, RECORD_CRN, 0); ##############################################################################
############################################################################## #Here is the line you need to add to the tables that will be ordered #If you add this to an existing database you need to populate the p_order #field for every record before this will work. ############################################################################### #p_order INT(10) NOT NULL ##############################################################################
############################################################################## #Make sure you order your main statement by p_order! ############################################################################## #$stmt = "SELECT * FROM table ORDER BY p_order"; ##############################################################################
############################################################################## #Make sure when you are adding a new record to the table that you insert it #with the order. You use the function getNumElements() to get the largest #number then you add one. ############################################################################## #$number = getNumElements("pricing") + 1; ##############################################################################
function getNumElements($table) { $stmt = "SELECT MAX(p_order) as num FROM $table"; $result = mysql_query($stmt); $obj = mysql_fetch_object($result); return $obj->num; }
function move($table, $order, $crn, $up) { function checkOrder($table, $order, $updown) { if($updown == "up") $stmt = "select * from $table where p_order < '$order' ORDER BY p_order DESC LIMIT 1"; else $stmt = "select * from $table where p_order > '$order' ORDER BY p_order LIMIT 1"; $result = mysql_query($stmt); mysql_error(); $obj = mysql_fetch_object($result); $number = mysql_num_rows($result); if($number) return $obj->crn."|".$obj->p_order; else return; } if($up) { list($move_crn, $new_order) = split("\|", checkOrder($table, $order, "up")); $move_stmt = "UPDATE $table set p_order='$new_order' WHERE crn='$crn'"; $move2_stmt = "UPDATE $table set p_order='$order' WHERE crn='$move_crn'"; mysql_query($move_stmt); mysql_error(); mysql_query($move2_stmt); mysql_error(); } else { list($move_crn, $new_order) = split("\|", checkOrder($table, $order, "down")); $move_stmt = "UPDATE $table set p_order='$new_order' WHERE crn='$crn'"; $move2_stmt = "UPDATE $table set p_order='$order' WHERE crn='$move_crn'"; mysql_query($move_stmt); mysql_error(); mysql_query($move2_stmt); mysql_error(); } }
?>
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More Database Code Articles More By Codewalkers developerWorks - FREE Tools! | This demonstration gives you an overview of IBM® Rational® Build Forge Express Edition, a global offering that provides a framework to automate and execute software processes. Rational Build Forge provides a software assembly line that can support all of your tools, technologies, and platforms so you can achieve a repeatable, reliable, and traceable build and release process. FREE! Go There Now!
| | | | Attend this launch webcast with Scott Hebner, Vice President of IBM Rational Marketing and Strategy, for an overview of Rational’s new software offerings and resources to help modernize and accelerate software innovation on i on Power Systems – while ensuring past application investments are protected and continue to grow. Learn how these solutions are helping customers extend their core i5/OS solutions toward modern architectures such as SOA and web technologies to deliver business improvements that stand the test of time. FREE! Go There Now!
| | | | Poor Requirements Management capabilities in an Enterprise have been linked to excessive project failures, escalating IT costs, and failure to deliver competitive advantage into the marketplace. Join Brianna M Smith from IBM Rational and learn about how successful organizations align IT and Business stakeholders through collaborative processes and tools for effective requirements management, and how an integrated approach across the IT lifecycle can provide unparalleled visibility and traceability to ensure that project teams are delivering on the business vision by "doing the right things" and "doing things right." FREE! Go There Now!
| | | | Download a free trial version of IBM DB2 9.5 for Linux, UNIX, and Windows. DB2 9 is the result of a five-year development project that transformed traditional (static) database technology into an interactive data server that merges the high performance and ease of use of DB2 with the self-describing benefits of XML. FREE! Go There Now!
| | | | Visit IBM developerWorks to download a free trial of the latest release of IBM Lotus Sametime Standard V8.0. Lotus Sametime Standard V8.0 is a platform for unified communications and collaboration that combines security features with an extensible, open solution including integrated Voice over IP, geographic location awareness, mobile clients, and a robust Business Partner community offering telephony and video integration. FREE! Go There Now!
| | | | Visit IBM developerWorks to download a free trial version of WebSphere Extended Deployment Compute Grid, which lets you schedule, execute, and monitor batch jobs. Because online transaction processing and batch jobs execute simultaneously on the same server resources, you can avoid costly duplication of resources. Compute Grid supports job types of Java transactional batch, compute-intensive and a new type called "native execution", which enables non-Java workloads to run on distributed end points. FREE! Go There Now!
| | | | Join us for this web seminar to learn how you can defend your web applications from attack. Learn about the 3 most common web application attacks, including how they occur and what can be done to prevent them. We’ll also discuss manual versus automated approaches for scanning and identifying web application vulnerabilities and how IBM Rational AppScan, an automated vulnerability scanner, can help you automate more of what you are doing manually today. FREE! Go There Now!
| | | | Whether you are creating new applications or modifying existing ones, managing integration of new components with traditional z/OS elements is a critical part of building and deploying modern applications. Listen to this webcast to see how IBM can help you optimize your development process using an IDE like Rational Developer for System z that integrates with management tools, such as ClearCase to manage your application development on mainframes. FREE! Go There Now!
| | | | Join this webcast to learn how IBM Rational's Functional Testing solution enables you to implement automation your way, at your pace, with your existing staff. In this webcast, you’ll learn how you can eliminate redundancy of manual test scripts, reduce errors, and increase test coverage through test automation. After this presentation you will understand how IBM Rational Functional Testing solution can streamline your manual testing and make test automation easily attainable. FREE! Go There Now!
| | | | WebSphere Process Server delivers a unique integration framework that simplifies existing IT resources. Often, as IT assets grow to support business demand, so too does their complexity and manageability. In this webcast, we’ll discuss how WebSphere Process Server helps deliver an SOA infrastructure that provides a common model to orchestrate, mediate, connect, map, and execute the underlying IT functions. Discover how WebSphere Process Server simplifies integration of business processes by leveraging existing IT assets as reusable services without the complexities of traditional integration methodologies. FREE! Go There Now!
| | | | All FREE IBM® developerWorks Tools! | |