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

后端 未结 5 424
孤独总比滥情好
孤独总比滥情好 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:04

    If you're looking to do odd/even lists separately, then you can use the currentrow property of the query combined with the modulo operator (%) to work out if the row is odd or even:

    <cfloop query="topBands>
      <cfif topBands.currentRow % 2 = 1>
        <!--- do your odd number output here --->
      </cfif>
    </cfloop>
    <cfloop query="topBands>
      <cfif topBands.currentRow % 2 = 0>
        <!--- do your even number output here --->
      </cfif>
    </cfloop>
    
    0 讨论(0)
  • 2021-01-25 00:05

    I think these answers address your side-by-side part of your question but does not explain the "same image" issue. Their code is written correctly but does not explain the reason.

    Your code:

            <IMG src="logo/#top10MostRated.Logo#" 
                 alt="#top10MostRated.Name#" 
                 width="100%" height="100%"></IMG>
    


    ... would be fine if you were only inside a <cfloop query = "top10MostRated"> or <cfoutput query = "top10MostRated"> block. The reason is because inside these types of blocks CF is smart enough to know you want the data for the current row. It would be the same as:

            <IMG src="logo/#top10MostRated.Logo[top10MostRated.currentRow]#" 
                 alt="#top10MostRated.Name[top10MostRated.currentRow]#" 
                 width="100%" height="100%" />
    


    Because you're nesting the to/from cfloop inside a <cfoutput query = ""> block, you are getting unexpected results. Your existing code is always asking for the record provided by your outer loop. Hence you see the same image 5 times. (using any of the fine examples provided will help you get out of this) but, you can remove the query from your cfoutput and simply ask CF to show you the value for the correct row in your loop using your index (you set your index to "i") so the below would show you the image that corresponds to your loop.

            <IMG src="logo/#top10MostRated.Logo[i]#" 
                  alt="#top10MostRated.Name[i]#" 
                  width="100%" height="100%" />
    
    0 讨论(0)
  • 2021-01-25 00:12

    I'd do it a different way. The objective is to have records 1 and 2 side by side and I don't see that in @barnyr's answer.

    <cfoutput>
    <cfloop from="2" to="topbands.recordcount + 1" index = "i" step="2">
        #topbands.fieldname[i-1]#  
        <cfif i lte topbands.recordcount>
            #topbands.fieldname[i]# <br />
        </cfif>
    </cfloop>
    </cfoutput>
    
    0 讨论(0)
  • 2021-01-25 00:13

    Ben Nadel has a post exactly for this. Link here A breakdown for this is

    <cfloop query="top10MostRated">
        <cfif top10MostRated.CurrentRow MOD 2>
            <!--- Add to the "odd list" --->
        <cfelse>
            <!--- Add the record to the "even list" --->
        </cfif>
    </cfloop>
    

    Then you'll have 2 lists oddList and evenList. Then it's just a matter of displaying them.

    0 讨论(0)
  • 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:

    <cfoutput>
       <cfloop from="1" to="5" index="i">
          <div class="left">#groupedBands.odd[i].name#</div>
          <div class="right">#groupedBands.even[i].name#</div>
       </cfloop>
    </cfoutput>
    

    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

    0 讨论(0)
提交回复
热议问题