ColdFusion: Can you pull out a unique record from a query using recordCount?

后端 未结 5 474
孤独总比滥情好
孤独总比滥情好 2021-01-24 23:38

It\'s a bit of tricky question, however, my page for most rated bands displays the band logos in order of how high they have been rated. My only problem is i want to count throu

5条回答
  •  日久生厌
    2021-01-25 00:15

    It sounds like what you'd like to get is a collection of even-numbered records and a collection of odd-numbered records. In Coldfusion 10 or Railo 4, you can use groupBy() from Underscore.cfc to split up your query result into manageable sub-sets, like so:

    _ = new Underscore();// instantiate the library
    groupedBands = _.groupBy(topBands, function (val, index) {
       return index % 2 ? "odd" : "even";
    }); 
    

    This returns a struct with two elements odd and even, each containing an array of records which are odd or even. Example result:

    {
       odd: [{name: "Band one"}, {name: "Band three"}],
       even: [{name: "Band two"}, {name: "Band four"}]
    }
    

    Splitting your results into logical sub-sets makes the code more readable:

    
       
          
    #groupedBands.odd[i].name#
    #groupedBands.even[i].name#

    You'll also be able to use those sub-sets in other places on your page if you need to.

    Note: I wrote Underscore.cfc

提交回复
热议问题