practicallyalex Posted May 6, 2014 Share Posted May 6, 2014 Im having some trouble finishing this code. This is the index.php <!DOCTYPE html> <html> <head> <title>Your own do-while</title> <link type='text/css' rel='stylesheet' href='style.css'/> </head> <body> <?php $rollcount = 0; do { $roll = rand(1,6); $rollcount++; if($roll = 1){ echo " <div class=\"dice\">1</div>"; } if($roll = 2); { echo "<div class=\"dice\">2</div>"; } if($roll = 3);{ echo "<div class=\"dice\">3</div>"; } if($roll = 4); { echo "<div class=\"dice\">4</div>"; } if($roll = 5); { echo "<div class=\"dice\">5</div>"; } if($roll = 6); { echo "<div class=\"dice\">6</div>"; } if ($roll = 3);{ echo "<p>There {$verb} {$rollcount} {$last}!</p>"; end($roll) } } while($roll);{ $verb = "were"; $last = "rolls"; if($rollcount = 1);{ $verb = "was"; $last = "roll"; } } ?> </body> </html> and this is the style.css .dice { height: 50px; width: 50px; border-radius: 10px; background-color: white; text-align: center; font-weight: bold; font-family: sans-serif; color: black; margin: 10px; display: inline-block; line-height: 50px; font-size: 20px; } Please help. Its a roll the dice game im working on for VERY basic coding and i cant figure it out Quote Link to comment https://forums.phpfreaks.com/topic/288285-basic-php-help/ Share on other sites More sharing options...
practicallyalex Posted May 6, 2014 Author Share Posted May 6, 2014 Sorry this is an add on, im learning this on codecademy in do your own do-while exercise Quote Link to comment https://forums.phpfreaks.com/topic/288285-basic-php-help/#findComment-1478399 Share on other sites More sharing options...
ginerjm Posted May 6, 2014 Share Posted May 6, 2014 What is the trouble? Quote Link to comment https://forums.phpfreaks.com/topic/288285-basic-php-help/#findComment-1478401 Share on other sites More sharing options...
practicallyalex Posted May 6, 2014 Author Share Posted May 6, 2014 (edited) It wont show the output. Like im expecting it to roll a die as many times as it can until it hits 3 then i want it to display the shape of a die with numbers on them. Then i want it to say There were (rollcount) rolls. EDIT 1: I have a little white output box that shows what im echoing and my style output EDIT 2: It thinks its an infinite loop so im trying to figure out when it hits 3 to end the program Edited May 6, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/288285-basic-php-help/#findComment-1478405 Share on other sites More sharing options...
practicallyalex Posted May 6, 2014 Author Share Posted May 6, 2014 (edited) one more thing sorry. I revised it and it accepts it but its not displaying. <!DOCTYPE html><html> <head> <title>Your own do-while</title> <link type='text/css' rel='stylesheet' href='style.css'/> </head> <body> <?php //write your do-while loop below $rollcount = 0 do { $roll = rand(1,6); $rollcount++; if($roll = 1){ echo " <div class=\"coin\">1</div>"; } if($roll = 2); { echo "<div class=\"coin\">2</div>"; } if($roll = 3);{ echo "<div class=\"coin\">3</div>"; } if($roll = 4); { echo "<div class=\"coin\">4</div>"; } if($roll = 5); { echo "<div class=\"coin\">5</div>"; } if($roll = 6); { echo "<div class=\"coin\">6</div>"; } } else if($roll = 3); { echo "<p>There {$verb} {$rollcount} {$last}!</p>"; } while($roll);{ $verb = "were"; $last = "rolls"; if($rollcount = 1);{ $verb = "was"; $last = "roll" }} ?> </body></html> and all it says is unexpected T_DO on line 11 Edited May 6, 2014 by practicallyalex Quote Link to comment https://forums.phpfreaks.com/topic/288285-basic-php-help/#findComment-1478412 Share on other sites More sharing options...
ginerjm Posted May 6, 2014 Share Posted May 6, 2014 Please use code tags to post your code. Quote Link to comment https://forums.phpfreaks.com/topic/288285-basic-php-help/#findComment-1478416 Share on other sites More sharing options...
gizmola Posted May 6, 2014 Share Posted May 6, 2014 Let's try some proper indentation and see what we have: <!DOCTYPE html> <html> <head> <title>Your own do-while</title> <link type='text/css' rel='stylesheet' href='style.css'/> </head> <body> <?php $rollcount = 0; do { $roll = rand(1,6); $rollcount++; if($roll = 1) { echo " <div class=\"dice\">1</div>"; } if($roll = 2); { echo "<div class=\"dice\">2</div>"; } if($roll = 3); { echo "<div class=\"dice\">3</div>"; } if($roll = 4); { echo "<div class=\"dice\">4</div>"; } if($roll = 5); { echo "<div class=\"dice\">5</div>"; } if($roll = 6); { echo "<div class=\"dice\">6</div>"; } if ($roll = 3);{ echo "<p>There {$verb} {$rollcount} {$last}!</p>"; end($roll) } } } while($roll); { $verb = "were"; $last = "rolls"; if ($rollcount = 1); { $verb = "was"; $last = "roll"; } } ?> </body> </html> Hopefully you should see that you have blocks ended improperly, loops with statement end characters (the ";") and a function call (end) without a statement separator, that is also not appropriate whatsoever, as it as it works on arrays, which you aren't using. In short, right now the code you provided has syntax errors, so it's not actually running, nor will it until you fix those errors. Specifically let's start with: if($roll = 2); { echo "<div class=\"dice\">2</div>"; } Do you see the problem? How about this: if ($roll = 3);{ echo "<p>There {$verb} {$rollcount} {$last}!</p>"; end($roll) } } Same problem, but also you have a random end-block "}") in there. And to make matters worse, you are trying to create a string with variables that are not initialized and have no value yet, as they don't appear in your code until after the do-while loop has finished. First thing to do is clean up your code so it's actually syntactically correct. It's really important to have an editor that helps you with proper indentation of blocks, or this type of mistake will creep into your code. Just to throw in my 2 cents, but if this assignment doesn't prohibit it, your code would be much cleaner with a switch statement instead of the repetetive set of mutually exclusive if $roll = ... blocks. Quote Link to comment https://forums.phpfreaks.com/topic/288285-basic-php-help/#findComment-1478417 Share on other sites More sharing options...
ginerjm Posted May 6, 2014 Share Posted May 6, 2014 You have many problems with this code as Gizmola says. See my notes. <!DOCTYPE html> <html> <head> <title>Your own do-while</title> <link type='text/css' rel='stylesheet' href='style.css'/> </head> <body> <?php //write your do-while loop below $rollcount = 0 do // you have no condition on this do loop. INFINITE LOOP { $roll = rand(1,6); $rollcount++; if($roll = 1) // BAD IF COMPARISON - SHOULD USE == HERE { echo " <div class=\"coin\">1</div>"; } if($roll = 2); // BAD STATMENT - IT ENDS HERE AND SERVES NO PURPOSE { echo "<div class=\"coin\">2</div>"; } if($roll = 3); // SAME { echo "<div class=\"coin\">3</div>"; } if($roll = 4); // SAME { echo "<div class=\"coin\">4</div>"; } if($roll = 5); // SAME { echo "<div class=\"coin\">5</div>"; } if($roll = 6); // SAME { echo "<div class=\"coin\">6</div>"; } } // PLUS - YOU REALLY SHOULD LEARN HOW TO USE SINGLE AND DOUBLE QUOTES TO MAKE // YOUR TYPING EASIER // THIS ELSE IS OUTSIDE OF YOU 'DO' LOOP ??? else if($roll = 3); // SAME { echo "<p>There {$verb} {$rollcount} {$last}!</p>"; } // THIS WHILE IS A NEW LOOP while($roll); // SAME { $verb = "were"; $last = "rolls"; if($rollcount = 1); // SAME { $verb = "was"; $last = "roll" } } ?> </body> </html> You have some logic errors as well but you have enough to work on for now. Quote Link to comment https://forums.phpfreaks.com/topic/288285-basic-php-help/#findComment-1478423 Share on other sites More sharing options...
gizmola Posted May 6, 2014 Share Posted May 6, 2014 Oh yeah what ginerjm ^^^^^^ stated as well, which I neglected to state. Set a variable value: $foo = 3; Logically test for equivalence: if ($foo == 3) Oldest mistake in the book: if ($foo = 3) // well $foo sure does == 3 now!, and that's going to be true as well! All I can say is that all these mistakes have been made by lots of other people before you, so don't feel bad. Quote Link to comment https://forums.phpfreaks.com/topic/288285-basic-php-help/#findComment-1478426 Share on other sites More sharing options...
practicallyalex Posted May 6, 2014 Author Share Posted May 6, 2014 Well im only on basic coding. So i cant do much but try to learn. Can you post how i can fix it with the current setup i have Quote Link to comment https://forums.phpfreaks.com/topic/288285-basic-php-help/#findComment-1478433 Share on other sites More sharing options...
ginerjm Posted May 6, 2014 Share Posted May 6, 2014 I gave you all kinds of corrections to be made. Just do it? Quote Link to comment https://forums.phpfreaks.com/topic/288285-basic-php-help/#findComment-1478435 Share on other sites More sharing options...
practicallyalex Posted May 6, 2014 Author Share Posted May 6, 2014 Yeah but you misunderstand what you see here is the extent of how much i know how to do in PHP so fixing it would be extreemly helpful. Quote Link to comment https://forums.phpfreaks.com/topic/288285-basic-php-help/#findComment-1478441 Share on other sites More sharing options...
gizmola Posted May 6, 2014 Share Posted May 6, 2014 We gave you specific fixes to get your code running. Make those changes to the best of your ability and post the results. If you make the effort people here tend to continue to support you. If not, they probably won't. Quote Link to comment https://forums.phpfreaks.com/topic/288285-basic-php-help/#findComment-1478443 Share on other sites More sharing options...
ginerjm Posted May 6, 2014 Share Posted May 6, 2014 Do you plan on learning how to program without reading a single manual or educational resource ever? Quote Link to comment https://forums.phpfreaks.com/topic/288285-basic-php-help/#findComment-1478473 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.