Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

display of query data

Posted on 2006-11-01
3
Medium Priority
?
161 Views
Last Modified: 2013-12-24
Hello,

What I am looking to do is set up a program where I enter data into a database and have the data displayed in a table cell individually. I would like to be able to set the program to display so many records across before the program starts a new row. How can I do that using coldfusion?

Thanks,
Brandon
0
Comment
Question by:brandonpowell
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 7

Expert Comment

by:aseusainc
ID: 17854489
something like this:

<cfset howmanycols=5>
<table>
  <tr>
    <cfoutput query="yourquery">
      <td>#recorddata#</td>
      <cfif currentrow MOD howmanycols EQ 0 AND currentrow NEQ yourquery.recordcount>
        </tr><tr>
      </cfif>
    </cfoutput>
  </tr>
</table>
0
 
LVL 6

Expert Comment

by:javasharp
ID: 17855917
I will give you an example,suppose the query is like:

<cfquery name="myquery'>
select first_name,age,pin from mytable
</cfquery>

you can output the data like this :

<table>
    <cfloop query="myquery">
      <tr>
        <td>#first_name#</td>
         <td>#age#</td>
         <td>#pin#</td>
      </tr>
   </cfloop
</table>
0
 
LVL 20

Accepted Solution

by:
trailblazzyr55 earned 2000 total points
ID: 17931207
here's how you can control the display either displaying the records top to bottom or left to right

also you'll see this below in your column output

#myQuery.CarMake[queryItem]# <!--- format is: queryName.ColumnName[queryItem] --->

queryItem is really your query's currentrow, since we're modifying how you want things to display, we make a custom currentrow variable called "queryItem" so you don't need to change anything here, it'll adjust based on the display type you want and number of columns you want.

and the format for anything you want to display will always be: "queryName.ColumnName[queryItem]"

here's a visual of the display types....

top to bottom
----------------
1 5  9  13
2 6 10 14
3 7 11 15
4 8 12 16

left to right
---------------
1    2  3   4
5    6  7   8
9   10 11 12
13 14 15 16

<!--- Sample Query --->
<cfquery name="myQuery" datasource="YOUR_DSN">
SELECT CarMake FROM myCarsTable
</cfquery>

<!--- specify number of columns to show --->
<cfset colCount = 4>
<!--- TopToBottom or LeftToRight --->
<cfset displayType = "TopToBottom">

<cfset rowCount = ceiling(myQuery.recordcount/colCount)>
<cfset itemTotal = myQuery.recordcount>
<cfset queryItem = 0>

<cfoutput>
<table cellspacing="0" cellpadding="0" border="1">
      <cfloop from="1" to="#rowCount#" index="i">
      <cfif displayType is "TopToBottom">
            <cfset queryItem = i>
      </cfif>
      <tr>
            <cfloop from="1" to="#colCount#" index="ii">
            <cfswitch expression="#displayType#">
                  <cfcase value="TopToBottom">
                        <cfif ii gt 1>
                              <cfset queryItem = queryItem + colCount>
                        <cfelse>
                              <cfset queryItem = queryItem>
                        </cfif>
                  </cfcase>
                  <cfdefaultcase>
                        <cfset queryItem = queryItem + 1>
                  </cfdefaultcase>
            </cfswitch>
            <td>
                  <cfif queryItem gt itemTotal>&nbsp;
                  <cfelse>
                        #myQuery.CarMake[queryItem]# <!--- format is: queryName.ColumnName[queryItem] --->
                  </cfif>
            </td>
            </cfloop>
      </tr>
      </cfloop>
</table>
</cfoutput>

if you want to test a sample, here's an example query with the code above used to display this....

<cfset myCars = queryNew("ID,Make")>
<cfset addRow = queryAddRow(myCars,15)>
<cfset setRow = querySetCell(myCars,"ID",1,1)>
<cfset setRow = querySetCell(myCars,"Make","Honda",1)>
<cfset setRow = querySetCell(myCars,"ID",2,2)>
<cfset setRow = querySetCell(myCars,"Make","Toyota",2)>
<cfset setRow = querySetCell(myCars,"ID",3,3)>
<cfset setRow = querySetCell(myCars,"Make","Ford",3)>
<cfset setRow = querySetCell(myCars,"ID",4,4)>
<cfset setRow = querySetCell(myCars,"Make","Mitsubishi",4)>
<cfset setRow = querySetCell(myCars,"ID",5,5)>
<cfset setRow = querySetCell(myCars,"Make","Aston-Martin",5)>
<cfset setRow = querySetCell(myCars,"ID",6,6)>
<cfset setRow = querySetCell(myCars,"Make","Buick",6)>
<cfset setRow = querySetCell(myCars,"ID",7,7)>
<cfset setRow = querySetCell(myCars,"Make","Saleen",7)>
<cfset setRow = querySetCell(myCars,"ID",8,8)>
<cfset setRow = querySetCell(myCars,"Make","Mini",8)>
<cfset setRow = querySetCell(myCars,"ID",9,9)>
<cfset setRow = querySetCell(myCars,"Make","Suburu",9)>
<cfset setRow = querySetCell(myCars,"ID",10,10)>
<cfset setRow = querySetCell(myCars,"Make","Mazda",10)>
<cfset setRow = querySetCell(myCars,"ID",11,11)>
<cfset setRow = querySetCell(myCars,"Make","Chevy",11)>
<cfset setRow = querySetCell(myCars,"ID",12,12)>
<cfset setRow = querySetCell(myCars,"Make","Lincoln",12)>
<cfset setRow = querySetCell(myCars,"ID",13,13)>
<cfset setRow = querySetCell(myCars,"Make","Ferrari",13)>
<cfset setRow = querySetCell(myCars,"ID",14,14)>
<cfset setRow = querySetCell(myCars,"Make","Porsch",14)>
<cfset setRow = querySetCell(myCars,"ID",15,15)>
<cfset setRow = querySetCell(myCars,"Make","Audi",15)>

<cfquery name="myQuery" dbtype="query">
SELECT * FROM myCars
</cfquery>

<cfset colCount = 4>
<cfset rowCount = ceiling(myQuery.recordcount/colCount)>
<cfset itemTotal = myQuery.recordcount>
<cfset displayType = "TopToBottom"> <!--- TopToBottom or LeftToRight --->
<cfset queryItem = 0>

<cfoutput>
<table cellspacing="0" cellpadding="0" border="1">
      <cfloop from="1" to="#rowCount#" index="i">
      <cfif displayType is "TopToBottom">
            <cfset queryItem = i>
      </cfif>
      <tr>
            <cfloop from="1" to="#colCount#" index="ii">
            <cfswitch expression="#displayType#">
                  <cfcase value="TopToBottom">
                        <cfif ii gt 1>
                              <cfset queryItem = queryItem + colCount>
                        <cfelse>
                              <cfset queryItem = queryItem>
                        </cfif>
                  </cfcase>
                  <cfdefaultcase>
                        <cfset queryItem = queryItem + 1>
                  </cfdefaultcase>
            </cfswitch>
            <td>
                  <cfif queryItem gt itemTotal>&nbsp;
                  <cfelse>
                        #myQuery.Make[queryItem]#
                  </cfif>
            </td>
            </cfloop>
      </tr>
      </cfloop>
</table>
</cfoutput>




hope that helps,
~trail
0

Featured Post

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question