INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

How to read string of 0s and 1s

How to read string of 0s and 1s

(OP)
I have a table of kids(5) and which of the 6 fruits they bring:
Table: KidsData
ID Name FruitData
1 Candy 100000
2 Andy 001000
3 Sarah 001010
4 Gabby NULL
5 Jack 000110
Table: Fruitdescription
ID FruitName
1 apple
2 orange
3 kiwi
4 banana
5 pineapple
6 grape

<cfquery name="kidsData">
SELECT *
FROM KidsData
WHERE FruitData IS NOT NULL AND ID = #ID#
</cfquery>

<cfoutput query="kidsData">
<cfif #left(#FruitData#, 1)# EQ '1'>
Apple
</cfif>
<cfif #left(#FruitData#, 2)# EQ '1'>
Orange
</cfif>
<cfif #left(#FruitData#, 3)# EQ '1'>
Kiwi
</cfif>
<cfif #left(#FruitData#, 4)# EQ '1'>
Banana
</cfif>
<cfif #left(#FruitData#, 5)# EQ '1'>
Pineapple
</cfif>
<cfif #left(#FruitData#, 6)# EQ '1'>
Grape
</cfif>: #Name#<br />
</cfoutput>

The code works fine if there is only 1 fruit in #FruitData# string, like
Apple: Candy
Kiwi: Andy

But for the #FruitData# string that has multiple 1s only shows the first record that it hits for 1, like
Kiwi: Sarah
Banana: Jack

it is not showing the output as needed:
Kiwi: Sarah
Pineapple: Sarah
Banana: Jack
Pineapple: Jack

I don't know where I'm wrong. Any help is appreciated.

RE: How to read string of 0s and 1s

You need to study the Mid() function.
<cfset fruitList=''>
<cfif Mid(kidsData.FruidData,1,1) eq '1'>
<cfset fruitList=ListAppend(fruitList,'apple')>
</cfif>
<cfif Mid(kidsData.FruidData,2,1) eq '1'>
<cfset fruitList=ListAppend(fruitList,'orange')>
</cfif>
...
<cfdump var=#fruitList# />

Lyndon

RE: How to read string of 0s and 1s

(OP)
Thanks Lyndon. that did help me. I have one more question, might be incorrect for CF thread since it is SQL query question. How can I Join on KidsData and Fruitdescription table to get Fruit Name from Fruitdescription table rather than hard coding it.
this is what I can get to this far.
<cfquery name="kidsData">
SELECT *
FROM KidsData K
LEFT JOIN Fruitdescription F ON F.FruitData = K.ID
WHERE FruitData IS NOT NULL AND ID = #ID#
</cfquery>

Table: KidsData
ID Name FruitData
1 Candy 100000
2 Andy 001000
3 Sarah 001010
4 Gabby NULL
5 Jack 000110
Table: Fruitdescription
ID FruitName
0 apple
1 orange
2 kiwi
3 banana
4 pineapple
5 grape

RE: How to read string of 0s and 1s

You should go to a SQL forum because:
A. The solution depends on the database you are using.
B. This dataset needs to be normalized.
SQL Forum List

Lyndon

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close