Can I determine if the ID is odd or even?

Results 1 to 8 of 8

Thread: Can I determine if the ID is odd or even?

  1. #1
    Fred Guest

    Default Can I determine if the ID is odd or even?

    I have this page to view records. All works fine. However, I cannot find how to determine if an ID # is odd/even. I want that to be known ahead of time. Sort of like<BR><BR>If id # is odd then<BR>Blah blah<BR>Else<BR>blah blah 2<BR>end if<BR><BR>Is that possible? What syntax is used to allow it to recognize ID #&#039;s that are odd?<BR><BR>Tx

  2. #2
    Darren Neimke Guest

    Default RE: Can I determine if the ID is odd or even?

    You can use the MOD operator to return the remainder from a division operation.<BR><BR>Therefore you could do something like...<BR><BR> If intYourNumber Mod 2 = 0 Then<BR><BR> &#039;The number is even<BR> <BR> Else<BR><BR> &#039; The number is odd<BR><BR> End If<BR><BR><BR>Here&#039;s a link to the MS documentation for MOD...<BR><BR><BR><BR>HTH<BR><BR>Darren<BR>[ ]

  3. #3
    Fred Guest

    Default RE: Can I determine if the ID is odd or even?

    Sorry, I am confused. How would it know it&#039;s odd or even by dividing? If I did the follwing:<BR><BR>Dim Id <BR>Id = Recordset.Fields("ID")<BR><BR>And then displayed it such as:<BR>response.write(" THE ID IS : " & id & "")<BR><BR>The user sees the id is like 32. The var id is the id #. I just want to know if it&#039;s possible somehow for any syntax I never learned to say "Yes, the id is even, &#039;cause it&#039;s 32"<BR><BR>By dividing, how would that come about? It still has to know what is odd in the first place. I will check it out, I just had to ask, this is something I&#039;m eager to learn for the first time.<BR><BR>See, I&#039;ve worked with loops. I can print out from 1-any # the odd #&#039;s. Great, I just want ID to be recognized immediately AS odd or even. Is mod the only way? If so, what does dividing have to do with it? Isn&#039;t it going to be recognized as odd/even after? If so, how? I couldn&#039;t find an explanation as to why and I figured I&#039;ll ask before I dig.<BR><BR>I must be missing the logic. I can determine if it&#039;s greater than 20 with one line of code. I can&#039;t do that to tell if it&#039;s even with one line?<BR><BR>(Not to be rude, just learning this)<BR><BR>Thank you

  4. #4
    Fred Guest

    Default Got it, one last thing...

    Hey, thanks man. I tested it. I didn&#039;t see the mod 2, just mod and divide and add and subtract and multiply.<BR><BR>Do I know WHY it works? No. I&#039;d love to. But I&#039;ll try to find out and not just take your time for granted. Thank you VERY much.<BR><BR>Now intNumber can be the ID. :)<BR><BR>Terrific. Thanks. I&#039;ll try to figure out WHY that works or HOW. This is very exciting to learn.<BR><BR>Fred

  5. #5
    FRED Guest

    Default DARREN>>SORRY, One last question if I ma

    Hey, you know that code is great. I use ID which is the id<BR>to be that var you used.<BR><BR>However, I am using a loop to display records which displays the name and I can have it display the id as well.<BR><BR>You know the if/then part of the mod 2 ex. you used?<BR><BR>Well, would it be possible for me to use it to display a table bg that is different for odd and even? THe loop I use to select records is:<BR><BR>If Not RecordSet.BOF Then<BR>RecordSet.MoveFirst<BR><BR>&#039;-- Loop through records until we are at the end<BR><BR>Do Until RecordSet.EOF<BR><BR>&#039;-- Display the fields<BR><BR><BR>Response.Write("&#060;tr&#062;" & Chr(13))<BR>Response.Write(" &#060;td&#062;" & RecordSet.Fields("name") & "&#060;td&#062;" & Chr(13))<BR>Response.Write(" &#060;td&#062;" & RecordSet.Fields("name") & "&#060;td&#062;" & Chr(13))<BR>Response.Write("&#060;/tr&#062;" & Chr(13))<BR><BR>RecordSet.MoveNext<BR>Loop<BR><BR> End If<BR><BR>See that loop? If there are 20 records, it displays all 20. The goal was to do if/then whereas if the ID is odd, the table display is Black (I have that code) and if even, Blue (have that)<BR><BR>The key is *wher* in this loop can I use YOUR code? I tried ALL areas, and it only shows ID #1.<BR><BR>The code you showed DOES determine odd/even. Terrific. But in that loop, I want odd records to have the table show black background (I have that code, just &#060;td bgcolor="000000"&#062;<BR><BR>And for blue too, for those even records.<BR><BR>But I can&#039;t get that code YOU provided IN that loop.<BR><BR>How can one do that? Just thought I&#039;d ask.<BR><BR>Thanks<BR>

  6. #6
    FRED Guest

    Default GOT IT :)

    THanks for the help on MOD 2<BR><BR>The goal was to have all odd records display in one table color, the even in another. I did it. Thanks to you with that mod 2 which even then I couldn&#039;t find in the documentation, though it&#039;s late and I&#039;m about to collapse. Thanks again<BR><BR>Fred

  7. #7
    Join Date
    Dec 1969

    Default RE: Can I determine if the ID is odd or even?

    Hey Fred, from the sound of your reply, you don&#039;t have much programming background. MOD is an operator to find out the remainder of a division. There is a MOD operator, no MOD 2. What he&#039;s trying to tell you is use your "ID" mod 2 and test the result. what it means is ID divides by 2 and see what the remainder of that division is. If it&#039;s 0 then it&#039;s even and if it&#039;s other than 0, it&#039;s logically telling you it&#039;s odd. Test it ur self, say ID is 5, 5 divides by 2, the remainder is 1, 11 divides by 2, it also has the remainder of 1, finally 4 divides by 2, waa la.. the remainder is 0 so it&#039;s even. get it???

  8. #8
    Leto Guest

    Default Assumption

    For the background colour, you are assuming that the ID number is always going to follow the odd, even, odd, even, etc pattern. But what happens if you have (eg) 1, 2, 3, 5, 6 ? You will get two black rows together :)<BR><BR>How I usually do this is will a temporary counter. Something like this:<BR><BR>---------------------<BR>bgC = "#000000"<BR>Start Loop<BR> response.write "&#060;tr bgcolor=""" & bgC & """&#062;"<BR> response.write "&#060;td&#062;Blah&#060;/td&#062;"<BR> response.write "&#060;/tr&#062;"<BR><BR> If bgC = "#000000" Then bgC = "#0000cc" Else bgC = "#000000"<BR><BR>RS.MoveNext<BR>--------------------------<BR><BR>This way you cycle through the two colours, but it is not reliant on the ID number.<BR><BR>-- Leto<BR>

Posting Permissions

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