Advertisement
xah

Query class

xah
Sep 29th, 2020 (edited)
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.78 KB | None | 0 0
  1. <?php
  2. class Query{
  3.   public $select;
  4.   public $from;
  5.   public $where;
  6.   public $select_input = 'SELECT ';
  7.   public $join_input = '';
  8.   public $where_input = ' WHERE ';
  9.  
  10.   public $modified_cols = array('Location' => array('WarehouseLocation.Name AS Location', 'INNER JOIN WarehouseLocation ON Stockitem.Location = WarehouseLocation.Code'),
  11.                          'Product' => array('Product.Name AS Product', 'INNER JOIN Product ON Stockitem.Product = Product.Code'),
  12.                          'Manufacturer' => array('Manufacturer.Name AS Manufacturer', 'LEFT JOIN Manufacturer ON (Manufacturer = Manufacturer.Code)'),
  13.                          'Model' => array('ProductModel.Name AS Model', 'LEFT JOIN ProductModel ON (ProductModel.Code = StockItem.Model)'),
  14.                          'Customer' => array('Customer.Name as Customer', 'LEFT JOIN Customer ON (Customer.Code = StockItem.Customer)'),
  15.                          'Status' => array('Status.Name AS Status', 'INNER JOIN Status ON Stockitem.Status = Status.Code'),
  16.                          'Owner' => array('Owner.Name AS Owner', 'INNER JOIN Owner ON Stockitem.Owner = Owner.Code'),
  17.                          'Condition' => array('Condition.Name AS Condition', 'INNER JOIN Condition ON Stockitem.Condition = Condition.Code')
  18.                        );
  19.  
  20.   public function __construct($select, $from, $where){
  21.     $this->select = $select;
  22.     $this->from = $from;
  23.     $this->where = $where;
  24.   }
  25.  
  26.   public function try_this(){
  27.     foreach($this->select as $s){
  28.         if(isset($this->modified_cols[$s])){
  29.             $this->select_input .= $this->modified_cols[$s][0] . ',';
  30.             $this->join_input .= $this->modified_cols[$s][1] . ',';
  31.         }else{
  32.             $this->select_input .= $s . ','; }
  33.     }
  34.     $this->select_input = substr($this->select_input, 0, -1);
  35.     $this->join_input = substr($this->join_input, 0, -1);
  36.   }
  37.  
  38.   // public $count = sqlsrv_num_rows($this->output);
  39. }
  40.  
  41. $q = new Query(array('Product', 'Model', 'ItemCode'), 'StockItem', 'Owner.Code=69');
  42. // $q->query();
  43. // print_r($q->select);
  44. $q->try_this();
  45. echo $q->join_input;
  46.  
  47. /**  $query_item_input = 'SELECT ItemCode, WarehouseLocation.Name AS Location, AuditDate, Product.Name AS Product,
  48.                   Manufacturer.Name AS Manufacturer, ProductModel.Name AS Model, Customer.Name as Customer,
  49.                   Status.Name AS Status, PCSerialNo, DisplayCode, BuyPrice, AuditCharge, FreightCharge,
  50.                   MiscCharge, AvailableForSale, Owner.Name AS Owner, Condition.Name AS Condition
  51.  
  52.                   FROM StockItem
  53.  
  54.                   INNER JOIN Product ON Stockitem.Product = Product.Code
  55.                   INNER JOIN Owner ON Stockitem.Owner = Owner.Code
  56.                   INNER JOIN Status ON Stockitem.Status = Status.Code
  57.                   INNER JOIN WarehouseLocation ON Stockitem.Location = WarehouseLocation.Code
  58.                   INNER JOIN Condition ON Stockitem.Condition = Condition.Code
  59.                   LEFT JOIN Manufacturer ON (Manufacturer = Manufacturer.Code)
  60.                   LEFT JOIN ProductModel ON (ProductModel.Code = StockItem.Model)
  61.                   LEFT JOIN Customer ON (Customer.Code = StockItem.Customer)
  62.  
  63.                   WHERE Owner.Code = ' . intval($_SESSION['company_id']) . $date_str . $sale_str . ';';
  64.  
  65.   // Executes the query
  66.   $query_item_output = sqlsrv_query($conn, $query_item_input, array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));
  67.   $row_count = sqlsrv_num_rows($query_item_output);
  68.   $all_stock_arr = array();
  69.  
  70.   if ($query_item_output === false) {
  71.     die(formatErrors(sqlsrv_errors()));
  72.     echo 'Connection status: DOWN';}
  73.  
  74.   while( $row = sqlsrv_fetch_array( $query_item_output, SQLSRV_FETCH_ASSOC) ) {
  75.     $all_stock_arr[] = $row;}
  76.  
  77.   sqlsrv_free_stmt($query_item_output);
  78.   sqlsrv_close($conn);**/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement