|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Odd and even
Hey. I need to be able do difference between odd and even id numbers (from a db) whats the php code fore this ?
|
|
#2
|
||||
|
||||
|
If you're doing your ids as auto_increments, the difference will always be either one or zero. If that doesn't help, you'll have to be a little more specific about what you want to know.
__________________
Please don't PM me asking for solutions outside the scope of a thread. Keeping all responses in a thread stands to help others who come along later, which is after all what this forum's all about. |
|
#3
|
|||
|
|||
|
specified
Yes, the id is auto increased. But i need to be able to sort by od or even id numer. ei. odd id goes on the right, even id goes on the left. is there a function, that reconises whether the id is odd or even ?
|
|
#4
|
|||
|
|||
|
I had a kind of solution to this.
Code:
<?php $test_result = $test_num / 2; if(is_int($test_result)) echo "This number is even"; else echo "This number is odd"; ?> But of course Data Type Int has restrictions. Oh yeah by the way, this is a cheap solution. ![]() |
|
#5
|
|||
|
|||
|
<TABLE BORDER=1>
<TR> <TH>ODD</TH> <TH>EVEN</TH> </TR> <? $odd_counter = 0; $even_counter = 0; while ($id_array=mysql_fetch_array($result)) { // *** even number *** // if ($id_array['id'] % 2 == 0) { $even_array[$even_counter] = $id_array['id']; $even_counter++; } // *** odd number *** // else { $odd_array[$odd_counter] = $id_array['id']; $odd_counter++; } } // *** get larger counter variable *** // $counter = ($even_counter > $odd_counter) ? $even_counter : $odd_counter; // *** separate odd and even id's *** // for($ctr=0;$ctr<$counter;$ctr++) { ?> <TR> <TD><? print ($odd_array[$ctr]); ?></TD> <TD><? print ($even_array[$ctr]); ?></TD> </TR> <? } ?> </TABLE> i hope this idea helps. ![]() crudesys |
|
#6
|
|||
|
|||
|
Hmmm.... that was a bit more complicatet than i had expected. I dont understand the part where u suddently get a $ctr in there.... where did that come from ?!...
- Pheifel |
|
#7
|
|||
|
|||
|
the $ctr variable is just my idea of going through the odd_array and the even_array members. $ctr is used as the index and increments until $counter is reached. that would go through each member of each array.
did you try the code? ![]() |
|
#8
|
|||
|
|||
|
i have implimentet it... but im still working to make it function :S it doesnt seem to work.. :S... im still trying to figure it out... im getting at a result
thnx |
|
#9
|
|||
|
|||
|
ok. just let me know if you have problems. i tried it with sample data and it worked for me
![]() good luck |
![]() |
| Viewing: Dev Articles Community Forums > Programming > General Programming Help > Odd and even |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|