jetlife76 Posted November 9, 2011 Share Posted November 9, 2011 I am creating a program to simulate the Powerball Lottery game, I have everything working but i forgot to add the Quick Pick Option, having alittle bit of an issue with my "if" statement I believe. My confusion is whether it should be a nested "if", or should it be it's own entity? Here's what i have so far: <?php $intGrandPize = 10000000; $intRegCount = 5; $intMaxReg = 59; $intMaxPB = 39; //$quick = rand(1, 59); //$quickPB = rand(1, 49); if (isset($_POST['Quick Pick'])){ if (count($_POST)>0) { $aryPick = array(); $intPower = 0; for($t=1;$t<=$intRegCount;$t++) { if (isset($_POST['num'.$t])) { $intPick = (int)$_POST['num'.$t]; if ($intPick > 0 && $intPick <= $intMaxReg && !in_array($intPick,$aryPick)) { $aryPick[] = $intPick; } } } if (isset($_POST['Power']) && (int)$_POST['Power'] > 0 && $_POST['Power'] <= $intMaxPB) { $intPower = (int)$_POST['Power']; } if (count($aryPick) < 5 || $intPower == 0) { echo "<font color='red'>'*Five unique numbers and a PowerBall selection are Required*'</font>";; } else { // Have valid numbers... sort($aryPick); // For if you are going to display them, they will be in order... // Pick your winners $aryAllBalls = range(1,$intMaxReg); // array of numbers 1 - 59 shuffle($aryAllBalls); // Randomize their order $aryAllBalls = array_chunk($aryAllBalls,$intRegCount); // The above breaks up into an array with 5 numbers each // [0] contains the first 5 balls of all balls randomized, these are what are "picked" $aryPickedBalls = $aryAllBalls[0]; sort($aryPickedBalls); // Sort them for display $intPowerBall = rand(1,$intMaxPB); echo "YOUR WINNING NUMBERS ARE: <br>",implode(' ',$aryPickedBalls),' PB: ',$intPowerBall,"<br>\n<br>\n"; echo "You Picked: "; foreach($aryPick as $key=>$val) { if (in_array($val,$aryPickedBalls)) { echo '<strong>',$val,'</strong> '; } else { echo $val,' '; unset($aryPick[$key]); // Remove it since it didn't match } } $bMatchPB = ($intPower == $intPowerBall); // Set here since checked in 3 places... if ($bMatchPB) { echo 'PB: <strong>',$intPower,"</strong><br>\n<br>\n"; } else { echo 'PB: '.$intPower,"<br>\n<br>\n"; } // At this point, $aryPick will only contain matching numbers... $intMatches = count($aryPick); echo 'You matched '.$intMatches,' numbers and did '; if (!$bMatchPB) { echo 'not '; } echo "match the PowerBall number.<br><br>\n\n"; // HERE YOU WOULD DO SOMETHING TO SAY HOW MUCH THEY WON if ($intMatches>=3 || $bMatchPB) { $intWinnings = 0; switch ($intMatches) { case 0: if ($bMatchPB) { $intWinnings = 3; } break; case 1: if ($bMatchPB) { $intWinnings = 4; } break; case 2: if ($bMatchPB) { $intWinnings = 7; } break; case 3: if ($bMatchPB) { $intWinnings = 100; } else { $intWinnings = 7; } break; case 4: if ($bMatchPB) { $intWinnings = 10000; } else { $intWinnings = 100; } break; case 5: if ($bMatchPB) { $intWinnings = $intGrandPize; } else { $intWinnings = 200000; } break; default: echo "ERROR: Winning Combination not defined in systen!!!<br>\n"; } echo "<strong>YOU ARE A WINNER!!!!</strong><br>\n"; echo 'You Won: $'.number_format($intWinnings,0),"<br>\n"; } else { echo "Sorry, you didn't win. Try again!<br>\n"; } } // END: if (had valid numbers picked) } // END: There was data posted (ie. Form submitted) ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 9, 2011 Share Posted November 9, 2011 I'm not even going to read through your code. There is a very simple way to get a random pick from a fixed set of numbers using only a few lines of code (aside from defining the available numbers and the number of items to choose) and no loops. I'm not sure I understand your numbers though. I see this $intMaxReg = 59; $intMaxPB = 39; Is that supposed to meant the the 'regular' numbers can be from 1 to 59 and the powerball can be from 1 to 39? That doesn't make sense to me based upon how I understand the powerball lottery to work - which is the powerball can be any number in the total range. So, based "my" understanding I would do this to get a random pick: //Create an array from 1 to $intMaxReg $numberRange = range(1, $intMaxReg); //Randomize the array shuffle($numberRange); //Get 6 numbers from the array $quick_pick = array_slice($numberRange, 0, 6); //Use the first or last number as the powerball If the powerball really is supposed to be limited to only 39 while the rest of the numbers can be up to 59, that is easy enough to do as well. Would just need to know if the powerball selection is separate from the regular numbers. E.g. if the powerball is 5, can 5 also be selected as one of the regular numbers. Quote Link to comment Share on other sites More sharing options...
jetlife76 Posted November 9, 2011 Author Share Posted November 9, 2011 Ok the range of the powerball is actually 49, just noticed i had 39, the first 5 numbers' range is 59 which cannot be duplicates, the powerball can be a duplicate of one of the regular numbers Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 9, 2011 Share Posted November 9, 2011 OK, I just read through your code a little bit and see you are doing something similar, but I'm confused as to what your problem is. What is it that is not working as you want it - you have several if() statements, so not sure which one you are even referring to. Quote Link to comment Share on other sites More sharing options...
jetlife76 Posted November 9, 2011 Author Share Posted November 9, 2011 I have a quick pick button on my front page that should give me random numbers in my output as the user's selections. I also have list boxes for the user to select their numbers manually. (if (isset($_POST['Quick Pick'])){) on line 9 i think for you, wondering if i could just nest everything in it or would i have to do it seperately, if that makes since? Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 9, 2011 Share Posted November 9, 2011 If the quick pick field is a checkbox, then yes, you can just do an isset check. I would do in if/else. If quick pick is selected determine the picks programatically, else use the selections the user selected. THEN, after you have determined the user selections, you would compare against the lottery selections. It looks like you are only comparing against the lotter selections if the user did a quick pick. If you are not getting the results you expect, echo the pertinent variables to the page to verify that the values are what you expect. I think you really need to pull out a sheet of paper and figure out your logic, then go back and code it. The flow of your code seems pretty haphazard. For example: if (isset($_POST['Quick Pick'])){ if (count($_POST)>0) { What is the second if() condition for? If the first condition is true, then the second conditions will always be true. Quote Link to comment Share on other sites More sharing options...
jetlife76 Posted November 9, 2011 Author Share Posted November 9, 2011 if (isset($_POST['Quick Pick'])){ was supposed to be commented out, sorry. Quote Link to comment Share on other sites More sharing options...
jetlife76 Posted November 9, 2011 Author Share Posted November 9, 2011 I have it set up to recieve the users' input manually as of now. i want to know where i would validate that the quick pick was selected and have it perform the same tasks to randomize the numbers and display them. Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 9, 2011 Share Posted November 9, 2011 I have it set up to recieve the users' input manually as of now. i want to know where i would validate that the quick pick was selected and have it perform the same tasks to randomize the numbers and display them. As I said, you should work out the logic on paper - THEN write your code. I would think the logic should go somethign like this: if(isset($_POST['Quick Pick'])) { //Dynamically define user picks } else { //Define user picks from POST data } //Validate that user picks are valid (could be invalid if user did not use quick pick) if($userPicks) { //Compare user picks against lottery picks } Quote Link to comment Share on other sites More sharing options...
jetlife76 Posted November 9, 2011 Author Share Posted November 9, 2011 Ok thanks for your help, since i don't have the time to re-write my code, i guess i'll just tinker with what i got till i get it working Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 9, 2011 Share Posted November 9, 2011 Ok thanks for your help, since i don't have the time to re-write my code, i guess i'll just tinker with what i got till i get it working You don't have that much code! You should never "tinker" with code in the hopes of getting the results you want. That is a recipe for disaster. If you don't "know" what your code is doing and why you will likely have bugs that you don't even know exist. Quote Link to comment Share on other sites More sharing options...
xyph Posted November 9, 2011 Share Posted November 9, 2011 Ok thanks for your help, since i don't have the time to re-write my code, i guess i'll just tinker with what i got till i get it working You've pretty much said "I don't have time to do this right, so I'm going to half ass it till it works" If that's your attitude, why bother asking for help? We're here to help teach you how to do things the right way. If you don't care about that, why waste your time? If this is homework that you really don't feel like doing yourself, then disguise it as a job and pay someone to do it. Good luck on the exam though Quote Link to comment Share on other sites More sharing options...
jetlife76 Posted November 9, 2011 Author Share Posted November 9, 2011 Wow!!!! thanks for the lecture , but i am not half assing it, i have a million other things to do and i figure that there is an easier fix than re-writing my entire code . Great attitude by the way i'm sure everyone that gets on here wants to be chastised!! just because i didnt take your suggestions doesnt mean that you can coy with me. But you have a great day Quote Link to comment Share on other sites More sharing options...
xyph Posted November 9, 2011 Share Posted November 9, 2011 You've got a bad attitude. You come here and ask for help. We tell you that you're on the wrong track, and that there's a lot of conflicting and erroneous logic in your code. This isn't the answer you want to hear, so you dismiss it. Do you really act this way in real life? When someone gives you advice you requested, you tell them 'Oh, okay, well I can't be bothered to apply your advice, thanks anyways!' Do you realize you've wasted their time? Do you realize they are only telling you this to help you avoid future problems, and headaches down the line? If you want to use our experience and knowledge to learn how to program properly, please continue to post. If you want to just get things to work in a timely manner, hire someone who already knows what they're doing. We don't care about your deadlines. We aren't here to make sure your script is done on time. We are here to help you learn the language and how to apply that knowledge. If you don't care to learn, we don't care to help. And for the record, you ARE half-assing it. I don't want to attack you personally, but I don't appreciate being flat-out lied to. Again, we really do want to help you, but only if you are ready to learn. Quote Link to comment Share on other sites More sharing options...
jetlife76 Posted November 9, 2011 Author Share Posted November 9, 2011 First of all i dont have a bad attitiude, i simply stated that i would try to find another way to do the code. i understand the purpose of getting help from this site, but to assassinate my character is very unprofessional. I never said anything about you catering to my deadline, but for me to rewrite my entire code the way you want it done is going to take far more time than i have at the moment. Next time you decide to evaluate someone or their character know what you are talking about. and for the record you never answered my question. Quote Link to comment Share on other sites More sharing options...
xyph Posted November 9, 2011 Share Posted November 9, 2011 I never said anything about you catering to my deadline, but for me to rewrite my entire code the way you want it done is going to take far more time than i have at the moment. Time is an issue, but there's no deadline to meet? Please elaborate. If there is no deadline, and time is still an issue, I read it as "I can't be bothered to learn how to do this properly. Can't you see I'm busy?" If that isn't a bad attitude in a place of learning, then I guess I don't know what is. Would you prefer for me not to speak up? I've been on this board for quite some time, and I see threads like this all the time. You know what happens when volunteers read responses like yours? They move on. The thread dies. It may get bumped a couple times, but those with experience will simply move on to someone eager to learn to do things right, and not just do things quickly. I'm trying to help you get help. Why take offense? Don't get defensive at negative comments. Consider the source, analyze what was said, and if you can't find any truth to it, ignore it. On that note, I'm done with this thread. You show no desire to learn. I have not assassinated your character, I have simply pointed out how others will interpret your attitude. I said nothing that you haven't made apparent in your posts yourself. Quote Link to comment Share on other sites More sharing options...
jetlife76 Posted November 9, 2011 Author Share Posted November 9, 2011 WOW really? why are you still on the time thing. ok here it is in simple terms since you feel like im such a slacker and want to reword what i am saying: i am trying to see if there is a way to do this without rewriting everything and if i cannot find another solution then i will reference the advice i was given here. Is that clear enough for you. Quote Link to comment Share on other sites More sharing options...
xyph Posted November 9, 2011 Share Posted November 9, 2011 I'm still on the time thing because our time is volunteered. If you don't have time to learn to code our way, why should we have time to try to understand how you've decided to lay out your code, and come up with a solution that applies to it? Mentioning anything about time is extremely offensive to me. In a way, it's saying your time is more important than mine. You are busy, therefor, we must cater to you. I'm not saying that's how you meant it, I'm saying that's how I interpret it. I hope you understand this. There is always a way to make it work. You seem to be checking for the 'Quick Pick' posted variable, but then do nothing about it. Are you completely lost, or have you just not shown any attempted code? In Reply #1, mjdamato has given you a great snippet that will generate 6 random numbers in a range without repeating. Have you tried applying that snippet? Is there something in that snippet your don't understand? Quote Link to comment Share on other sites More sharing options...
jetlife76 Posted November 9, 2011 Author Share Posted November 9, 2011 ok if you could explain to me ,the way to make it work that would be great, if not then i'll keep looking at his, but my question with his snippet is do i define the user picks(Quick Pick) the same way i defined the random winning numbers? and where in my code would i place it because ive tried numerous ways and the Quick Pick button does the same as when i submit the manual picks. it does not randomly assign the numbers Quote Link to comment Share on other sites More sharing options...
jetlife76 Posted November 9, 2011 Author Share Posted November 9, 2011 i also think it has something to do with my front page because i have it all on one Notepad++ page. <hr> <p>Pick 5 unique numbers!!</p> <form action="" method="post"> <?php for($t=1;$t<=$intRegCount;$t++): ?> <select name="num<?php echo $t; ?>"> <option value="0">Pick</option> <?php for($n=1;$n<=$intMaxReg;$n++): ?> <?php if (isset($_POST['num'.$t]) && $_POST['num'.$t]==$n): ?> <option selected="selected" value="<?php echo $n; ?>"><?php echo $n; ?></option> <?php else: ?> <option value="<?php echo $n; ?>"><?php echo $n; ?></option> <?php endif; ?> <?php endfor; ?> </select> <?php endfor; ?> PB <select name="Power"> <option value="0">Pick</option> <?php for($n=1;$n<=$intMaxPB;$n++): ?> <?php if (isset($_POST['Power']) && $_POST['Power']==$n): ?> <option selected="selected" value="<?php echo $n; ?>"><?php echo $n; ?></option> <?php else: ?> <option value="<?php echo $n; ?>"><?php echo $n; ?></option> <?php endif; ?> <?php endfor; ?> </select> <br><br> <input type="submit" name="submit" value="PLAY THESE NUMBERS!"> <p> OR</p> <input type="submit" value="Quick Pick"> <p> *Randomly Generates numbers </p> </form> Quote Link to comment Share on other sites More sharing options...
xyph Posted November 9, 2011 Share Posted November 9, 2011 You need to look more closely at your markup. Accuracy is extremely important in programming <input type="submit" value="Quick Pick"> How will PHP reference that input? Quote Link to comment Share on other sites More sharing options...
jetlife76 Posted November 9, 2011 Author Share Posted November 9, 2011 That's another issue im having, i understand how i did it for the user picked numbers and powerball, but it's kinda got me stuck on how to have php reference it, (syntax) Quote Link to comment Share on other sites More sharing options...
jetlife76 Posted November 9, 2011 Author Share Posted November 9, 2011 for the quick pick button sorry my cpu froze on me Quote Link to comment Share on other sites More sharing options...
xyph Posted November 9, 2011 Share Posted November 9, 2011 If you don't want to try, then I don't really want to help. I am saying this under the assumption that the code you've posted is indeed yours. If you have written that code, and can't figure out the problem when it's isolated to a single 41 character line, then you aren't trying. If the code isn't yours, then I would like to direct you to the Third Party Scripts forum. This one is meant to ask for help with your code. Your <input> has no name attribute. Though this isn't a required attribute, it becomes very hard to reference a value that has no name. Quote Link to comment Share on other sites More sharing options...
jetlife76 Posted November 9, 2011 Author Share Posted November 9, 2011 I wasnt asking you how to do it bro, i was just saying i was having a lil trouble writing the syntax. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.