-2

Need favor regarding this matter. I'm trying to display checkbox by retrieving it from db query. Currently I'm able to display all the data into checkbox from db query. My problem is how can i select only specific data (not all) from db query to display on checkbox.

Below are my code to display all data into checkbox.

<!--- to get data  --->
<cfquery name="getData" datasource="#menu#">
  select namefood,price
  from menu_detail
  where foodname = <cfqueryparam cfsqltype="cf_sql_varchar" 
  value="#foodname#">   
  order by creation asc
</cfquery>
<!--- end --->

  <!--- To display checkbox --->
  <cfif #getdata.recordcount# neq 0>
         <div id="Layer1" style="position:absolute; left:477px; top:7px; width:95px; height:24px; z-index:1">Menu list:</div> 
         <div id="Layer1" style="position:absolute; left:479px; top:22px; width:220px; height:122px; z-index:1">
           <cfloop query="getData">
                   <input type="checkbox" class="cssNormal" name="cbox" >&nbsp;&nbsp;#namefood#<br>
            </cfloop>
         </div>  
    </cfif> 
ANS
  • 3
  • 2
  • Where is the information stored about the items to be marked? – Ilya Yaremchuk Sep 03 '18 at 06:38
  • the information stored inside the database – ANS Sep 03 '18 at 06:50
  • 1
    @ANS he wanted to see the table format of your database. – Leandro Keen Zapa Sep 03 '18 at 06:52
  • @ANS you might be looking for https://stackoverflow.com/questions/1212554/can-i-get-a-query-row-by-index-in-coldfusion . – Leandro Keen Zapa Sep 03 '18 at 06:53
  • We don't know what you mean by "specific data". What does #getData# contain ie ``? Which of those records do you want to display, and why? Also, SQL syntax can vary by vendor, so please include your DBMS (SQL Server, MySQL, etc..) with any SQL questions. – SOS Sep 03 '18 at 07:49
  • @Ageax sorry.What I mean by specific data is the specific record or row in the column namefood. Example of record are pasta, burger, risotto and etc. Some of the records I want to display into checkbox. – ANS Sep 03 '18 at 08:06
  • @ANS Add an attribute `checked` to the checkbox if the value of `namefood` qualifies the criteria. – Abhishekh Gupta Sep 03 '18 at 08:31
  • @ANS - So you want to display a checkbox for all of them, but some of the boxes should be pre-checked, correct? How do you determine which ones should be checked? Is that information stored in another database table? – SOS Sep 03 '18 at 12:57
  • *"How do you determine which ones should be checked?. Is that information stored in another database table?*" Remember we can't see anything about your application code/database/variables! ;-) You need to provide more specifics for us to provide anything more than "general" suggestions. If you're new to S.O. may want to read [How to Ask a Good Question](https://stackoverflow.com/help/how-to-ask) and [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) for tips. – SOS Sep 03 '18 at 14:49
  • @Ageax I just want to display only some of them in a checkbox. Not all. I will try to provide more specific then. – ANS Sep 04 '18 at 00:11
  • My nitpick, code review note: In ``, you don't need the "#"s. And you can also shorten the logic to just ``. – Shawn Sep 04 '18 at 13:52

1 Answers1

3

Very little information from you (

For example:

.....

<cfif #getdata.recordcount# neq 0>
         <div id="Layer1" style="position:absolute; left:477px; top:7px; width:95px; height:24px; z-index:1">Menu list:</div> 
         <div id="Layer1" style="position:absolute; left:479px; top:22px; width:220px; height:122px; z-index:1">
           <cfloop query="getData">
                   <input type="checkbox" class="cssNormal" name="cbox" <cfif namefood EQ 'french fries'>checked="checked"</cfif> >&nbsp;&nbsp;#namefood#<br>
            </cfloop>
         </div>  
    </cfif>

Added <cfif namefood EQ 'french fries'>checked="checked"</cfif> in <input type="checkbox" ...> where the variable namefood is checked and the condition

Ilya Yaremchuk
  • 2,007
  • 2
  • 19
  • 36
  • 1
    Adding a brief explanation about *how* the code supplied works will help other beginners with the same question. – SOS Sep 03 '18 at 14:45