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! | Join us for this on demand webcast to learn about developing complex systems more quickly and efficiently. We'll cover market drivers for developing, governing and reusing systems software assets and how you can develop system software assets with Rational Asset Manager. FREE! Go There Now!
| | | | Visit IBM developerWorks to download IBM DB2 Express-C 9.5, a no-charge version of DB2 Express 9 database server. DB2 Express-C offers the same core data server base features as other DB2 Express editions and provides a solid base to build and deploy applications developed using C/C++, Java, .NET, PHP, and other programming languages. 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!
| | | | Portfolio Management is about effectively managing portfolio value by aligning portfolio investments with business goals. This complimentary e-kit provides a collection of materials that can help you understand how IBM Rational enables and automates best practices for improved governance and clear visibility into portfolio and project performance across the entire IT project lifecycle. FREE! Go There Now!
| | | | Get a free trial download of IBM Lotus Forms V3.0 (formerly Workplace Forms), which provides a zero-footprint eForms solution to help you automate and move forms-based business processes off the desktop and onto the Web. With Lotus Forms, you can extend applications beyond the firewall by creating a single electronic form document ready for use in both thick and Web 2.0 thin client format. FREE! Go There Now!
| | | | You can now evaluate IBM Rational Asset Manager V7.0 online without installing or configuring it on your own system! Rational Asset Manager helps create, modify, govern, find, and reuse any type of development assets, including SOA and systems development assets. Rational Asset Manager helps you reduce software development costs and improve quality by facilitating the reuse of all types of software development-related assets. Visit developerWorks to learn more about this product and register to explore its capabilities online. FREE! Go There Now!
| | | | Join the IBM Watchfire team for an informative discussion on techniques and best practices to proactively manage Web application security and how to effectively build application security testing into the software development lifecycle (SDLC). In this Software Delivery Platform webcast you will learn: How to better understand potential web application security vulnerabilities, best practices and how to effectively integrate application security testing into the software development lifecycle, the importance of detecting and removing software vulnerabilities during application development. FREE! Go There Now!
| | | | The Eclipse community is constantly working to extend Eclipse's functionality. In this webcast, learn about some of the most important and feature-rich projects under development. From multi-language support to plug-in development, tune in to see what Eclipse is capable of now. FREE! Go There Now!
| | | | The discipline of assembling and delivering software is maturing beyond standard developer-centric compile/test software builds. The end-to-end software development lifecycle is emerging as the new focus moves “Beyond the Build.” Join this on demand webcast to learn about methods for streamlining software delivery and key capabilities of the IBM Rational Build Forge framework for automating build and release management in environments of any size. FREE! Go There Now!
| | | | Explore how Rational and WebSphere software enable enterprise documentation in SOA environments. Specifically, a new integration between IBM WebSphere® Business Modeler and IBM Rational® Method Composer software can help technical writers more easily keep enterprise operations manuals in sync with changes that are made to business processes, resulting in more accurate and timely documentation that benefits the entire enterprise. FREE! Go There Now!
| | | | All FREE IBM® developerWorks Tools! | |