cfquery only returning one database record per group category

Discussion in 'Programming' started by lespaul00, Dec 10, 2007.

  1. #1
    Hello,

    I have the following code that takes my database entries and displays them by grouptype:

    <cfparam name="form.CATEGORY_ID" default="">
    <cfparam name="form.GROUP_TYPE_ID" default="">
    <cfquery name="GROUPtype" datasource="MYDATASOURCE">
    SELECT dt.GROUP_TYPE, r.RECIPE_ID, r.RECIPE_NAME, r.CATEGORY_ID, r.LINK, r.IMAGE
    FROM   TBLRECIPES AS r INNER JOIN GROUPTYPE dt
    	ON r.GROUP_TYPE_ID = dt.GROUP_TYPE_ID
    	WHERE  1 = 1
    	<cfif val(form.CATEGORY_ID) GT 0>
    		AND	 r.CATEGORY_ID = <cfqueryparam value="#form.CATEGORY_ID#" cfsqltype="cf_sql_integer">
    	</cfif>
    	<cfif val(form.GROUP_TYPE_ID) GT 0>
    		AND	r.GROUP_TYPE_ID = <cfqueryparam value="#form.GROUP_TYPE_ID#" cfsqltype="cf_sql_integer">
    	</cfif>
    
    
    	ORDER BY r.GROUP_TYPE_ID
    </cfquery>
       Filter by: </p>
    <form method="post" action="Untitled-2.cfm">
          <p align="center">GROUP type:  
            <select name="GROUP_TYPE_ID">
              <option value="">All</option>
              <option value="1">1</option>
              <option value="2">2</option>
              <option value="3">3</option>
              </select> 
            GROUP type: 
              <select name="CATEGORY_ID">
                <option value="">All</option>
                <option value="1">1</option>
                <option value="2">2</option>
              </select>
              <input name="submit" type="submit" />
          </p>
        </form>
    <table width="90%" border="0.5" cellspacing="9" cellpadding="0">
    
    <cfoutput query="GROUPtype" group="GROUP_type">
       <tr>
       	 <th colspan="3" align="left"><h2><span class="style5">#GROUP_TYPE#</span></h2>
       	   </th>
       </tr>
       <tr>
    
       		<td valign="middle"><span class="style5"><a href="#GROUPtype.LINK#">#GROUPtype.RECIPE_NAME#</a></span></td>
            <td><span class="style5">dfsd</span><br></td>
    		<td valign="top"><div align="center"><span class="style5"><img src="images/small/#GROUPtype.IMAGE#"></span></div></td>
    	  	</tr>
    </table>
       </cfoutput>
    Code (markup):
    For some reason, it is only returning 1 record under each GROUP_TYPE listing. Why is this?
     
    lespaul00, Dec 10, 2007 IP
  2. cfStarlight

    cfStarlight Peon

    Messages:
    398
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You forgot the inner cfoutput tags so CF will only show one record for each value in the "grouped" field.

    This is untested

    
    <cfoutput query="GROUPtype" group="GROUP_type">
       <tr>
       	 <th colspan="3" align="left"><h2><span class="style5">#GROUP_TYPE#</span></h2>
       	   </th>
       </tr>
      [COLOR="Red"]<cfoutput>[/COLOR]
       <tr>
    
       		<td valign="middle"><span class="style5"><a href="#GROUPtype.LINK#">#GROUPtype.RECIPE_NAME#</a></span></td>
            <td><span class="style5">dfsd</span><br></td>
    		<td valign="top"><div align="center"><span class="style5"><img src="images/small/#GROUPtype.IMAGE#"></span></div></td>
    	  	</tr>
      [COLOR="Red"]</cfoutput>[/COLOR]
    
    </cfoutput>
    
    Code (markup):
     
    cfStarlight, Dec 10, 2007 IP
  3. lespaul00

    lespaul00 Peon

    Messages:
    283
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Great! It worked. Sorry, I know you explained this to me before.


    So it successfully shows a recipe name and an image to its right. For each recipe, a new row is created (tr).

    How can I have two recipes and images show on one row?

    Something like:

    <td width="30%">#recipe_name#</td>
    <td width="20%">#image#</td>
    <td width="30%">#recipe_name#</td>
    <td width="20%">#image#</td>
    Code (markup):
    I know this is incorrect - however I can't get it to display correctly. Instead, i've only been successful displaying a recipe and image twice in one row.
     
    lespaul00, Dec 10, 2007 IP
  4. cfStarlight

    cfStarlight Peon

    Messages:
    398
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If you don't care about empty cells, you can use MOD to start a new row every two images. This isn't tested, but something like this.

    
    ...
    <cfoutput query="GROUPtype" group="GROUP_type">
       <tr>
       	 <th colspan="3" align="left"><h2><span class="style5">#GROUP_TYPE#</span></h2>
       	   </th>
       </tr>
       <tr>
    	<cfset counter = 0>
    	<cfoutput>
    		<td width="30%">#recipe_name#</td>
    		<td width="20%">#image#</td>
    		<cfset counter = counter + 1>
    		<cfif counter MOD 2 EQ 0>
    			</tr><tr>
    		</cfif>
    	</cfoutput>
      </tr>
    </cfoutput>
    ...
    
    Code (markup):
     
    cfStarlight, Dec 10, 2007 IP