Support » Fixing WordPress » How to simlify my code?

  • I have some PHP code I’m working on which works but I’m wondering if there is a way to simplify it. Also looking to render button on the front end to change the query from ALL to PRO or UPCOMING? Thanks for any help in advance.

    function get_production_history_play_playid($playid)
    	{
    
        $fmt_date = fn1('return $x == "0000-00-00" ? false : date("M j, Y", strtotime($x));');
    
    		// They have two tables, so we will need to merge the results
    		$results = array();
    
    		$count 	= ($this->input->get('count')) ? $this->input->get('count') : $this->max_results;
    		$offset = ($this->input->get('offset')) ? $this->input->get('offset') : 0;
    		$view = 'all';
    
    		if ($view == 'all') {
    		// ALL
    		$query = $this->db->query("SELECT distinct customer.customerid as customerid, organization, vcity, vstate, vcountry, firstdate, lastdate
    								   FROM customer, orders, venue
    								   WHERE orders.ordersid=venue.ordersid
    								   AND orders.playid=? AND orders.active=1
    								   AND customer.customerid=orders.customerid
    								   AND shipped != 0
    								   AND defunct != 1
    								   AND orders.lastdate != 0
    								   AND (ordertype != 'misc'
    								   AND ordertype != 'reading')
    								   AND hidepic!=1
    								   AND hideprod!=1
    								   AND num_perf>0
    								   ORDER BY firstdate desc
    								   LIMIT $offset,$count", $playid);
    		foreach ($query->result() as $row)
    		{
    			$results[] = array
    			(
            'id' => $row->customerid,
            'src' => 'customerid',
    				'organization' => $row->organization,
    				'city' => $row->vcity,
    				'state' => $row->vstate,
    				'country' => $row->vcountry,
    				'firstdate' => call_user_func($fmt_date, $row->firstdate),
    				'lastdate' => call_user_func($fmt_date, $row->lastdate)
    			);
    		}
    
    		// Legacy data from oldprods
    		$query = $this->db->query("SELECT oldprodsid, organization, city, state, country, firstdate, lastdate
    								   FROM oldprods where playid=?
    								   ORDER BY firstdate desc
    								   LIMIT $offset,$count",$playid);
    		foreach ($query->result() as $row)
    		{
    			$results[] = array
    			(
            'id' => $row->oldprodsid,
            'src' => 'oldprods',
    				'organization' => $row->organization,
    				'city' => $row->city,
    				'state' => $row->state,
    				'country' => $row->country,
    				'firstdate' => call_user_func($fmt_date, $row->firstdate),
    				'lastdate' => call_user_func($fmt_date, $row->lastdate)
    			);
    		}
    
    		// IF UPCOMING
    		}else if ($view == 'upcoming') {
    		$extra_condition = "AND lastdate >= DATE_SUB(CURRENT_DATE,INTERVAL 0 DAY)"; //This line of code designates this as UPCOMING but omit the Legacy Query
    		$query = $this->db->query("SELECT distinct customer.customerid as customerid, organization, vcity, vstate, vcountry, firstdate, lastdate
    								   FROM customer, orders, venue
    								   WHERE orders.ordersid=venue.ordersid
    								   AND orders.playid=? AND orders.active=1
    								   AND customer.customerid=orders.customerid
    								   AND shipped != 0
    								   AND defunct != 1
    								   AND orders.lastdate != 0
    								   AND (ordertype != 'misc'
    								   AND ordertype != 'reading')
    								   AND hidepic!=1
    								   AND hideprod!=1
    								   AND num_perf>0
    								   $extra_condition
    								   ORDER BY firstdate desc
    								   LIMIT $offset,$count", $playid);
    		foreach ($query->result() as $row)
    		{
    			$results[] = array
    			(
            'id' => $row->customerid,
            'src' => 'customerid',
    				'organization' => $row->organization,
    				'city' => $row->vcity,
    				'state' => $row->vstate,
    				'country' => $row->vcountry,
    				'firstdate' => call_user_func($fmt_date, $row->firstdate),
    				'lastdate' => call_user_func($fmt_date, $row->lastdate)
    			);
    		}
    
    		// IF PRO
    		}else if ($view == 'pro') {
    		$extra_condition = "AND (ordertype = 'pro')"; //This line of code designates this as PRO along with the Legacy Query
    		$query = $this->db->query("SELECT distinct customer.customerid as customerid, organization, vcity, vstate, vcountry, firstdate, lastdate
    								   FROM customer, orders, venue
    								   WHERE orders.ordersid=venue.ordersid
    								   AND orders.playid=? AND orders.active=1
    								   AND customer.customerid=orders.customerid
    								   AND shipped != 0
    								   AND defunct != 1
    								   AND orders.lastdate != 0
    								   AND (ordertype != 'misc'
    								   AND ordertype != 'reading')
    								   AND hidepic!=1
    								   AND hideprod!=1
    								   AND num_perf>0
    								   $extra_condition
    								   ORDER BY firstdate desc
    								   LIMIT $offset,$count", $playid);
    		foreach ($query->result() as $row)
    		{
    			$results[] = array
    			(
            'id' => $row->customerid,
            'src' => 'customerid',
    				'organization' => $row->organization,
    				'city' => $row->vcity,
    				'state' => $row->vstate,
    				'country' => $row->vcountry,
    				'firstdate' => call_user_func($fmt_date, $row->firstdate),
    				'lastdate' => call_user_func($fmt_date, $row->lastdate)
    			);
    		}
    
    		// Legacy data from oldprods
    		$query = $this->db->query("SELECT oldprodsid, organization, city, state, country, firstdate, lastdate
    								   FROM oldprods where playid=?
    								   ORDER BY firstdate desc
    								   LIMIT $offset,$count",$playid);
    		foreach ($query->result() as $row)
    		{
    			$results[] = array
    			(
            'id' => $row->oldprodsid,
            'src' => 'oldprods',
    				'organization' => $row->organization,
    				'city' => $row->city,
    				'state' => $row->state,
    				'country' => $row->country,
    				'firstdate' => call_user_func($fmt_date, $row->firstdate),
    				'lastdate' => call_user_func($fmt_date, $row->lastdate)
    			);
    		}
    
    		}
    		return array_slice($results, 0, $count);
    	}

  • The topic ‘How to simlify my code?’ is closed to new replies.