Link to home
Start Free TrialLog in
Avatar of hibbsusan
hibbsusan

asked on

optimizing php postgresql code

I have some code that pulls out orders, then finds their line items, then pulls the statuses of each workstation the line item has been through. I know that this question is extremely unspecific, but I wonder if an expert can take a look and tell me how it might be optimized. maybe through the use of OOP or some other technique that's above my head.

Thank you so much for looking!

$query = 
"SELECT * FROM sales_orders 
WHERE customer_id  = (SELECT id FROM customers where customer ILIKE '".$user."') 
AND order_description ilike 
'%".implode("%' AND order_description ilike '%", $search)."%'
 OR order_number ilike 
'%".implode("%' AND order_description ilike '%", $search)."%'
ORDER BY order_number ASC";
//print_r($query);

$result = pg_query( $con , $query );






if (!$result)
{
	die( pg_last_error($con) );
	}
	else
	{ 
	$arr = array(); // create an empty array
	
		$count = 0;
		while ($row = pg_fetch_array($result)) 
		{
			
			$arr[] = array( 
			"id"                   => $row['id'],
			"status"               => $row['status'],
			"customer_id"          => $row['customer_id'],
			"shipping_address_id"  => $row['shipping_address_id'],
			"order_number"         => $row['order_number'], 
			"reference"            => $row['reference'],
			"ship_via_id"          => $row['ship_via_id'],
			"order_date"           => $row['order_date'],
			"due_date"             => $row['due_date'],
			"created"              => $row['created'],
			"sales_tax"            => $row['sales_tax'],
			"freight"              => $row['freight'],
			"misc"                 => $row['misc'],
			"taxable"              => $row['taxable'],
			"nontaxable"           => $row['nontaxable'],
			"job_name"             => $row['job_name'],
			"order_description"    => $row['order_description'],
			"ship_to_name"         => $row['ship_to_name'],
			"ship_to_address1"     => $row['ship_to_address1'],
			"ship_to_address2"     => $row['ship_to_address2'],
			"ship_to_city"         => $row['ship_to_city'],
			"ship_to_zipcode"      => $row['ship_to_zipcode'],
			"name"                 => $row['name'],
			"address1"             => $row['address1'],
			"address2"             => $row['address2'],
			"city"                 => $row['city'],
			"state"                => $row['state'],
			"zipcode"              => $row['zipcode'],
			"act_ship_date"        => $row['act_ship_date'],
			"ksisoldby"            => $row['ksisoldby'],
			);
			
			//$line_item_query = "SELECT * FROM sales_order_lines WHERE sales_order_id = '12317'";
			
			$sales_order_id   = $row['id'];
			$line_item_query  = "SELECT * FROM sales_order_lines WHERE sales_order_id = '$sales_order_id'";
			$line_item_result =  pg_query( $con , $line_item_query );
		
			//$line_item_array = array();
	
			$count2 = 0;
			while ($row1 = pg_fetch_array($line_item_result)) 
			{
				
				
				
				
				//put line item info into arrays
				$arr[$count][] = array( 
					"id"            => $row1['id'],
					"description"   => $row1['description'],
					"sell_price"    => $row1['sell_price'],
					"created"       => $row1['created'],
				);	
				
				
				$sales_order_line_id = $row1['id'];
				
				$line_item_status_query = 
				"SELECT p.description, p.production_order_id, l.start_user, p.sequence, l.start, l.stop 
				FROM production_order_processes p
				LEFT JOIN 
				production_order_process_log l ON 
				l.production_order_process_id = p.id WHERE l.production_order_id = 
				(SELECT id FROM production_orders WHERE sales_order_line_id = '".$sales_order_line_id."')";
			
				$line_item_status_result = pg_query( $con , $line_item_status_query );
	
	
				while ($row2 = pg_fetch_array($line_item_status_result)) 
				{
					$is_complete = NULL;
					if ( !empty($row2['stop']) )
					{
						$is_complete = TRUE;	
					}
					else
					{
						$is_complete = FALSE;		
					}
					
				$arr[$count][$count2][] =  array(
					"processing_started"   => TRUE,
					"line_item_id"         => $row1['id'],
					"description"          => $row2['description'],
					"production_order_id"  => $row2['production_order_id'],
					"work_center_id"       => $row2['work_center_id'],
					"sequence"             => $row2['sequence'],
					"checked_in"           => $row2['start'],
					"checked_out"          => $row2['stop'],
					"is_complete"           => $is_complete,
					
				);
			}
			$count2++;
		}
		$count ++;
	}		
}
			echo json_encode($arr);
?>

Open in new window


ps.  I'm extremely new to most of this stuff, so apologies upfront if i've done something offensively amateurish..
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

You might replace lines 30-60 with this:
$arr[] = $row;

I'll look at the rest of it in a few minutes.
Avatar of hibbsusan
hibbsusan

ASKER

great. thank you! :D
Yeah, it's basically copying one array to another and all the keys are the same so there is an easy shortcut.
Do the timestamps on our posts look odd to you?  They do to me!
Yeah, it's basically copying one array to another and all the keys are the same so there is an easy shortcut.

Can you explain this a bit more..

I'm not sure about the timestamps...
the new experts exchange is too beautiful; i can't find anything in all this whitespace... :D
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of earth man2
The code is merging results from different queries into a single result set...

Another way of achieving this is to use the database to left/right join the queries together...