Jump to content

how to access custom table via template


Rob(AU)
 Share

Recommended Posts

I am working on a personal project and can't figure this out.
The aim is to feed data from a microprocessor into a table in the PW database and then be able to read and display it via the frontend.
So far the data is being added to the table, and I can display it via a php file that is not part of PW.

The table is called SensorData and includes fields for id, temperature, humidity, dewpoint and reading_time.

From what I understand the code below should get the last reading from the table.

$lastReading = $this->db->query("SELECT id, sensor, location, temperature, humidity, dewpoint, reading_time FROM SensorData order by reading_time desc limit 1");

How do I display then display that on a page because the data is probably not available to any template?

Edited by Rob(AU)
refining question
Link to comment
Share on other sites

  • 2 weeks later...

I have been exploring the link given by @Gideon So which gives a sparse conceptual overview. I can see how to get a PDOStatement object, but my question remains - how to get from there to data that can be used via a template.

Can anyone give a brief example?

Thanks

Rob

Link to comment
Share on other sites

Using straight database methods/functions to retrieve data would, unless you process the results differently, just be standard PHP / MySQL requests. You can use the API docs about $database, and take advantage of it, but it's just a ProcessWire way of querying a separate database. The resultant data would still be just that -- data. How you then further process that data and then display it in a template is up to you. Most of what is rendered in a template is up to you, as a developer, anyway.

If you're looking for a more ProcessWire-like way of referring to data when using the WireDatabasePDO class (ex: $page->MyCustomTable->row->first('property')), you won't get it, that's just a helper class to standard PDO methods within PHP. Something like kixe's FieldtypeSelectExternalOption might provide you with a PW-like feeling, though I don't know if it's entirely necessary. It all depends on what you want to do with the data from the table, and how you want to interact with it, if at all.

If you want to be able to edit it in the admin, and/or create pages from it, then it's not quite as simple, and I'm not sure you'd want to. You certainly can, however.

So with the separate database, what is it you'd like to be able to do to, or with, it? Simply display it?

Here's another good discussion on this topic, also in the forum...

  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...