CMS Development

ben-duchy
Top Contributor

Display number of rows, as a number

SOLVE

Does anyone know how to display the number of rows, as a number?

 

Imagine you have a table with 10 rows with a column called 'transport'. Each row will reference a particular type of transport, for example Train, Bus, Car, or Bike.

 

If for example 5 rows had 'car' selected, how do I display this as a number?  I assume it will be something along the lines of: if transport.type='car' display row_count

 

Am I getting close?

0 Upvotes
1 Accepted solution
Ntbrown
Solution
Contributor

Display number of rows, as a number

SOLVE

@ben-duchy  Was meant ot be starter code.... Have you tried debugging it....? Maybe print out the rows....

 

If it's a select there may be a different serialized representation such as a dictionary, list or something. I don't recall which naturally changes the access methdology.

 

I believe you can do: 

 

rows|selectattr("COLUMN_NAME_OF_SELECT.name", "equalto", "car")|length

 

Or, if that fails:

 

{% set rows = hubdb_table_rows("TABLE ID", "COLUMN_NAME=OPTION VALUE") %}

 

But, selectattr with a single query is preferred. If even that fails.... Great! As I said print it out, debug, and work with the data structure provided. If there's not a simple built in just iterate and build a counter. Whichever way.... there's certainly a way to get the count. Just figure out how to get there 🙂

 

There's also a much cleaner way to write what you have written.

View solution in original post

6 Replies 6
ben-duchy
Top Contributor

Display number of rows, as a number

SOLVE

Spot on @Ntbrown thanks 😀

I'm sure I tried the '.name' method between my last post and yours, but I must've had an error elsewhere.

Ntbrown
Solution
Contributor

Display number of rows, as a number

SOLVE

@ben-duchy  Was meant ot be starter code.... Have you tried debugging it....? Maybe print out the rows....

 

If it's a select there may be a different serialized representation such as a dictionary, list or something. I don't recall which naturally changes the access methdology.

 

I believe you can do: 

 

rows|selectattr("COLUMN_NAME_OF_SELECT.name", "equalto", "car")|length

 

Or, if that fails:

 

{% set rows = hubdb_table_rows("TABLE ID", "COLUMN_NAME=OPTION VALUE") %}

 

But, selectattr with a single query is preferred. If even that fails.... Great! As I said print it out, debug, and work with the data structure provided. If there's not a simple built in just iterate and build a counter. Whichever way.... there's certainly a way to get the count. Just figure out how to get there 🙂

 

There's also a much cleaner way to write what you have written.

ben-duchy
Top Contributor

Display number of rows, as a number

SOLVE

I've got it to work by changing the field type to a textbox.  What do I need to do with the above code to make it work for select list?

0 Upvotes
ben-duchy
Top Contributor

Display number of rows, as a number

SOLVE

Unfortunately I couldnt get it to work. @Kevin-C  any thoughts?

 

This is what I have so far but the results just display 0

 

{% set rows = hubdb_table_rows(1234567) %}

{% set num_car = rows | selectattr ("transport_type","equalto","Car") | length %}
{% set num_train = rows | selectattr ("transport_type","equalto","Train") | length %}
{% set num_bus = rows | selectattr ("transport_type","equalto","Bus") | length %}
{% set num_bike = rows | selectattr ("transport_type","equalto","Bike") | length %}

 

<table>
<tr><td>Number commuted by car:</td><td>{{ num_car }}</td></tr>
<tr><td>Number commuted by Train:</td><td>{{ num_train }}</td></tr>
<tr><td>Number commuted by Bus:</td><td>{{ num_bus }}</td></tr>
<tr><td>Number commuted by Bike:</td><td>{{ num_bike }}</td></tr>
</table>

0 Upvotes
ben-duchy
Top Contributor

Display number of rows, as a number

SOLVE

Hi @Ntbrown,

 

My apologies I forgot to specify which type of table... Yes I meant HubDB.

 

ok I'll try that, thanks

0 Upvotes
Ntbrown
Contributor

Display number of rows, as a number

SOLVE

Entirely dependent on what you mean by "table". Is this HubDB or a custom table via the module? Either way... Just coallesce the results and display the sum.  Your post is vague, but I'm assuming you want something like this?

 

 

{# rows = module.rows or some other accessor if a module... or rows = hubdb_table_rows(tableId, other parameters) #}
{% set rows = pick a referencing methodology.... #}
{# count = rows|selectattr("column name in HubDB table or field in module group", "equalto", "mode of transportation")|length . So maybe.... #}
{% set numCars = rows|selectattr("mode_of_transportation", "equalto", "car")|length %}

 

 

You can fill in the gaps... Or, add further detail.