SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Feb 2008
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Displaying query results in 3 column table - within a cfloop

    Hello,

    I have the following code. In red is the area of interest. I would like the results to be displayed in 3 columns, rather than just 1:

    Code:
    <cfparam name="start" type="numeric" default="1">
    <cfparam name="step" type="numeric" default="10">
    <cfquery datasource="mydatabase" cachedwithin=".01" name="queryResults">
    select * 
    from tblrecipes
    order by recipe_name asc
    </cfquery>
    <cfif queryResults.recordcount gt 0>
    <cfoutput>
        <p>
        <!--- if past start --->
        <cfif (start-step-step) gt 1>
            <a href="#cgi.SCRIPT_NAME#?start=1"><img src="images/Beginning_blue.png" alt="Beginning" width="31" height="21" align="absbottom"></a>
        </cfif> 
        <cfif start gt 1>
            <a href="#cgi.SCRIPT_NAME#?start=#start-step#"><img src="images/previous_blue.png" alt="Previous" align="absbottom"></a>
        </cfif>
        <strong>#start# - #iif(start + step gt queryResults.recordcount,queryResults.recordcount,start + step-1)# of #queryResults.recordcount# records</strong>
        <!--- if still some not displayed --->
        <cfif (start + step) lte queryResults.recordcount>
            <a href="#cgi.SCRIPT_NAME#?start=#start+step#"><img src="images/next_blue.png" alt="Next" align="absbottom"></a>
        </cfif>
        <cfif (start+step+step) lte queryResults.recordcount>
            <a href="#cgi.SCRIPT_NAME#?start=#queryResults.recordcount-step+1#"><img src="images/end_blue.png" alt="End" align="absbottom"></a>
        </cfif> 
        </p>    
    </cfoutput>
    </cfif>
    <cfloop query="queryResults" startrow="#start#" endrow="#start + step-1#">
    
    
    <table width="100%" border="1" bordercolor="#FFFFFF" cellspacing="0" cellpadding="2">
    <tr>
    	<cfoutput>
    		<td width="33%">#queryResults.RECIPE_NAME#</td>
       		<cfif currentRow mod 3 eq 0>
    	  		</tr>
    		</cfif>	
    	</cfoutput>
    
    </table>
    
    </cfloop>
    It displays in one column.... How can I get the results on each page to display in a table with three columns?
    Find cocktails at Dr. Mixology, the new mixology community

  2. #2
    SitePoint Member
    Join Date
    Jul 2005
    Location
    Oklahoma
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could loop through this as many times as needed.

    Code:
    <tr>
       <cfoutput query="queryResults" startrow="#start#" maxrows="3">
            <td width="33&#37;">#queryResults.RECIPE_NAME#</td>
       </cfoutput>
       <cfset #start# = #start# + 3>
    </tr>

  3. #3
    SitePoint Member
    Join Date
    Feb 2008
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for the reply. It worked, and i'm almost there. I have the following:

    Code:
    <cfparam name="start" type="numeric" default="1">
    <cfparam name="step" type="numeric" default="3">
    <cfquery datasource="drmixology" cachedwithin=".01" name="queryResults">
    select * 
    from tblrecipes
    order by recipe_name asc
    </cfquery>
    <cfif queryResults.recordcount gt 0>
    <cfoutput>
        <p>
        <!--- if past start --->
        <cfif (start-step-step) gt 1>
            <a href="#cgi.SCRIPT_NAME#?start=1"><img src="images/Beginning_blue.png" alt="Beginning" width="31" height="21" align="absbottom"></a>
        </cfif> 
        <cfif start gt 1>
            <a href="#cgi.SCRIPT_NAME#?start=#start-step#"><img src="images/previous_blue.png" alt="Previous" align="absbottom"></a>
        </cfif>
        <strong>#start * 3# - #iif(start * 3 + step gt queryResults.recordcount,queryResults.recordcount,start * 3 + step-1)# of #queryResults.recordcount# records</strong>
        <!--- if still some not displayed --->
        <cfif (start + step) lte queryResults.recordcount>
            <a href="#cgi.SCRIPT_NAME#?start=#start+step#"><img src="images/next_blue.png" alt="Next" align="absbottom"></a>
        </cfif>
        <cfif (start+step+step) lte queryResults.recordcount>
            <a href="#cgi.SCRIPT_NAME#?start=#queryResults.recordcount-step+1#"><img src="images/end_blue.png" alt="End" align="absbottom"></a>
        </cfif> 
        </p>    
    </cfoutput>
    </cfif>
    <cfloop query="queryResults" startrow="#start#" endrow="#start + step-1#">
    
    
    <table width="100%" border="1" bordercolor="#FFFFFF" cellspacing="0" cellpadding="2">
    <tr>
       <cfoutput query="queryResults" startrow="#start#" maxrows="3">
            <td width="33%">#queryResults.RECIPE_NAME#</td>
       </cfoutput>
       <cfset #start# = #start# + 3>
    </tr>
    
    </table>
    
    </cfloop>
    I modified it to display 3 x 3 records (3 columns by 3 rows). But now, the page buttons display the wrong record numbers on the first page. It starts by labeling the records on the page as 3-5 rather than 1-9. Then when I click forward, it's 12-14. It increases by 9, which is correct, but the start page of 3-5 is wrong. How can I fix this?
    Find cocktails at Dr. Mixology, the new mixology community

  4. #4
    SitePoint Member
    Join Date
    Jul 2005
    Location
    Oklahoma
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you might need to change your "step" variable to 9 since you are displaying 9 records.

    I also noticed that you are multiplying #start# by 3 to display the starting record number. If you change the step value to 9, I think you can get rid of that "*3" and it should work.

  5. #5
    SitePoint Member
    Join Date
    Feb 2008
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the response. I found the problem. I set the step back to 9, but had to change the cfloop criteria as such:

    Code:
    <cfloop query="queryResults" startrow="#start#" endrow="#start + step/3-1#">
    Otherwise, the loop tries to create 9 rows with 3 columns each, rather than just 3.

    Thanks.
    Find cocktails at Dr. Mixology, the new mixology community


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •