1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Take this code and incorporate into a page

Discussion in 'XenForo Development Discussions' started by LPH, Apr 1, 2015.

  1. LPH

    LPH Well-Known Member

    I have a working PHP script that would be better if the query printed to a XenForo page instead of outside XenForo. My brain has been numb for several days and so I'm chasing my tail instead of running straight. I could use a little direction because none of my code over the past week has worked (numerous projects).

    I'd love a little success tonight. :D

    Option 1: Create an XF page, add the query into a class, call that class
    Option 2: Sigh and Whine. Pause. Figure out how to build this as a report form in the admin area of XenForo.
    Option 3: Just keep being miserable; It's an art form worthy of simple minded people like myself.

    PHP:
    <?php
    /**
    * XenProduct Sales Report
    */

    $startTime microtime(true);

    // Set to forum location - See XenForo index.php for information
    $fileDir '/path/to/community';

    require(
    $fileDir '/library/XenForo/Autoloader.php');
    XenForo_Autoloader::getInstance()->setupAutoloader($fileDir '/library');

    XenForo_Application::initialize($fileDir '/library'$fileDir);
    XenForo_Application::set('page_start_time'$startTime);

    // Report Query

    $report_qry "
                    SELECT license_id, license_price, purchase_date FROM `xenproduct_license`
                    WHERE license_state = 'active' AND license_price != 0
                    ORDER BY `purchase_date` ASC
                    "
    ;

    $licenses XenForo_Application::get'db' )->fetchAll$report_qry );

    echo 
    '<h2>Total Active Licenses: ' count$licenses ) . '</h2>';

    // Change to desired year
    $year 15;

    // Change to desired month
    $month 3;

    $sum 0;

    foreach ( 
    $licenses as $license ) {

        if ( 
    date("y"$license['purchase_date'] ) == $year && date("m"$license['purchase_date'] ) == $month )  {

            
    $sum += $license['license_price'];

            echo 
    'License ID: '
                 
    $license['license_id']
                 . 
    ' Purchase Date: '
                 
    gmdate"m-d-y"$license['purchase_date'] )
                 . 
    ' Price: '
                 
    $license['license_price']
                 . 
    '<br />';
        }
    }

    echo 
    'The total sales $' number_format$sum);
    If you aren't sure how to kickstart me on this little project, maybe you can at least figure out why the array_sum is wrong Update ... changed to $sum . :D

    Also -- I suspect there is a much simpler way to get this information rather than using direct queries. Alas, XenProduct_Model_ is something that I'm still trying to figure out but a direct queries works for now.

    Maybe it's time to go for a walk.
     
    Last edited: Apr 1, 2015
  2. LPH

    LPH Well-Known Member

    Decided to go with a page. It seemed easier.

    Created the following class:

    PHP:
    <?php
    /**
    * XenProduct Sales Report
    */

    class TRN_XenProduct_Model_Sales {

        public static function 
    salesQuery(XenForo_ControllerPublic_Abstract $controllerXenForo_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;

        }
    }

    2. Created page with proper callbacks and the following template

    PHP:
    <xen:foreach loop="$licensesvalue="$license">
    {
    $license.license_id} - {$license.purchase_date}
    <
    br />
    </
    xen:foreach>
    This loop is very basic and doesn't do all the counts, summations etc I have in the other code but it's a start. The timestamp isn't converted either. Guess I get to learn how to do all the template things now.

    Any suggestions would be helpful.

    :D
     

Share This Page