We use cookies to make HubSpot's community a better place. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. To learn more, and to see a full list of cookies we use, check out our Cookie Policy (baked goods not included).
Mar 6, 2021 4:15 PM
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?
Solved! Go to Solution.
Mar 8, 2021 7:41 AM - edited Mar 8, 2021 7:43 AM
@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.
Mar 8, 2021 8:27 AM
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.
Mar 8, 2021 7:41 AM - edited Mar 8, 2021 7:43 AM
@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.
Mar 8, 2021 4:51 AM
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?
Mar 8, 2021 4:26 AM - edited Mar 8, 2021 4:45 AM
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>
Mar 7, 2021 3:03 AM
Hi @Ntbrown,
My apologies I forgot to specify which type of table... Yes I meant HubDB.
ok I'll try that, thanks
Mar 6, 2021 7:19 PM - edited Mar 6, 2021 7:19 PM
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.