dragunoff

wpquestions.com, ID = 2790

Aug 5th, 2011
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.85 KB | None | 0 0
  1. function room_current_select_location_number_of_racks( $attr ) {
  2.  
  3.     global $wpdb;
  4.  
  5.     // Get shortcode parameters :
  6.     // Date Range (by month).  Default: Last Calendar Month
  7.     $date = ( isset( $attr['date'] ) ) ? $attr['date'] : date('m/Y',strtotime("last month"));
  8.  
  9.     // Room_desc. Default value: ALL Rooms  (if no parameter is provided, it returns all rooms)
  10.     $room_desc = ( isset( $attr['room_desc'] ) ) ? "AND L.room_desc = '" . $attr['room_desc'] . "'" : '';
  11.  
  12.     //Item_category.  Default value:  ALL Item Categories  (if no parameter is provided, it returns all categories)
  13.     $item_category = ( isset( $attr['item_category'] ) ) ? "AND I.item_category = '" .$attr['item_category']  . "'": '';
  14.  
  15.     // Set up query and make sure it's always secure (no malformed SQL injection etc...)
  16.     $query = $wpdb->prepare( "
  17.         SELECT
  18.             I.item_desc,
  19.             SUM(T.quantity)
  20.  
  21.         FROM
  22.             wp_transactions T,
  23.             wp_location L,
  24.             wp_items I
  25.  
  26.         WHERE
  27.             T.item_id=I.item_id
  28.             AND T.location_id=L.location_id
  29.             $item_category      // 'Red Widgets, Blue Widgets, etc
  30.             $room_desc          //ROOM 4A, ROOM 4B, etc
  31.             AND T.date= '$date' //  03/2011, 05/2011 etc
  32.  
  33.         GROUP BY
  34.             I.item_desc
  35.            
  36.         ORDER BY
  37.             I.item_name
  38.  
  39.     " );
  40.  
  41.     $r = $wpdb->get_var( $query );
  42.    
  43.     // next few lines simply print the results of the query on screen, nothing fancy
  44.     echo '<pre>';
  45.     print_r ( $r );
  46.     echo '</pre>';
  47.  
  48. }
  49.  
  50. // Register a new shortcode
  51. // Usage :
  52. // [19]
  53. // [19 room_desc="ROOM 4A"]    returns Room 4A, and defaults values:   ALL item_category, date=Last Calendar Month
  54. // [19 item_category="Blue Widgets"]   returns Blue Widgets, and default values:    ALL room_desc, date=Last Calendar Month
  55. //[19 item_category="Green Widgets" date='03/2011'] returns Green Widgets, for March, 2011  and default value:  ALL room_desc
  56.  
  57. add_shortcode( '19', 'room_current_select_location_number_of_racks' );
Advertisement
Add Comment
Please, Sign In to add comment