Skip to content

How To: Drive Smashing widgets with PHP

Toby Creek edited this page Oct 1, 2020 · 7 revisions

Overview

To leverage an existing code base or separate the dashboard from the data source using only an HTTP connection, it may be desirable to POST data to the widgets easily using PHP's curl() functions.

Setup

  1. Create a function library to allow POSTing data to the dashing widget.

This repository has a function library

This is an example Smashing job

  1. Edit the function library to insert the Smashing widget URL and the auth token.

  2. Use PHP's json_encode function to turn an array or object into a JSON string.

  3. Use the function library's send_event() function to POST data.

Examples:

  • Sending data to a list widget, including moreinfo.
$json=@array();
$json['items'][0]['label']="Monday";
$json['items'][0]['value']="8:00am - 6:00pm";
$json['items'][1]['label']="Tuesday";
$json['items'][1]['value']="8:30am - 4:30pm";
$json['moreinfo']="And then, beer!";
send_event('labhours', json_encode($json));
  • Sending the result of a database query to a list widget
$result=mysqli_query($db, "SELECT fruit AS label, SUM(fruit) AS value FROM fruitbystate GROUP BY fruit");
$json=@array();
$json['items']=mysqli_fetch_all($result, MYSQLI_ASSOC);
send_event('fruitbystate', json_encode($json));
Clone this wiki locally