Template After Building File with Query

LPH

Well-known member
I'm finally revisiting an old project that was abandoned because my knowledge just isn't there. I've built a PHP page with the following salesQuery.

PHP:
/**
* Class TRN_XenProduct_Model_Sales
*/
class TRN_XenProduct_Model_Sales {

   /**
    * @param XenForo_ControllerPublic_Abstract $controller
    * @param XenForo_ControllerResponse_Abstract $response
    */
    public static function salesQuery(XenForo_ControllerPublic_Abstract $controller, XenForo_ControllerResponse_Abstract &$response)
    {

        $salesQuery = XenForo_Application::get('db')->fetchAll("
                SELECT license_id, license_price, purchase_date
                FROM `xenproduct_license`
                WHERE license_state = 'active' AND license_price != 0
                ORDER BY `purchase_date` ASC");

        $response->params['licenses'] = $salesQuery;

   }
}

A XenForo Page was created and the class and method information were added. Below is code placed in the template HTML .

Code:
2014 Sales<br /><br />
<xen:foreach loop="$licenses" value="$license" i="$i" count="$count">

   <xen:if is="{$license.purchase_date} >= 1388563200 AND {$license.purchase_date} <= 1420012800" >

      {$i}: {$license.license_id} - {xen:datetime $license.purchase_date, html} - {$license.license_price}
      <br />

   </xen:if>

</xen:foreach>

<br /><br /><br />
2015 Sales<br /><br />
<xen:foreach loop="$licenses" value="$license" i="$i" count="$count">

   <xen:if is="{$license.purchase_date} >= 1420099200 AND {$license.purchase_date} <= 1451548800" >

      {$i}: {$license.license_id} - {xen:datetime $license.purchase_date, html} - {$license.license_price}
      <br />

   </xen:if>

</xen:foreach>

<br /><br /><br />
2016 Sales<br /><br />
<xen:foreach loop="$licenses" value="$license" i="$i" count="$count">

   <xen:if is="{$license.purchase_date} >= 1451635200 AND {$license.purchase_date} <= 1483171200" >

      {$i}: {$license.license_id} - {xen:datetime $license.purchase_date, html} - {$license.license_price}
      <br />

   </xen:if>

</xen:foreach>

The above seems rather repetitive and there is probably a better way to grab the information.

Ultimately I'm going for a sales page: showing the daily sales, the weekly, the monthly, and annual sales.

Would it be best to build the loop in the PHP file and not in the template? Is it possible for the template to do the calculations?

Thank you for your time.
 
You could use xen:elseif.

Code:
 <xen:if is="{$license.purchase_date} >= 1420099200 AND {$license.purchase_date} <= 1451548800" > ....
    -- 2014 template code --
<xen:elseif is"{$license.purchase_date} >= 1451635200 AND {$license.purchase_date} <= 1483171200" > ...
    -- 2015 template code --
...

That way you only need one foreach loop.

You could even automate the year stuff using {$i}.
 
I'll try the else if to combine. I need to work through the use of the $i.

Thank you.
 
Back
Top Bottom