XF 2.2 first add on to show result of a simple query

Speil

Member
Hi, I'm trying to write my first add on to implement some queries in my board.

So I used the cmd.php to create a new addon mediabasar/dealstats. Inside the dealstats folder there are two files, addon.json and stats.php.

stats.php looks as the following:
PHP:
<?php


namespace mediabasar\dealstats;
$db = \XF::db();

use XF\Mvc\Entity\Entity;

class extension
{

$totaldeals = $this->db()->fetchAll("
            SELECT count(listing_id) as anzahl
            FROM xf_z61_classifieds_listing
            WHERE listing_status like 'sold'
            GROUP BY listing_status");

$totaldealsanzahl = $totaldeals['anzahl'];  
}

?>

In the statistic widget template I inserted {$totaldealsanzahl} but nothing is shown. Also nothing when using {$totaldeals|anzahl}


As I said, it's my first try to create an addon. Am I on the right path or do I get something completly wrong?
Do I need somewhere to add that the addon is initiated by pageload or will this be done automatically every time because the add on is "installed"?


thx
 
A few things:
  • the group by clause in the sql query is redundant, since you've already limited the result set to a single group in the where clause
  • Your SQL returns an array of rows since you called fetchAll rather than fetchRow. Your following line will throw an error since it assumes to operate on a single row, rather than on a set of rows
  • File names should match class names and names and namespaces are case sensitive
  • Raw code inside classes is not valid php, you'll need to pack it into functions
  • Your files shouldn't have any raw code floating around outside of classes
  • Your extension isn't actually extending anything, so the file is just completely ignored. i suggest you work through the how to build an add-on guide to get a first sense of how to work with the system.
 
Top Bottom