CMS Development

ben-duchy
トップ投稿者

Display number of rows, as a number

解決

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 いいね!
1件の承認済みベストアンサー
Ntbrown
解決策
投稿者

Display number of rows, as a number

解決

@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.

元の投稿で解決策を見る

6件の返信
ben-duchy
トップ投稿者

Display number of rows, as a number

解決

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
解決策
投稿者

Display number of rows, as a number

解決

@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
トップ投稿者

Display number of rows, as a number

解決

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 いいね!
ben-duchy
トップ投稿者

Display number of rows, as a number

解決

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 いいね!
ben-duchy
トップ投稿者

Display number of rows, as a number

解決

Hi @Ntbrown,

 

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

 

ok I'll try that, thanks

0 いいね!
Ntbrown
投稿者

Display number of rows, as a number

解決

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.