Re: coding a loop in cake?
hey sam, thx. the number is sth like a product code. each product will be assigned with a unique number... seems like an alternative, since i am too stupid to make it with do while^^ On 13 Aug., 05:22, Sam s...@masterscommission360.com wrote: Also- I just had an idea... it is kind of janky but I think it will do what you want with a good speed increase. When you get your random number, check if it is already in the database by doing the following: $count = $this-find( 'count', array( 'conditions' = array( 'Linkable.number' = $randomNumber ) ) ); If $count is not 0 then the number already exists in the database- now instead of getting all the db values and looping a bunch of times, generate 10 random numbers and put them in an array and do the following $linkNumbers = $this-find( 'list', array( 'conditions' = array( 'Linkable.number' = $randomNumberArray ) ) ); Count the elements in the returned array(count($linkNumbers))- if it is less then 10 then at least one of the random numbers you generated is not yet in the database- use array_diff to find out which values are not in the database and you will be left with an array of good random numbers to use: $unusedRandomNumbers = array_diff($linkNumbers, $randomNumberArray) If the $linkNumbers array has 10 elements then just repeat the process. On Aug 12, 7:41 pm, Sam s...@masterscommission360.com wrote: Can you tell us what you are using this random number for? This might be a situation where there is an alternative solution that we could help you out with if we knew why you needed the random numbers. Also- on the topic of uuid's, aren't they just a hexadecimal number? Couldn't you just convert them to decimal as needed? On Aug 12, 3:10 am, Tomfox Wiranata tomfox.wiran...@gmail.com wrote: thx to both of you. i know i always do things complicated... so if i do what you suggested, cricket: public function getNewNumber() { do { $new_number = rand(10,10); } while ($this-_testNewNumber($new_number)); return $new_number; } i'm having an endless loop :( how would my function look like, that is checking for existence? cant believe i am having so much trouble with this On 12 Aug., 03:35, cricket zijn.digi...@gmail.com wrote: On Wed, Aug 11, 2010 at 4:45 PM, McBuck DGAF mcbuckd...@gmail.com wrote: This whole process strikes me as very inefficient. There is no mechanism to prevent your do-while loop from checking the same value an infinite number of times (unless you log each random value and check the next random value against the log AND the db). I don't know what the purpose of the random value is in your app, but I would suggest an entirely different approach. If you need random values in the range of min through max, you could generate a table ahead of time consisting of the fields id and random_number, with max- min+1 rows. (There are many random number generator sites available, and you might be able to access one of their APIs as needed.) It seems to me that it would be easier to generate these unique values in some random order ahead of time, and then just associate the random_numbers table with your current table through a 1-1 relationship. This process would make it easy to identify the next random number to be assigned, and when the random numbers have been exhausted. Just a thought. What McBuck said. I've given you a solution but you're implementing it in a very strange way. FWIW, this: while ($eyed = $this-Linkable-validateEyed($lkbl_eyed) == taken); is always going to return true. Why are you assigning a value to $eyed, which is never used, in any case? That assignment is what is screwing things up. Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
Re: coding a loop in cake?
What's so wrong with using the (auto incrementing unique) id field then? Jeremy Burns Class Outfit jeremybu...@classoutfit.com http://www.classoutfit.com On 13 Aug 2010, at 08:39, Tomfox Wiranata wrote: hey sam, thx. the number is sth like a product code. each product will be assigned with a unique number... seems like an alternative, since i am too stupid to make it with do while^^ On 13 Aug., 05:22, Sam s...@masterscommission360.com wrote: Also- I just had an idea... it is kind of janky but I think it will do what you want with a good speed increase. When you get your random number, check if it is already in the database by doing the following: $count = $this-find( 'count', array( 'conditions' = array( 'Linkable.number' = $randomNumber ) ) ); If $count is not 0 then the number already exists in the database- now instead of getting all the db values and looping a bunch of times, generate 10 random numbers and put them in an array and do the following $linkNumbers = $this-find( 'list', array( 'conditions' = array( 'Linkable.number' = $randomNumberArray ) ) ); Count the elements in the returned array(count($linkNumbers))- if it is less then 10 then at least one of the random numbers you generated is not yet in the database- use array_diff to find out which values are not in the database and you will be left with an array of good random numbers to use: $unusedRandomNumbers = array_diff($linkNumbers, $randomNumberArray) If the $linkNumbers array has 10 elements then just repeat the process. On Aug 12, 7:41 pm, Sam s...@masterscommission360.com wrote: Can you tell us what you are using this random number for? This might be a situation where there is an alternative solution that we could help you out with if we knew why you needed the random numbers. Also- on the topic of uuid's, aren't they just a hexadecimal number? Couldn't you just convert them to decimal as needed? On Aug 12, 3:10 am, Tomfox Wiranata tomfox.wiran...@gmail.com wrote: thx to both of you. i know i always do things complicated... so if i do what you suggested, cricket: public function getNewNumber() { do { $new_number = rand(10,10); } while ($this-_testNewNumber($new_number)); return $new_number; } i'm having an endless loop :( how would my function look like, that is checking for existence? cant believe i am having so much trouble with this On 12 Aug., 03:35, cricket zijn.digi...@gmail.com wrote: On Wed, Aug 11, 2010 at 4:45 PM, McBuck DGAF mcbuckd...@gmail.com wrote: This whole process strikes me as very inefficient. There is no mechanism to prevent your do-while loop from checking the same value an infinite number of times (unless you log each random value and check the next random value against the log AND the db). I don't know what the purpose of the random value is in your app, but I would suggest an entirely different approach. If you need random values in the range of min through max, you could generate a table ahead of time consisting of the fields id and random_number, with max- min+1 rows. (There are many random number generator sites available, and you might be able to access one of their APIs as needed.) It seems to me that it would be easier to generate these unique values in some random order ahead of time, and then just associate the random_numbers table with your current table through a 1-1 relationship. This process would make it easy to identify the next random number to be assigned, and when the random numbers have been exhausted. Just a thought. What McBuck said. I've given you a solution but you're implementing it in a very strange way. FWIW, this: while ($eyed = $this-Linkable-validateEyed($lkbl_eyed) == taken); is always going to return true. Why are you assigning a value to $eyed, which is never used, in any case? That assignment is what is screwing things up. Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send
Re: coding a loop in cake?
i was under the impression, that it makes trouble, when a product will be deleted and the number is free again. will this number then be skipped cause auto increment passed it a long time ago? also i want specific numbers to be reserved. so these ones should be left out when auto incrementing... if those 2 thing wont bother then it would be fine, i guess. On 13 Aug., 09:46, Jeremy Burns | Class Outfit jeremybu...@classoutfit.com wrote: What's so wrong with using the (auto incrementing unique) id field then? Jeremy Burns Class Outfit jeremybu...@classoutfit.comhttp://www.classoutfit.com On 13 Aug 2010, at 08:39, Tomfox Wiranata wrote: hey sam, thx. the number is sth like a product code. each product will be assigned with a unique number... seems like an alternative, since i am too stupid to make it with do while^^ On 13 Aug., 05:22, Sam s...@masterscommission360.com wrote: Also- I just had an idea... it is kind of janky but I think it will do what you want with a good speed increase. When you get your random number, check if it is already in the database by doing the following: $count = $this-find( 'count', array( 'conditions' = array( 'Linkable.number' = $randomNumber ) ) ); If $count is not 0 then the number already exists in the database- now instead of getting all the db values and looping a bunch of times, generate 10 random numbers and put them in an array and do the following $linkNumbers = $this-find( 'list', array( 'conditions' = array( 'Linkable.number' = $randomNumberArray ) ) ); Count the elements in the returned array(count($linkNumbers))- if it is less then 10 then at least one of the random numbers you generated is not yet in the database- use array_diff to find out which values are not in the database and you will be left with an array of good random numbers to use: $unusedRandomNumbers = array_diff($linkNumbers, $randomNumberArray) If the $linkNumbers array has 10 elements then just repeat the process. On Aug 12, 7:41 pm, Sam s...@masterscommission360.com wrote: Can you tell us what you are using this random number for? This might be a situation where there is an alternative solution that we could help you out with if we knew why you needed the random numbers. Also- on the topic of uuid's, aren't they just a hexadecimal number? Couldn't you just convert them to decimal as needed? On Aug 12, 3:10 am, Tomfox Wiranata tomfox.wiran...@gmail.com wrote: thx to both of you. i know i always do things complicated... so if i do what you suggested, cricket: public function getNewNumber() { do { $new_number = rand(10,10); } while ($this-_testNewNumber($new_number)); return $new_number; } i'm having an endless loop :( how would my function look like, that is checking for existence? cant believe i am having so much trouble with this On 12 Aug., 03:35, cricket zijn.digi...@gmail.com wrote: On Wed, Aug 11, 2010 at 4:45 PM, McBuck DGAF mcbuckd...@gmail.com wrote: This whole process strikes me as very inefficient. There is no mechanism to prevent your do-while loop from checking the same value an infinite number of times (unless you log each random value and check the next random value against the log AND the db). I don't know what the purpose of the random value is in your app, but I would suggest an entirely different approach. If you need random values in the range of min through max, you could generate a table ahead of time consisting of the fields id and random_number, with max- min+1 rows. (There are many random number generator sites available, and you might be able to access one of their APIs as needed.) It seems to me that it would be easier to generate these unique values in some random order ahead of time, and then just associate the random_numbers table with your current table through a 1-1 relationship. This process would make it easy to identify the next random number to be assigned, and when the random numbers have been exhausted. Just a thought. What McBuck said. I've given you a solution but you're implementing it in a very strange way. FWIW, this: while ($eyed = $this-Linkable-validateEyed($lkbl_eyed) == taken); is always going to return true. Why are you assigning a value to $eyed, which is never used, in any case? That assignment is what is screwing things up. Check out the new CakePHP Questions sitehttp://cakeqs.organd help others with their CakePHP related questions. You received this message because you are subscribed to
Re: coding a loop in cake?
The number will rise by 1 on each insert (or attempted insert that fails validation at the database level) and once a number has been used it is no longer available (although it is possible to do a direct insert using SQL, but not really recommended). It isn't really possible to reserve numbers ahead of time without doing lots of fancy stuff. Reading this whole trail it seems that you are using a sledgehammer to crack a nut. I'd keep it simple and let the database do what it was born to do. Jeremy Burns Class Outfit jeremybu...@classoutfit.com http://www.classoutfit.com On 13 Aug 2010, at 09:42, Tomfox Wiranata wrote: i was under the impression, that it makes trouble, when a product will be deleted and the number is free again. will this number then be skipped cause auto increment passed it a long time ago? also i want specific numbers to be reserved. so these ones should be left out when auto incrementing... if those 2 thing wont bother then it would be fine, i guess. On 13 Aug., 09:46, Jeremy Burns | Class Outfit jeremybu...@classoutfit.com wrote: What's so wrong with using the (auto incrementing unique) id field then? Jeremy Burns Class Outfit jeremybu...@classoutfit.comhttp://www.classoutfit.com On 13 Aug 2010, at 08:39, Tomfox Wiranata wrote: hey sam, thx. the number is sth like a product code. each product will be assigned with a unique number... seems like an alternative, since i am too stupid to make it with do while^^ On 13 Aug., 05:22, Sam s...@masterscommission360.com wrote: Also- I just had an idea... it is kind of janky but I think it will do what you want with a good speed increase. When you get your random number, check if it is already in the database by doing the following: $count = $this-find( 'count', array( 'conditions' = array( 'Linkable.number' = $randomNumber ) ) ); If $count is not 0 then the number already exists in the database- now instead of getting all the db values and looping a bunch of times, generate 10 random numbers and put them in an array and do the following $linkNumbers = $this-find( 'list', array( 'conditions' = array( 'Linkable.number' = $randomNumberArray ) ) ); Count the elements in the returned array(count($linkNumbers))- if it is less then 10 then at least one of the random numbers you generated is not yet in the database- use array_diff to find out which values are not in the database and you will be left with an array of good random numbers to use: $unusedRandomNumbers = array_diff($linkNumbers, $randomNumberArray) If the $linkNumbers array has 10 elements then just repeat the process. On Aug 12, 7:41 pm, Sam s...@masterscommission360.com wrote: Can you tell us what you are using this random number for? This might be a situation where there is an alternative solution that we could help you out with if we knew why you needed the random numbers. Also- on the topic of uuid's, aren't they just a hexadecimal number? Couldn't you just convert them to decimal as needed? On Aug 12, 3:10 am, Tomfox Wiranata tomfox.wiran...@gmail.com wrote: thx to both of you. i know i always do things complicated... so if i do what you suggested, cricket: public function getNewNumber() { do { $new_number = rand(10,10); } while ($this-_testNewNumber($new_number)); return $new_number; } i'm having an endless loop :( how would my function look like, that is checking for existence? cant believe i am having so much trouble with this On 12 Aug., 03:35, cricket zijn.digi...@gmail.com wrote: On Wed, Aug 11, 2010 at 4:45 PM, McBuck DGAF mcbuckd...@gmail.com wrote: This whole process strikes me as very inefficient. There is no mechanism to prevent your do-while loop from checking the same value an infinite number of times (unless you log each random value and check the next random value against the log AND the db). I don't know what the purpose of the random value is in your app, but I would suggest an entirely different approach. If you need random values in the range of min through max, you could generate a table ahead of time consisting of the fields id and random_number, with max- min+1 rows. (There are many random number generator sites available, and you might be able to access one of their APIs as needed.) It seems to me that it would be easier to generate these unique values in some random order ahead of time, and then just associate the random_numbers table with your current table through a 1-1 relationship. This process would make it easy to identify the next random number to be
Re: coding a loop in cake?
so you would suggest auto_increment? On 13 Aug., 10:48, Jeremy Burns | Class Outfit jeremybu...@classoutfit.com wrote: The number will rise by 1 on each insert (or attempted insert that fails validation at the database level) and once a number has been used it is no longer available (although it is possible to do a direct insert using SQL, but not really recommended). It isn't really possible to reserve numbers ahead of time without doing lots of fancy stuff. Reading this whole trail it seems that you are using a sledgehammer to crack a nut. I'd keep it simple and let the database do what it was born to do. Jeremy Burns Class Outfit jeremybu...@classoutfit.comhttp://www.classoutfit.com On 13 Aug 2010, at 09:42, Tomfox Wiranata wrote: i was under the impression, that it makes trouble, when a product will be deleted and the number is free again. will this number then be skipped cause auto increment passed it a long time ago? also i want specific numbers to be reserved. so these ones should be left out when auto incrementing... if those 2 thing wont bother then it would be fine, i guess. On 13 Aug., 09:46, Jeremy Burns | Class Outfit jeremybu...@classoutfit.com wrote: What's so wrong with using the (auto incrementing unique) id field then? Jeremy Burns Class Outfit jeremybu...@classoutfit.comhttp://www.classoutfit.com On 13 Aug 2010, at 08:39, Tomfox Wiranata wrote: hey sam, thx. the number is sth like a product code. each product will be assigned with a unique number... seems like an alternative, since i am too stupid to make it with do while^^ On 13 Aug., 05:22, Sam s...@masterscommission360.com wrote: Also- I just had an idea... it is kind of janky but I think it will do what you want with a good speed increase. When you get your random number, check if it is already in the database by doing the following: $count = $this-find( 'count', array( 'conditions' = array( 'Linkable.number' = $randomNumber ) ) ); If $count is not 0 then the number already exists in the database- now instead of getting all the db values and looping a bunch of times, generate 10 random numbers and put them in an array and do the following $linkNumbers = $this-find( 'list', array( 'conditions' = array( 'Linkable.number' = $randomNumberArray ) ) ); Count the elements in the returned array(count($linkNumbers))- if it is less then 10 then at least one of the random numbers you generated is not yet in the database- use array_diff to find out which values are not in the database and you will be left with an array of good random numbers to use: $unusedRandomNumbers = array_diff($linkNumbers, $randomNumberArray) If the $linkNumbers array has 10 elements then just repeat the process. On Aug 12, 7:41 pm, Sam s...@masterscommission360.com wrote: Can you tell us what you are using this random number for? This might be a situation where there is an alternative solution that we could help you out with if we knew why you needed the random numbers. Also- on the topic of uuid's, aren't they just a hexadecimal number? Couldn't you just convert them to decimal as needed? On Aug 12, 3:10 am, Tomfox Wiranata tomfox.wiran...@gmail.com wrote: thx to both of you. i know i always do things complicated... so if i do what you suggested, cricket: public function getNewNumber() { do { $new_number = rand(10,10); } while ($this-_testNewNumber($new_number)); return $new_number; } i'm having an endless loop :( how would my function look like, that is checking for existence? cant believe i am having so much trouble with this On 12 Aug., 03:35, cricket zijn.digi...@gmail.com wrote: On Wed, Aug 11, 2010 at 4:45 PM, McBuck DGAF mcbuckd...@gmail.com wrote: This whole process strikes me as very inefficient. There is no mechanism to prevent your do-while loop from checking the same value an infinite number of times (unless you log each random value and check the next random value against the log AND the db). I don't know what the purpose of the random value is in your app, but I would suggest an entirely different approach. If you need random values in the range of min through max, you could generate a table ahead of time consisting of the fields id and random_number, with max- min+1 rows. (There are many random number generator sites available, and you might be able to access one of their APIs as needed.) It seems to me that it would be easier to generate these unique values in some random
Re: coding a loop in cake?
I don't know the rest of your set up, but auto increment would seem like a really simple solution. It wouldn't need any programming either, which must be a good thing. You mention re-using numbers - I would guess that re-using a number that previously identified a completely different (and potentially obsolete) product is a bad thing. On balance, I would say that letting the database assign a new, unique and automatically generated number each time you create a product sounds like the right thing to do - unless there is some rock solid reason why this would break something else. Jeremy Burns Class Outfit jeremybu...@classoutfit.com http://www.classoutfit.com On 13 Aug 2010, at 10:04, Tomfox Wiranata wrote: so you would suggest auto_increment? On 13 Aug., 10:48, Jeremy Burns | Class Outfit jeremybu...@classoutfit.com wrote: The number will rise by 1 on each insert (or attempted insert that fails validation at the database level) and once a number has been used it is no longer available (although it is possible to do a direct insert using SQL, but not really recommended). It isn't really possible to reserve numbers ahead of time without doing lots of fancy stuff. Reading this whole trail it seems that you are using a sledgehammer to crack a nut. I'd keep it simple and let the database do what it was born to do. Jeremy Burns Class Outfit jeremybu...@classoutfit.comhttp://www.classoutfit.com On 13 Aug 2010, at 09:42, Tomfox Wiranata wrote: i was under the impression, that it makes trouble, when a product will be deleted and the number is free again. will this number then be skipped cause auto increment passed it a long time ago? also i want specific numbers to be reserved. so these ones should be left out when auto incrementing... if those 2 thing wont bother then it would be fine, i guess. On 13 Aug., 09:46, Jeremy Burns | Class Outfit jeremybu...@classoutfit.com wrote: What's so wrong with using the (auto incrementing unique) id field then? Jeremy Burns Class Outfit jeremybu...@classoutfit.comhttp://www.classoutfit.com On 13 Aug 2010, at 08:39, Tomfox Wiranata wrote: hey sam, thx. the number is sth like a product code. each product will be assigned with a unique number... seems like an alternative, since i am too stupid to make it with do while^^ On 13 Aug., 05:22, Sam s...@masterscommission360.com wrote: Also- I just had an idea... it is kind of janky but I think it will do what you want with a good speed increase. When you get your random number, check if it is already in the database by doing the following: $count = $this-find( 'count', array( 'conditions' = array( 'Linkable.number' = $randomNumber ) ) ); If $count is not 0 then the number already exists in the database- now instead of getting all the db values and looping a bunch of times, generate 10 random numbers and put them in an array and do the following $linkNumbers = $this-find( 'list', array( 'conditions' = array( 'Linkable.number' = $randomNumberArray ) ) ); Count the elements in the returned array(count($linkNumbers))- if it is less then 10 then at least one of the random numbers you generated is not yet in the database- use array_diff to find out which values are not in the database and you will be left with an array of good random numbers to use: $unusedRandomNumbers = array_diff($linkNumbers, $randomNumberArray) If the $linkNumbers array has 10 elements then just repeat the process. On Aug 12, 7:41 pm, Sam s...@masterscommission360.com wrote: Can you tell us what you are using this random number for? This might be a situation where there is an alternative solution that we could help you out with if we knew why you needed the random numbers. Also- on the topic of uuid's, aren't they just a hexadecimal number? Couldn't you just convert them to decimal as needed? On Aug 12, 3:10 am, Tomfox Wiranata tomfox.wiran...@gmail.com wrote: thx to both of you. i know i always do things complicated... so if i do what you suggested, cricket: public function getNewNumber() { do { $new_number = rand(10,10); } while ($this-_testNewNumber($new_number)); return $new_number; } i'm having an endless loop :( how would my function look like, that is checking for existence? cant believe i am having so much trouble with this On 12 Aug., 03:35, cricket zijn.digi...@gmail.com wrote: On Wed, Aug 11, 2010 at 4:45 PM, McBuck DGAF mcbuckd...@gmail.com wrote: This whole process strikes me as very inefficient. There is no mechanism to prevent your
Re: coding a loop in cake?
only onewhat if all numbers are used? it might take a while but the time will come On 13 Aug., 11:09, Jeremy Burns | Class Outfit jeremybu...@classoutfit.com wrote: I don't know the rest of your set up, but auto increment would seem like a really simple solution. It wouldn't need any programming either, which must be a good thing. You mention re-using numbers - I would guess that re-using a number that previously identified a completely different (and potentially obsolete) product is a bad thing. On balance, I would say that letting the database assign a new, unique and automatically generated number each time you create a product sounds like the right thing to do - unless there is some rock solid reason why this would break something else. Jeremy Burns Class Outfit jeremybu...@classoutfit.comhttp://www.classoutfit.com On 13 Aug 2010, at 10:04, Tomfox Wiranata wrote: so you would suggest auto_increment? On 13 Aug., 10:48, Jeremy Burns | Class Outfit jeremybu...@classoutfit.com wrote: The number will rise by 1 on each insert (or attempted insert that fails validation at the database level) and once a number has been used it is no longer available (although it is possible to do a direct insert using SQL, but not really recommended). It isn't really possible to reserve numbers ahead of time without doing lots of fancy stuff. Reading this whole trail it seems that you are using a sledgehammer to crack a nut. I'd keep it simple and let the database do what it was born to do. Jeremy Burns Class Outfit jeremybu...@classoutfit.comhttp://www.classoutfit.com On 13 Aug 2010, at 09:42, Tomfox Wiranata wrote: i was under the impression, that it makes trouble, when a product will be deleted and the number is free again. will this number then be skipped cause auto increment passed it a long time ago? also i want specific numbers to be reserved. so these ones should be left out when auto incrementing... if those 2 thing wont bother then it would be fine, i guess. On 13 Aug., 09:46, Jeremy Burns | Class Outfit jeremybu...@classoutfit.com wrote: What's so wrong with using the (auto incrementing unique) id field then? Jeremy Burns Class Outfit jeremybu...@classoutfit.comhttp://www.classoutfit.com On 13 Aug 2010, at 08:39, Tomfox Wiranata wrote: hey sam, thx. the number is sth like a product code. each product will be assigned with a unique number... seems like an alternative, since i am too stupid to make it with do while^^ On 13 Aug., 05:22, Sam s...@masterscommission360.com wrote: Also- I just had an idea... it is kind of janky but I think it will do what you want with a good speed increase. When you get your random number, check if it is already in the database by doing the following: $count = $this-find( 'count', array( 'conditions' = array( 'Linkable.number' = $randomNumber ) ) ); If $count is not 0 then the number already exists in the database- now instead of getting all the db values and looping a bunch of times, generate 10 random numbers and put them in an array and do the following $linkNumbers = $this-find( 'list', array( 'conditions' = array( 'Linkable.number' = $randomNumberArray ) ) ); Count the elements in the returned array(count($linkNumbers))- if it is less then 10 then at least one of the random numbers you generated is not yet in the database- use array_diff to find out which values are not in the database and you will be left with an array of good random numbers to use: $unusedRandomNumbers = array_diff($linkNumbers, $randomNumberArray) If the $linkNumbers array has 10 elements then just repeat the process. On Aug 12, 7:41 pm, Sam s...@masterscommission360.com wrote: Can you tell us what you are using this random number for? This might be a situation where there is an alternative solution that we could help you out with if we knew why you needed the random numbers. Also- on the topic of uuid's, aren't they just a hexadecimal number? Couldn't you just convert them to decimal as needed? On Aug 12, 3:10 am, Tomfox Wiranata tomfox.wiran...@gmail.com wrote: thx to both of you. i know i always do things complicated... so if i do what you suggested, cricket: public function getNewNumber() { do { $new_number = rand(10,10); } while ($this-_testNewNumber($new_number)); return $new_number; } i'm having an endless loop :( how would my function look like, that is checking for existence? cant believe i am having so much trouble with
Re: coding a loop in cake?
Use the right data type. If you are concerned about the numbers running out use an unsigned bigint; that goes to 1,8446,744,073,709,551,615. Would that be enough for you? ;-) http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html Jeremy Burns Class Outfit jeremybu...@classoutfit.com http://www.classoutfit.com On 13 Aug 2010, at 10:24, Tomfox Wiranata wrote: only onewhat if all numbers are used? it might take a while but the time will come On 13 Aug., 11:09, Jeremy Burns | Class Outfit jeremybu...@classoutfit.com wrote: I don't know the rest of your set up, but auto increment would seem like a really simple solution. It wouldn't need any programming either, which must be a good thing. You mention re-using numbers - I would guess that re-using a number that previously identified a completely different (and potentially obsolete) product is a bad thing. On balance, I would say that letting the database assign a new, unique and automatically generated number each time you create a product sounds like the right thing to do - unless there is some rock solid reason why this would break something else. Jeremy Burns Class Outfit jeremybu...@classoutfit.comhttp://www.classoutfit.com On 13 Aug 2010, at 10:04, Tomfox Wiranata wrote: so you would suggest auto_increment? On 13 Aug., 10:48, Jeremy Burns | Class Outfit jeremybu...@classoutfit.com wrote: The number will rise by 1 on each insert (or attempted insert that fails validation at the database level) and once a number has been used it is no longer available (although it is possible to do a direct insert using SQL, but not really recommended). It isn't really possible to reserve numbers ahead of time without doing lots of fancy stuff. Reading this whole trail it seems that you are using a sledgehammer to crack a nut. I'd keep it simple and let the database do what it was born to do. Jeremy Burns Class Outfit jeremybu...@classoutfit.comhttp://www.classoutfit.com On 13 Aug 2010, at 09:42, Tomfox Wiranata wrote: i was under the impression, that it makes trouble, when a product will be deleted and the number is free again. will this number then be skipped cause auto increment passed it a long time ago? also i want specific numbers to be reserved. so these ones should be left out when auto incrementing... if those 2 thing wont bother then it would be fine, i guess. On 13 Aug., 09:46, Jeremy Burns | Class Outfit jeremybu...@classoutfit.com wrote: What's so wrong with using the (auto incrementing unique) id field then? Jeremy Burns Class Outfit jeremybu...@classoutfit.comhttp://www.classoutfit.com On 13 Aug 2010, at 08:39, Tomfox Wiranata wrote: hey sam, thx. the number is sth like a product code. each product will be assigned with a unique number... seems like an alternative, since i am too stupid to make it with do while^^ On 13 Aug., 05:22, Sam s...@masterscommission360.com wrote: Also- I just had an idea... it is kind of janky but I think it will do what you want with a good speed increase. When you get your random number, check if it is already in the database by doing the following: $count = $this-find( 'count', array( 'conditions' = array( 'Linkable.number' = $randomNumber ) ) ); If $count is not 0 then the number already exists in the database- now instead of getting all the db values and looping a bunch of times, generate 10 random numbers and put them in an array and do the following $linkNumbers = $this-find( 'list', array( 'conditions' = array( 'Linkable.number' = $randomNumberArray ) ) ); Count the elements in the returned array(count($linkNumbers))- if it is less then 10 then at least one of the random numbers you generated is not yet in the database- use array_diff to find out which values are not in the database and you will be left with an array of good random numbers to use: $unusedRandomNumbers = array_diff($linkNumbers, $randomNumberArray) If the $linkNumbers array has 10 elements then just repeat the process. On Aug 12, 7:41 pm, Sam s...@masterscommission360.com wrote: Can you tell us what you are using this random number for? This might be a situation where there is an alternative solution that we could help you out with if we knew why you needed the random numbers. Also- on the topic of uuid's, aren't they just a hexadecimal number? Couldn't you just convert them to decimal as needed? On Aug 12, 3:10 am, Tomfox Wiranata tomfox.wiran...@gmail.com wrote: thx to both of you. i know i always do things complicated... so if i do what you suggested, cricket: public function getNewNumber()
Re: coding a loop in cake?
On Aug 13, 11:24 am, Tomfox Wiranata tomfox.wiran...@gmail.com wrote: only onewhat if all numbers are used? it might take a while but the time will come If you really are going to be creating more than 18,446,744,073,709,551,615 products - I suggest a couple of things: 1) get help 2) use uuids in the mean time just build it dammit Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
Re: coding a loop in cake?
Seriously... a while? You're right... the time will eventually come. Using bigiint as your primary key will give you 2^64 rows... roughly 1.8e19... that's over a billion squared. You'd have to add a billion rows a day for about 50 million years to reach that limit. If your application will have that many products you should invest in hiring a database architect or getting someone to write you a custom database. On Aug 13, 4:24 am, Tomfox Wiranata tomfox.wiran...@gmail.com wrote: only onewhat if all numbers are used? it might take a while but the time will come On 13 Aug., 11:09, Jeremy Burns | Class Outfit jeremybu...@classoutfit.com wrote: I don't know the rest of your set up, but auto increment would seem like a really simple solution. It wouldn't need any programming either, which must be a good thing. You mention re-using numbers - I would guess that re-using a number that previously identified a completely different (and potentially obsolete) product is a bad thing. On balance, I would say that letting the database assign a new, unique and automatically generated number each time you create a product sounds like the right thing to do - unless there is some rock solid reason why this would break something else. Jeremy Burns Class Outfit jeremybu...@classoutfit.comhttp://www.classoutfit.com On 13 Aug 2010, at 10:04, Tomfox Wiranata wrote: so you would suggest auto_increment? On 13 Aug., 10:48, Jeremy Burns | Class Outfit jeremybu...@classoutfit.com wrote: The number will rise by 1 on each insert (or attempted insert that fails validation at the database level) and once a number has been used it is no longer available (although it is possible to do a direct insert using SQL, but not really recommended). It isn't really possible to reserve numbers ahead of time without doing lots of fancy stuff. Reading this whole trail it seems that you are using a sledgehammer to crack a nut. I'd keep it simple and let the database do what it was born to do. Jeremy Burns Class Outfit jeremybu...@classoutfit.comhttp://www.classoutfit.com On 13 Aug 2010, at 09:42, Tomfox Wiranata wrote: i was under the impression, that it makes trouble, when a product will be deleted and the number is free again. will this number then be skipped cause auto increment passed it a long time ago? also i want specific numbers to be reserved. so these ones should be left out when auto incrementing... if those 2 thing wont bother then it would be fine, i guess. On 13 Aug., 09:46, Jeremy Burns | Class Outfit jeremybu...@classoutfit.com wrote: What's so wrong with using the (auto incrementing unique) id field then? Jeremy Burns Class Outfit jeremybu...@classoutfit.comhttp://www.classoutfit.com On 13 Aug 2010, at 08:39, Tomfox Wiranata wrote: hey sam, thx. the number is sth like a product code. each product will be assigned with a unique number... seems like an alternative, since i am too stupid to make it with do while^^ On 13 Aug., 05:22, Sam s...@masterscommission360.com wrote: Also- I just had an idea... it is kind of janky but I think it will do what you want with a good speed increase. When you get your random number, check if it is already in the database by doing the following: $count = $this-find( 'count', array( 'conditions' = array( 'Linkable.number' = $randomNumber ) ) ); If $count is not 0 then the number already exists in the database- now instead of getting all the db values and looping a bunch of times, generate 10 random numbers and put them in an array and do the following $linkNumbers = $this-find( 'list', array( 'conditions' = array( 'Linkable.number' = $randomNumberArray ) ) ); Count the elements in the returned array(count($linkNumbers))- if it is less then 10 then at least one of the random numbers you generated is not yet in the database- use array_diff to find out which values are not in the database and you will be left with an array of good random numbers to use: $unusedRandomNumbers = array_diff($linkNumbers, $randomNumberArray) If the $linkNumbers array has 10 elements then just repeat the process. On Aug 12, 7:41 pm, Sam s...@masterscommission360.com wrote: Can you tell us what you are using this random number for? This might be a situation where there is an alternative solution that we could help you out with if we knew why you needed the random numbers. Also- on the topic of uuid's, aren't they just a hexadecimal number?
Re: coding a loop in cake?
lol...thx for the sarcasm...;) was not aware that i can go till 1,8446,744,073,709,551,615. that might be enough i'll go with auto incrementTHX a lot On 13 Aug., 13:41, Sam s...@masterscommission360.com wrote: Seriously... a while? You're right... the time will eventually come. Using bigiint as your primary key will give you 2^64 rows... roughly 1.8e19... that's over a billion squared. You'd have to add a billion rows a day for about 50 million years to reach that limit. If your application will have that many products you should invest in hiring a database architect or getting someone to write you a custom database. On Aug 13, 4:24 am, Tomfox Wiranata tomfox.wiran...@gmail.com wrote: only onewhat if all numbers are used? it might take a while but the time will come On 13 Aug., 11:09, Jeremy Burns | Class Outfit jeremybu...@classoutfit.com wrote: I don't know the rest of your set up, but auto increment would seem like a really simple solution. It wouldn't need any programming either, which must be a good thing. You mention re-using numbers - I would guess that re-using a number that previously identified a completely different (and potentially obsolete) product is a bad thing. On balance, I would say that letting the database assign a new, unique and automatically generated number each time you create a product sounds like the right thing to do - unless there is some rock solid reason why this would break something else. Jeremy Burns Class Outfit jeremybu...@classoutfit.comhttp://www.classoutfit.com On 13 Aug 2010, at 10:04, Tomfox Wiranata wrote: so you would suggest auto_increment? On 13 Aug., 10:48, Jeremy Burns | Class Outfit jeremybu...@classoutfit.com wrote: The number will rise by 1 on each insert (or attempted insert that fails validation at the database level) and once a number has been used it is no longer available (although it is possible to do a direct insert using SQL, but not really recommended). It isn't really possible to reserve numbers ahead of time without doing lots of fancy stuff. Reading this whole trail it seems that you are using a sledgehammer to crack a nut. I'd keep it simple and let the database do what it was born to do. Jeremy Burns Class Outfit jeremybu...@classoutfit.comhttp://www.classoutfit.com On 13 Aug 2010, at 09:42, Tomfox Wiranata wrote: i was under the impression, that it makes trouble, when a product will be deleted and the number is free again. will this number then be skipped cause auto increment passed it a long time ago? also i want specific numbers to be reserved. so these ones should be left out when auto incrementing... if those 2 thing wont bother then it would be fine, i guess. On 13 Aug., 09:46, Jeremy Burns | Class Outfit jeremybu...@classoutfit.com wrote: What's so wrong with using the (auto incrementing unique) id field then? Jeremy Burns Class Outfit jeremybu...@classoutfit.comhttp://www.classoutfit.com On 13 Aug 2010, at 08:39, Tomfox Wiranata wrote: hey sam, thx. the number is sth like a product code. each product will be assigned with a unique number... seems like an alternative, since i am too stupid to make it with do while^^ On 13 Aug., 05:22, Sam s...@masterscommission360.com wrote: Also- I just had an idea... it is kind of janky but I think it will do what you want with a good speed increase. When you get your random number, check if it is already in the database by doing the following: $count = $this-find( 'count', array( 'conditions' = array( 'Linkable.number' = $randomNumber ) ) ); If $count is not 0 then the number already exists in the database- now instead of getting all the db values and looping a bunch of times, generate 10 random numbers and put them in an array and do the following $linkNumbers = $this-find( 'list', array( 'conditions' = array( 'Linkable.number' = $randomNumberArray ) ) ); Count the elements in the returned array(count($linkNumbers))- if it is less then 10 then at least one of the random numbers you generated is not yet in the database- use array_diff to find out which values are not in the database and you will be left with an array of good random numbers to use: $unusedRandomNumbers = array_diff($linkNumbers, $randomNumberArray) If the $linkNumbers array has 10 elements then just repeat the process. On Aug 12, 7:41 pm, Sam
Re: coding a loop in cake?
Thats why they suggested using UUID. The sun will burn out before you duplicate one of those. On the flip side you could use a bigint for a possible 9,223,372,036,854,775,807 products or a regular int will provide you with 2,147,483,647 products. (Values apply to MySQL) On Aug 13, 4:24 am, Tomfox Wiranata tomfox.wiran...@gmail.com wrote: only onewhat if all numbers are used? it might take a while but the time will come On 13 Aug., 11:09, Jeremy Burns | Class Outfit jeremybu...@classoutfit.com wrote: I don't know the rest of your set up, but auto increment would seem like a really simple solution. It wouldn't need any programming either, which must be a good thing. You mention re-using numbers - I would guess that re-using a number that previously identified a completely different (and potentially obsolete) product is a bad thing. On balance, I would say that letting the database assign a new, unique and automatically generated number each time you create a product sounds like the right thing to do - unless there is some rock solid reason why this would break something else. Jeremy Burns Class Outfit jeremybu...@classoutfit.comhttp://www.classoutfit.com On 13 Aug 2010, at 10:04, Tomfox Wiranata wrote: so you would suggest auto_increment? On 13 Aug., 10:48, Jeremy Burns | Class Outfit jeremybu...@classoutfit.com wrote: The number will rise by 1 on each insert (or attempted insert that fails validation at the database level) and once a number has been used it is no longer available (although it is possible to do a direct insert using SQL, but not really recommended). It isn't really possible to reserve numbers ahead of time without doing lots of fancy stuff. Reading this whole trail it seems that you are using a sledgehammer to crack a nut. I'd keep it simple and let the database do what it was born to do. Jeremy Burns Class Outfit jeremybu...@classoutfit.comhttp://www.classoutfit.com On 13 Aug 2010, at 09:42, Tomfox Wiranata wrote: i was under the impression, that it makes trouble, when a product will be deleted and the number is free again. will this number then be skipped cause auto increment passed it a long time ago? also i want specific numbers to be reserved. so these ones should be left out when auto incrementing... if those 2 thing wont bother then it would be fine, i guess. On 13 Aug., 09:46, Jeremy Burns | Class Outfit jeremybu...@classoutfit.com wrote: What's so wrong with using the (auto incrementing unique) id field then? Jeremy Burns Class Outfit jeremybu...@classoutfit.comhttp://www.classoutfit.com On 13 Aug 2010, at 08:39, Tomfox Wiranata wrote: hey sam, thx. the number is sth like a product code. each product will be assigned with a unique number... seems like an alternative, since i am too stupid to make it with do while^^ On 13 Aug., 05:22, Sam s...@masterscommission360.com wrote: Also- I just had an idea... it is kind of janky but I think it will do what you want with a good speed increase. When you get your random number, check if it is already in the database by doing the following: $count = $this-find( 'count', array( 'conditions' = array( 'Linkable.number' = $randomNumber ) ) ); If $count is not 0 then the number already exists in the database- now instead of getting all the db values and looping a bunch of times, generate 10 random numbers and put them in an array and do the following $linkNumbers = $this-find( 'list', array( 'conditions' = array( 'Linkable.number' = $randomNumberArray ) ) ); Count the elements in the returned array(count($linkNumbers))- if it is less then 10 then at least one of the random numbers you generated is not yet in the database- use array_diff to find out which values are not in the database and you will be left with an array of good random numbers to use: $unusedRandomNumbers = array_diff($linkNumbers, $randomNumberArray) If the $linkNumbers array has 10 elements then just repeat the process. On Aug 12, 7:41 pm, Sam s...@masterscommission360.com wrote: Can you tell us what you are using this random number for? This might be a situation where there is an alternative solution that we could help you out with if we knew why you needed the random numbers. Also- on the topic of uuid's, aren't they just a hexadecimal number? Couldn't you just convert them to decimal as needed? On Aug 12, 3:10 am, Tomfox Wiranata tomfox.wiran...@gmail.com wrote: thx to
Re: coding a loop in cake?
bah, sorry... i didn't notice there was a second page... On Aug 13, 10:53 am, Anthony anthony.c.fra...@gmail.com wrote: Thats why they suggested using UUID. The sun will burn out before you duplicate one of those. On the flip side you could use a bigint for a possible 9,223,372,036,854,775,807 products or a regular int will provide you with 2,147,483,647 products. (Values apply to MySQL) On Aug 13, 4:24 am, Tomfox Wiranata tomfox.wiran...@gmail.com wrote: only onewhat if all numbers are used? it might take a while but the time will come On 13 Aug., 11:09, Jeremy Burns | Class Outfit jeremybu...@classoutfit.com wrote: I don't know the rest of your set up, but auto increment would seem like a really simple solution. It wouldn't need any programming either, which must be a good thing. You mention re-using numbers - I would guess that re-using a number that previously identified a completely different (and potentially obsolete) product is a bad thing. On balance, I would say that letting the database assign a new, unique and automatically generated number each time you create a product sounds like the right thing to do - unless there is some rock solid reason why this would break something else. Jeremy Burns Class Outfit jeremybu...@classoutfit.comhttp://www.classoutfit.com On 13 Aug 2010, at 10:04, Tomfox Wiranata wrote: so you would suggest auto_increment? On 13 Aug., 10:48, Jeremy Burns | Class Outfit jeremybu...@classoutfit.com wrote: The number will rise by 1 on each insert (or attempted insert that fails validation at the database level) and once a number has been used it is no longer available (although it is possible to do a direct insert using SQL, but not really recommended). It isn't really possible to reserve numbers ahead of time without doing lots of fancy stuff. Reading this whole trail it seems that you are using a sledgehammer to crack a nut. I'd keep it simple and let the database do what it was born to do. Jeremy Burns Class Outfit jeremybu...@classoutfit.comhttp://www.classoutfit.com On 13 Aug 2010, at 09:42, Tomfox Wiranata wrote: i was under the impression, that it makes trouble, when a product will be deleted and the number is free again. will this number then be skipped cause auto increment passed it a long time ago? also i want specific numbers to be reserved. so these ones should be left out when auto incrementing... if those 2 thing wont bother then it would be fine, i guess. On 13 Aug., 09:46, Jeremy Burns | Class Outfit jeremybu...@classoutfit.com wrote: What's so wrong with using the (auto incrementing unique) id field then? Jeremy Burns Class Outfit jeremybu...@classoutfit.comhttp://www.classoutfit.com On 13 Aug 2010, at 08:39, Tomfox Wiranata wrote: hey sam, thx. the number is sth like a product code. each product will be assigned with a unique number... seems like an alternative, since i am too stupid to make it with do while^^ On 13 Aug., 05:22, Sam s...@masterscommission360.com wrote: Also- I just had an idea... it is kind of janky but I think it will do what you want with a good speed increase. When you get your random number, check if it is already in the database by doing the following: $count = $this-find( 'count', array( 'conditions' = array( 'Linkable.number' = $randomNumber ) ) ); If $count is not 0 then the number already exists in the database- now instead of getting all the db values and looping a bunch of times, generate 10 random numbers and put them in an array and do the following $linkNumbers = $this-find( 'list', array( 'conditions' = array( 'Linkable.number' = $randomNumberArray ) ) ); Count the elements in the returned array(count($linkNumbers))- if it is less then 10 then at least one of the random numbers you generated is not yet in the database- use array_diff to find out which values are not in the database and you will be left with an array of good random numbers to use: $unusedRandomNumbers = array_diff($linkNumbers, $randomNumberArray) If the $linkNumbers array has 10 elements then just repeat the process. On Aug 12, 7:41 pm, Sam s...@masterscommission360.com wrote: Can you tell us what you are using this random number for? This might be a situation where there is an alternative solution that we could help you out with if we knew why you
Re: coding a loop in cake?
Second page... that's why I didn't see anyones messages until I saw the digest.. I am so lame right now. On Aug 13, 11:14 am, Anthony anthony.c.fra...@gmail.com wrote: bah, sorry... i didn't notice there was a second page... On Aug 13, 10:53 am, Anthony anthony.c.fra...@gmail.com wrote: Thats why they suggested using UUID. The sun will burn out before you duplicate one of those. On the flip side you could use a bigint for a possible 9,223,372,036,854,775,807 products or a regular int will provide you with 2,147,483,647 products. (Values apply to MySQL) On Aug 13, 4:24 am, Tomfox Wiranata tomfox.wiran...@gmail.com wrote: only onewhat if all numbers are used? it might take a while but the time will come On 13 Aug., 11:09, Jeremy Burns | Class Outfit jeremybu...@classoutfit.com wrote: I don't know the rest of your set up, but auto increment would seem like a really simple solution. It wouldn't need any programming either, which must be a good thing. You mention re-using numbers - I would guess that re-using a number that previously identified a completely different (and potentially obsolete) product is a bad thing. On balance, I would say that letting the database assign a new, unique and automatically generated number each time you create a product sounds like the right thing to do - unless there is some rock solid reason why this would break something else. Jeremy Burns Class Outfit jeremybu...@classoutfit.comhttp://www.classoutfit.com On 13 Aug 2010, at 10:04, Tomfox Wiranata wrote: so you would suggest auto_increment? On 13 Aug., 10:48, Jeremy Burns | Class Outfit jeremybu...@classoutfit.com wrote: The number will rise by 1 on each insert (or attempted insert that fails validation at the database level) and once a number has been used it is no longer available (although it is possible to do a direct insert using SQL, but not really recommended). It isn't really possible to reserve numbers ahead of time without doing lots of fancy stuff. Reading this whole trail it seems that you are using a sledgehammer to crack a nut. I'd keep it simple and let the database do what it was born to do. Jeremy Burns Class Outfit jeremybu...@classoutfit.comhttp://www.classoutfit.com On 13 Aug 2010, at 09:42, Tomfox Wiranata wrote: i was under the impression, that it makes trouble, when a product will be deleted and the number is free again. will this number then be skipped cause auto increment passed it a long time ago? also i want specific numbers to be reserved. so these ones should be left out when auto incrementing... if those 2 thing wont bother then it would be fine, i guess. On 13 Aug., 09:46, Jeremy Burns | Class Outfit jeremybu...@classoutfit.com wrote: What's so wrong with using the (auto incrementing unique) id field then? Jeremy Burns Class Outfit jeremybu...@classoutfit.comhttp://www.classoutfit.com On 13 Aug 2010, at 08:39, Tomfox Wiranata wrote: hey sam, thx. the number is sth like a product code. each product will be assigned with a unique number... seems like an alternative, since i am too stupid to make it with do while^^ On 13 Aug., 05:22, Sam s...@masterscommission360.com wrote: Also- I just had an idea... it is kind of janky but I think it will do what you want with a good speed increase. When you get your random number, check if it is already in the database by doing the following: $count = $this-find( 'count', array( 'conditions' = array( 'Linkable.number' = $randomNumber ) ) ); If $count is not 0 then the number already exists in the database- now instead of getting all the db values and looping a bunch of times, generate 10 random numbers and put them in an array and do the following $linkNumbers = $this-find( 'list', array( 'conditions' = array( 'Linkable.number' = $randomNumberArray ) ) ); Count the elements in the returned array(count($linkNumbers))- if it is less then 10 then at least one of the random numbers you generated is not yet in the database- use array_diff to find out which values are not in the database and you will be left with an array of good random numbers to use: $unusedRandomNumbers = array_diff($linkNumbers, $randomNumberArray) If the $linkNumbers array has 10
Re: coding a loop in cake?
thx to both of you. i know i always do things complicated... so if i do what you suggested, cricket: public function getNewNumber() { do { $new_number = rand(10,10); } while ($this-_testNewNumber($new_number)); return $new_number; } i'm having an endless loop :( how would my function look like, that is checking for existence? cant believe i am having so much trouble with this On 12 Aug., 03:35, cricket zijn.digi...@gmail.com wrote: On Wed, Aug 11, 2010 at 4:45 PM, McBuck DGAF mcbuckd...@gmail.com wrote: This whole process strikes me as very inefficient. There is no mechanism to prevent your do-while loop from checking the same value an infinite number of times (unless you log each random value and check the next random value against the log AND the db). I don't know what the purpose of the random value is in your app, but I would suggest an entirely different approach. If you need random values in the range of min through max, you could generate a table ahead of time consisting of the fields id and random_number, with max- min+1 rows. (There are many random number generator sites available, and you might be able to access one of their APIs as needed.) It seems to me that it would be easier to generate these unique values in some random order ahead of time, and then just associate the random_numbers table with your current table through a 1-1 relationship. This process would make it easy to identify the next random number to be assigned, and when the random numbers have been exhausted. Just a thought. What McBuck said. I've given you a solution but you're implementing it in a very strange way. FWIW, this: while ($eyed = $this-Linkable-validateEyed($lkbl_eyed) == taken); is always going to return true. Why are you assigning a value to $eyed, which is never used, in any case? That assignment is what is screwing things up. Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
Re: coding a loop in cake?
Can you tell us what you are using this random number for? This might be a situation where there is an alternative solution that we could help you out with if we knew why you needed the random numbers. Also- on the topic of uuid's, aren't they just a hexadecimal number? Couldn't you just convert them to decimal as needed? On Aug 12, 3:10 am, Tomfox Wiranata tomfox.wiran...@gmail.com wrote: thx to both of you. i know i always do things complicated... so if i do what you suggested, cricket: public function getNewNumber() { do { $new_number = rand(10,10); } while ($this-_testNewNumber($new_number)); return $new_number; } i'm having an endless loop :( how would my function look like, that is checking for existence? cant believe i am having so much trouble with this On 12 Aug., 03:35, cricket zijn.digi...@gmail.com wrote: On Wed, Aug 11, 2010 at 4:45 PM, McBuck DGAF mcbuckd...@gmail.com wrote: This whole process strikes me as very inefficient. There is no mechanism to prevent your do-while loop from checking the same value an infinite number of times (unless you log each random value and check the next random value against the log AND the db). I don't know what the purpose of the random value is in your app, but I would suggest an entirely different approach. If you need random values in the range of min through max, you could generate a table ahead of time consisting of the fields id and random_number, with max- min+1 rows. (There are many random number generator sites available, and you might be able to access one of their APIs as needed.) It seems to me that it would be easier to generate these unique values in some random order ahead of time, and then just associate the random_numbers table with your current table through a 1-1 relationship. This process would make it easy to identify the next random number to be assigned, and when the random numbers have been exhausted. Just a thought. What McBuck said. I've given you a solution but you're implementing it in a very strange way. FWIW, this: while ($eyed = $this-Linkable-validateEyed($lkbl_eyed) == taken); is always going to return true. Why are you assigning a value to $eyed, which is never used, in any case? That assignment is what is screwing things up. Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
Re: coding a loop in cake?
Also- I just had an idea... it is kind of janky but I think it will do what you want with a good speed increase. When you get your random number, check if it is already in the database by doing the following: $count = $this-find( 'count', array( 'conditions' = array( 'Linkable.number' = $randomNumber ) ) ); If $count is not 0 then the number already exists in the database- now instead of getting all the db values and looping a bunch of times, generate 10 random numbers and put them in an array and do the following $linkNumbers = $this-find( 'list', array( 'conditions' = array( 'Linkable.number' = $randomNumberArray ) ) ); Count the elements in the returned array(count($linkNumbers))- if it is less then 10 then at least one of the random numbers you generated is not yet in the database- use array_diff to find out which values are not in the database and you will be left with an array of good random numbers to use: $unusedRandomNumbers = array_diff($linkNumbers, $randomNumberArray) If the $linkNumbers array has 10 elements then just repeat the process. On Aug 12, 7:41 pm, Sam s...@masterscommission360.com wrote: Can you tell us what you are using this random number for? This might be a situation where there is an alternative solution that we could help you out with if we knew why you needed the random numbers. Also- on the topic of uuid's, aren't they just a hexadecimal number? Couldn't you just convert them to decimal as needed? On Aug 12, 3:10 am, Tomfox Wiranata tomfox.wiran...@gmail.com wrote: thx to both of you. i know i always do things complicated... so if i do what you suggested, cricket: public function getNewNumber() { do { $new_number = rand(10,10); } while ($this-_testNewNumber($new_number)); return $new_number; } i'm having an endless loop :( how would my function look like, that is checking for existence? cant believe i am having so much trouble with this On 12 Aug., 03:35, cricket zijn.digi...@gmail.com wrote: On Wed, Aug 11, 2010 at 4:45 PM, McBuck DGAF mcbuckd...@gmail.com wrote: This whole process strikes me as very inefficient. There is no mechanism to prevent your do-while loop from checking the same value an infinite number of times (unless you log each random value and check the next random value against the log AND the db). I don't know what the purpose of the random value is in your app, but I would suggest an entirely different approach. If you need random values in the range of min through max, you could generate a table ahead of time consisting of the fields id and random_number, with max- min+1 rows. (There are many random number generator sites available, and you might be able to access one of their APIs as needed.) It seems to me that it would be easier to generate these unique values in some random order ahead of time, and then just associate the random_numbers table with your current table through a 1-1 relationship. This process would make it easy to identify the next random number to be assigned, and when the random numbers have been exhausted. Just a thought. What McBuck said. I've given you a solution but you're implementing it in a very strange way. FWIW, this: while ($eyed = $this-Linkable-validateEyed($lkbl_eyed) == taken); is always going to return true. Why are you assigning a value to $eyed, which is never used, in any case? That assignment is what is screwing things up. Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
Re: coding a loop in cake?
ok i tried your solution and somehow cake does not care if it writes the same number over and over in my database. maybe you see what's wrong here :) my function in my model abc that finds and compares the number. my logic: when no entry is found here $eyed is empty. function validateEyed($data) { $eyed = $this-find(array('eyed' = $data), array('eyed')); if(empty($eyed) == true) return $eyed['abc']; return false; } I am calling this function from my controller function __createEyed() { do{ $test=rand(1,2); } while ($eyed = $this-abc-validateEyed($test) == true); return $test; } cakes writes those rand numbers 1 or 2 multiple times in my table. so where is the mistake?? thx :) On 10 Aug., 18:39, cricket zijn.digi...@gmail.com wrote: On Tue, Aug 10, 2010 at 12:30 PM, Tomfox Wiranata tomfox.wiran...@gmail.com wrote: thx to both.. :) @cricket: am i getting it right? you are writing all numers from the database into an array and compare both? is it possible that at a certain point it will get to big or too much data? cause my range starts at 1 and ends at 9 if there are 50million in the table they all get saved in an array Yes, that's correct. I should have pointed that out. You could cache the results, of course, but perhaps a better way would be to pass $new_number to a protected method (that does a find() and comparison) inside the while section. You'd have to have the separate method because while() isn't a block, where you can have several lines of code. Something like: public function getNewNumber() { do { $new_number = rand(10,10); } while ($this-_testNewNumber($new_number)); return $new_number; } protected function _testNewNumber($x) { // return $x exists in DB } Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
Re: coding a loop in cake?
Do you have a unique index on the field? If you haven't and you really do want it to be unique, I'd stick on on. Then you could work on the basis that you *probably* aren't going to run into the same number that often (at least it is far more likely that your number is unique than it is a duplicate), generate a random number, save it and only create another one of the save fails (then rinse and repeat). Would a UUID work for you? That would save you some effort. Jeremy Burns Class Outfit jeremybu...@classoutfit.com http://www.classoutfit.com On 11 Aug 2010, at 10:18, Tomfox Wiranata wrote: ok i tried your solution and somehow cake does not care if it writes the same number over and over in my database. maybe you see what's wrong here :) my function in my model abc that finds and compares the number. my logic: when no entry is found here $eyed is empty. function validateEyed($data) { $eyed = $this-find(array('eyed' = $data), array('eyed')); if(empty($eyed) == true) return $eyed['abc']; return false; } I am calling this function from my controller function __createEyed() { do{ $test=rand(1,2); } while ($eyed = $this-abc-validateEyed($test) == true); return $test; } cakes writes those rand numbers 1 or 2 multiple times in my table. so where is the mistake?? thx :) On 10 Aug., 18:39, cricket zijn.digi...@gmail.com wrote: On Tue, Aug 10, 2010 at 12:30 PM, Tomfox Wiranata tomfox.wiran...@gmail.com wrote: thx to both.. :) @cricket: am i getting it right? you are writing all numers from the database into an array and compare both? is it possible that at a certain point it will get to big or too much data? cause my range starts at 1 and ends at 9 if there are 50million in the table they all get saved in an array Yes, that's correct. I should have pointed that out. You could cache the results, of course, but perhaps a better way would be to pass $new_number to a protected method (that does a find() and comparison) inside the while section. You'd have to have the separate method because while() isn't a block, where you can have several lines of code. Something like: public function getNewNumber() { do { $new_number = rand(10,10); } while ($this-_testNewNumber($new_number)); return $new_number; } protected function _testNewNumber($x) { // return $x exists in DB } Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
Re: coding a loop in cake?
thx for your reply jeremy. i dont have an unique index for that field. at least i didnt remember doing that.. how do i create this UUID in xampp and what do I have to change in my code? thx :) On 11 Aug., 11:50, Jeremy Burns | Class Outfit jeremybu...@classoutfit.com wrote: Do you have a unique index on the field? If you haven't and you really do want it to be unique, I'd stick on on. Then you could work on the basis that you *probably* aren't going to run into the same number that often (at least it is far more likely that your number is unique than it is a duplicate), generate a random number, save it and only create another one of the save fails (then rinse and repeat). Would a UUID work for you? That would save you some effort. Jeremy Burns Class Outfit jeremybu...@classoutfit.comhttp://www.classoutfit.com On 11 Aug 2010, at 10:18, Tomfox Wiranata wrote: ok i tried your solution and somehow cake does not care if it writes the same number over and over in my database. maybe you see what's wrong here :) my function in my model abc that finds and compares the number. my logic: when no entry is found here $eyed is empty. function validateEyed($data) { $eyed = $this-find(array('eyed' = $data), array('eyed')); if(empty($eyed) == true) return $eyed['abc']; return false; } I am calling this function from my controller function __createEyed() { do{ $test=rand(1,2); } while ($eyed = $this-abc-validateEyed($test) == true); return $test; } cakes writes those rand numbers 1 or 2 multiple times in my table. so where is the mistake?? thx :) On 10 Aug., 18:39, cricket zijn.digi...@gmail.com wrote: On Tue, Aug 10, 2010 at 12:30 PM, Tomfox Wiranata tomfox.wiran...@gmail.com wrote: thx to both.. :) @cricket: am i getting it right? you are writing all numers from the database into an array and compare both? is it possible that at a certain point it will get to big or too much data? cause my range starts at 1 and ends at 9 if there are 50million in the table they all get saved in an array Yes, that's correct. I should have pointed that out. You could cache the results, of course, but perhaps a better way would be to pass $new_number to a protected method (that does a find() and comparison) inside the while section. You'd have to have the separate method because while() isn't a block, where you can have several lines of code. Something like: public function getNewNumber() { do { $new_number = rand(10,10); } while ($this-_testNewNumber($new_number)); return $new_number; } protected function _testNewNumber($x) { // return $x exists in DB } Check out the new CakePHP Questions sitehttp://cakeqs.organd help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group athttp://groups.google.com/group/cake-php?hl=en Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
Re: coding a loop in cake?
Check the Cake section on UUIDs first to see if it is a possible solution for you - it might not be, of course. They are usually used when the field is an auto incrementing primary key - I am not sure if they are flexible enough to work as non primary keys. http://book.cakephp.org/view/1016/Using-UUIDs-as-Primary-Keys Jeremy Burns Class Outfit jeremybu...@classoutfit.com http://www.classoutfit.com On 11 Aug 2010, at 11:05, Tomfox Wiranata wrote: thx for your reply jeremy. i dont have an unique index for that field. at least i didnt remember doing that.. how do i create this UUID in xampp and what do I have to change in my code? thx :) On 11 Aug., 11:50, Jeremy Burns | Class Outfit jeremybu...@classoutfit.com wrote: Do you have a unique index on the field? If you haven't and you really do want it to be unique, I'd stick on on. Then you could work on the basis that you *probably* aren't going to run into the same number that often (at least it is far more likely that your number is unique than it is a duplicate), generate a random number, save it and only create another one of the save fails (then rinse and repeat). Would a UUID work for you? That would save you some effort. Jeremy Burns Class Outfit jeremybu...@classoutfit.comhttp://www.classoutfit.com On 11 Aug 2010, at 10:18, Tomfox Wiranata wrote: ok i tried your solution and somehow cake does not care if it writes the same number over and over in my database. maybe you see what's wrong here :) my function in my model abc that finds and compares the number. my logic: when no entry is found here $eyed is empty. function validateEyed($data) { $eyed = $this-find(array('eyed' = $data), array('eyed')); if(empty($eyed) == true) return $eyed['abc']; return false; } I am calling this function from my controller function __createEyed() { do{ $test=rand(1,2); } while ($eyed = $this-abc-validateEyed($test) == true); return $test; } cakes writes those rand numbers 1 or 2 multiple times in my table. so where is the mistake?? thx :) On 10 Aug., 18:39, cricket zijn.digi...@gmail.com wrote: On Tue, Aug 10, 2010 at 12:30 PM, Tomfox Wiranata tomfox.wiran...@gmail.com wrote: thx to both.. :) @cricket: am i getting it right? you are writing all numers from the database into an array and compare both? is it possible that at a certain point it will get to big or too much data? cause my range starts at 1 and ends at 9 if there are 50million in the table they all get saved in an array Yes, that's correct. I should have pointed that out. You could cache the results, of course, but perhaps a better way would be to pass $new_number to a protected method (that does a find() and comparison) inside the while section. You'd have to have the separate method because while() isn't a block, where you can have several lines of code. Something like: public function getNewNumber() { do { $new_number = rand(10,10); } while ($this-_testNewNumber($new_number)); return $new_number; } protected function _testNewNumber($x) { // return $x exists in DB } Check out the new CakePHP Questions sitehttp://cakeqs.organd help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group athttp://groups.google.com/group/cake-php?hl=en Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
Re: coding a loop in cake?
just the structure of UUIDs with 32 byte is nout suited for my purpose...but thx anyway :) so my hope is still that someone is identifiying my mistake from the code a wrote, i guess.. On 11 Aug., 12:14, Jeremy Burns | Class Outfit jeremybu...@classoutfit.com wrote: Check the Cake section on UUIDs first to see if it is a possible solution for you - it might not be, of course. They are usually used when the field is an auto incrementing primary key - I am not sure if they are flexible enough to work as non primary keys. http://book.cakephp.org/view/1016/Using-UUIDs-as-Primary-Keys Jeremy Burns Class Outfit jeremybu...@classoutfit.comhttp://www.classoutfit.com On 11 Aug 2010, at 11:05, Tomfox Wiranata wrote: thx for your reply jeremy. i dont have an unique index for that field. at least i didnt remember doing that.. how do i create this UUID in xampp and what do I have to change in my code? thx :) On 11 Aug., 11:50, Jeremy Burns | Class Outfit jeremybu...@classoutfit.com wrote: Do you have a unique index on the field? If you haven't and you really do want it to be unique, I'd stick on on. Then you could work on the basis that you *probably* aren't going to run into the same number that often (at least it is far more likely that your number is unique than it is a duplicate), generate a random number, save it and only create another one of the save fails (then rinse and repeat). Would a UUID work for you? That would save you some effort. Jeremy Burns Class Outfit jeremybu...@classoutfit.comhttp://www.classoutfit.com On 11 Aug 2010, at 10:18, Tomfox Wiranata wrote: ok i tried your solution and somehow cake does not care if it writes the same number over and over in my database. maybe you see what's wrong here :) my function in my model abc that finds and compares the number. my logic: when no entry is found here $eyed is empty. function validateEyed($data) { $eyed = $this-find(array('eyed' = $data), array('eyed')); if(empty($eyed) == true) return $eyed['abc']; return false; } I am calling this function from my controller function __createEyed() { do{ $test=rand(1,2); } while ($eyed = $this-abc-validateEyed($test) == true); return $test; } cakes writes those rand numbers 1 or 2 multiple times in my table. so where is the mistake?? thx :) On 10 Aug., 18:39, cricket zijn.digi...@gmail.com wrote: On Tue, Aug 10, 2010 at 12:30 PM, Tomfox Wiranata tomfox.wiran...@gmail.com wrote: thx to both.. :) @cricket: am i getting it right? you are writing all numers from the database into an array and compare both? is it possible that at a certain point it will get to big or too much data? cause my range starts at 1 and ends at 9 if there are 50million in the table they all get saved in an array Yes, that's correct. I should have pointed that out. You could cache the results, of course, but perhaps a better way would be to pass $new_number to a protected method (that does a find() and comparison) inside the while section. You'd have to have the separate method because while() isn't a block, where you can have several lines of code. Something like: public function getNewNumber() { do { $new_number = rand(10,10); } while ($this-_testNewNumber($new_number)); return $new_number; } protected function _testNewNumber($x) { // return $x exists in DB } Check out the new CakePHP Questions sitehttp://cakeqs.organdhelp others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group athttp://groups.google.com/group/cake-php?hl=en Check out the new CakePHP Questions sitehttp://cakeqs.organd help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group athttp://groups.google.com/group/cake-php?hl=en Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at
Re: coding a loop in cake?
On Wed, Aug 11, 2010 at 4:18 AM, Tomfox Wiranata tomfox.wiran...@gmail.com wrote: ok i tried your solution and somehow cake does not care if it writes the same number over and over in my database. maybe you see what's wrong here :) my function in my model abc that finds and compares the number. my logic: when no entry is found here $eyed is empty. function validateEyed($data) { $eyed = $this-find(array('eyed' = $data), array('eyed')); if(empty($eyed) == true) return $eyed['abc']; return false; } This part is trippin me up... You are checking that $eyed is empty and if it is you are returning an element from it? Is that right or are my eyes just seeing thigs? I am calling this function from my controller function __createEyed() { do{ $test=rand(1,2); } while ($eyed = $this-abc-validateEyed($test) == true); return $test; } cakes writes those rand numbers 1 or 2 multiple times in my table. so where is the mistake?? thx :) On 10 Aug., 18:39, cricket zijn.digi...@gmail.com wrote: On Tue, Aug 10, 2010 at 12:30 PM, Tomfox Wiranata tomfox.wiran...@gmail.com wrote: thx to both.. :) @cricket: am i getting it right? you are writing all numers from the database into an array and compare both? is it possible that at a certain point it will get to big or too much data? cause my range starts at 1 and ends at 9 if there are 50million in the table they all get saved in an array Yes, that's correct. I should have pointed that out. You could cache the results, of course, but perhaps a better way would be to pass $new_number to a protected method (that does a find() and comparison) inside the while section. You'd have to have the separate method because while() isn't a block, where you can have several lines of code. Something like: public function getNewNumber() { do { $new_number = rand(10,10); } while ($this-_testNewNumber($new_number)); return $new_number; } protected function _testNewNumber($x) { // return $x exists in DB } Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
Re: coding a loop in cake?
nabil you are right...i realized it too and returned a string taken or free...it works now.. i am saying: $eyed = $this-find(array('eyed' = $data), array('eyed')); if(empty($eyed) == true){ return free; }else{ return taken; and in my controller do{ $lkbl_eyed=rand(1,5); } while ($eyed = $this-Linkable-validateEyed($lkbl_eyed) == taken); return $lkbl_eyed; } now its finebut an endless loop is created when all numbers are taken.he is not ending the loop then...any idea how i can cake to take a break if all numbers are taken? thx On 11 Aug., 17:28, Nabil Alsharif bli...@gmail.com wrote: On Wed, Aug 11, 2010 at 4:18 AM, Tomfox Wiranata tomfox.wiran...@gmail.com wrote: ok i tried your solution and somehow cake does not care if it writes the same number over and over in my database. maybe you see what's wrong here :) my function in my model abc that finds and compares the number. my logic: when no entry is found here $eyed is empty. function validateEyed($data) { $eyed = $this-find(array('eyed' = $data), array('eyed')); if(empty($eyed) == true) return $eyed['abc']; return false; } This part is trippin me up... You are checking that $eyed is empty and if it is you are returning an element from it? Is that right or are my eyes just seeing thigs? I am calling this function from my controller function __createEyed() { do{ $test=rand(1,2); } while ($eyed = $this-abc-validateEyed($test) == true); return $test; } cakes writes those rand numbers 1 or 2 multiple times in my table. so where is the mistake?? thx :) On 10 Aug., 18:39, cricket zijn.digi...@gmail.com wrote: On Tue, Aug 10, 2010 at 12:30 PM, Tomfox Wiranata tomfox.wiran...@gmail.com wrote: thx to both.. :) @cricket: am i getting it right? you are writing all numers from the database into an array and compare both? is it possible that at a certain point it will get to big or too much data? cause my range starts at 1 and ends at 9 if there are 50million in the table they all get saved in an array Yes, that's correct. I should have pointed that out. You could cache the results, of course, but perhaps a better way would be to pass $new_number to a protected method (that does a find() and comparison) inside the while section. You'd have to have the separate method because while() isn't a block, where you can have several lines of code. Something like: public function getNewNumber() { do { $new_number = rand(10,10); } while ($this-_testNewNumber($new_number)); return $new_number; } protected function _testNewNumber($x) { // return $x exists in DB } Check out the new CakePHP Questions sitehttp://cakeqs.organd help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group athttp://groups.google.com/group/cake-php?hl=en Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
Re: coding a loop in cake?
This whole process strikes me as very inefficient. There is no mechanism to prevent your do-while loop from checking the same value an infinite number of times (unless you log each random value and check the next random value against the log AND the db). I don't know what the purpose of the random value is in your app, but I would suggest an entirely different approach. If you need random values in the range of min through max, you could generate a table ahead of time consisting of the fields id and random_number, with max- min+1 rows. (There are many random number generator sites available, and you might be able to access one of their APIs as needed.) It seems to me that it would be easier to generate these unique values in some random order ahead of time, and then just associate the random_numbers table with your current table through a 1-1 relationship. This process would make it easy to identify the next random number to be assigned, and when the random numbers have been exhausted. Just a thought. On Aug 11, 2:57 pm, Tomfox Wiranata tomfox.wiran...@gmail.com wrote: nabil you are right...i realized it too and returned a string taken or free...it works now.. i am saying: $eyed = $this-find(array('eyed' = $data), array('eyed')); if(empty($eyed) == true){ return free; }else{ return taken; and in my controller do{ $lkbl_eyed=rand(1,5); } while ($eyed = $this-Linkable-validateEyed($lkbl_eyed) == taken); return $lkbl_eyed; } now its finebut an endless loop is created when all numbers are taken.he is not ending the loop then...any idea how i can cake to take a break if all numbers are taken? thx Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
Re: coding a loop in cake?
On Wed, Aug 11, 2010 at 4:45 PM, McBuck DGAF mcbuckd...@gmail.com wrote: This whole process strikes me as very inefficient. There is no mechanism to prevent your do-while loop from checking the same value an infinite number of times (unless you log each random value and check the next random value against the log AND the db). I don't know what the purpose of the random value is in your app, but I would suggest an entirely different approach. If you need random values in the range of min through max, you could generate a table ahead of time consisting of the fields id and random_number, with max- min+1 rows. (There are many random number generator sites available, and you might be able to access one of their APIs as needed.) It seems to me that it would be easier to generate these unique values in some random order ahead of time, and then just associate the random_numbers table with your current table through a 1-1 relationship. This process would make it easy to identify the next random number to be assigned, and when the random numbers have been exhausted. Just a thought. What McBuck said. I've given you a solution but you're implementing it in a very strange way. FWIW, this: while ($eyed = $this-Linkable-validateEyed($lkbl_eyed) == taken); is always going to return true. Why are you assigning a value to $eyed, which is never used, in any case? That assignment is what is screwing things up. Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
Re: coding a loop in cake?
You need to set the number before you validate the data... so you could add that loop to the beforeValidate callback to set the proper number. The downside to that is validating a model will actually change it's state too, which might no be expected. On Tue, Aug 10, 2010 at 9:18 AM, Tomfox Wiranata tomfox.wiran...@gmail.com wrote: hi, i need to check if a specific, randomly generated number already exists in the database. when it does exist, a new number needs to be created and checked too. until there is a number, that doesn't exist already.. the check in my model for existance: function validatenumber($data) { $test = $this-find(array('number' = $data), array('user_id')); if(empty('test') == true) return true; return false; } now i can make a working check, but only once in my controller: $x=rand(10,10); if($test=$this-Linkable-validatenumber($x) == true) { return $x: }else{ } but how can i make a loop to create numbers as long as one is not already taken? is there sth like a for loop? havent seen anything like that yet thank you so much :) Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
Re: coding a loop in cake?
On Tue, Aug 10, 2010 at 10:18 AM, Tomfox Wiranata tomfox.wiran...@gmail.com wrote: hi, i need to check if a specific, randomly generated number already exists in the database. when it does exist, a new number needs to be created and checked too. until there is a number, that doesn't exist already.. the check in my model for existance: function validatenumber($data) { $test = $this-find(array('number' = $data), array('user_id')); if(empty('test') == true) return true; return false; } now i can make a working check, but only once in my controller: $x=rand(10,10); if($test=$this-Linkable-validatenumber($x) == true) { return $x: }else{ } but how can i make a loop to create numbers as long as one is not already taken? is there sth like a for loop? havent seen anything like that yet Use the do {} while() construct. public function getNewNumber() { /* fetch all existing numbers */ $numbers = $this-find( 'list', array( 'fields' = array( 'Linkable.number' ) ) ); /* create new number; check exists; repeat if necessary */ do { $new_number = rand(10,10); } while (in_array($new_number, array_values($numbers)); return $new_number; } So, in the controller, you could assign the number with a single call to $this-Linkable-getNewNumber() and be done with it. I didn't quite understand your find() code. I think what I have above should maybe work, though. Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
Re: coding a loop in cake?
thx to both.. :) @cricket: am i getting it right? you are writing all numers from the database into an array and compare both? is it possible that at a certain point it will get to big or too much data? cause my range starts at 1 and ends at 9 if there are 50million in the table they all get saved in an array thx On 10 Aug., 18:17, cricket zijn.digi...@gmail.com wrote: On Tue, Aug 10, 2010 at 10:18 AM, Tomfox Wiranata tomfox.wiran...@gmail.com wrote: hi, i need to check if a specific, randomly generated number already exists in the database. when it does exist, a new number needs to be created and checked too. until there is a number, that doesn't exist already.. the check in my model for existance: function validatenumber($data) { $test = $this-find(array('number' = $data), array('user_id')); if(empty('test') == true) return true; return false; } now i can make a working check, but only once in my controller: $x=rand(10,10); if($test=$this-Linkable-validatenumber($x) == true) { return $x: }else{ } but how can i make a loop to create numbers as long as one is not already taken? is there sth like a for loop? havent seen anything like that yet Use the do {} while() construct. public function getNewNumber() { /* fetch all existing numbers */ $numbers = $this-find( 'list', array( 'fields' = array( 'Linkable.number' ) ) ); /* create new number; check exists; repeat if necessary */ do { $new_number = rand(10,10); } while (in_array($new_number, array_values($numbers)); return $new_number; } So, in the controller, you could assign the number with a single call to $this-Linkable-getNewNumber() and be done with it. I didn't quite understand your find() code. I think what I have above should maybe work, though. Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
Re: coding a loop in cake?
On Tue, Aug 10, 2010 at 12:30 PM, Tomfox Wiranata tomfox.wiran...@gmail.com wrote: thx to both.. :) @cricket: am i getting it right? you are writing all numers from the database into an array and compare both? is it possible that at a certain point it will get to big or too much data? cause my range starts at 1 and ends at 9 if there are 50million in the table they all get saved in an array Yes, that's correct. I should have pointed that out. You could cache the results, of course, but perhaps a better way would be to pass $new_number to a protected method (that does a find() and comparison) inside the while section. You'd have to have the separate method because while() isn't a block, where you can have several lines of code. Something like: public function getNewNumber() { do { $new_number = rand(10,10); } while ($this-_testNewNumber($new_number)); return $new_number; } protected function _testNewNumber($x) { // return $x exists in DB } Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
Re: coding a loop in cake?
wow your goodi'll give it a shot thx. i appreciate it :) On 10 Aug., 18:39, cricket zijn.digi...@gmail.com wrote: On Tue, Aug 10, 2010 at 12:30 PM, Tomfox Wiranata tomfox.wiran...@gmail.com wrote: thx to both.. :) @cricket: am i getting it right? you are writing all numers from the database into an array and compare both? is it possible that at a certain point it will get to big or too much data? cause my range starts at 1 and ends at 9 if there are 50million in the table they all get saved in an array Yes, that's correct. I should have pointed that out. You could cache the results, of course, but perhaps a better way would be to pass $new_number to a protected method (that does a find() and comparison) inside the while section. You'd have to have the separate method because while() isn't a block, where you can have several lines of code. Something like: public function getNewNumber() { do { $new_number = rand(10,10); } while ($this-_testNewNumber($new_number)); return $new_number; } protected function _testNewNumber($x) { // return $x exists in DB } Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups CakePHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en