Jump to content

Basic PHP help


Recommended Posts

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
Link to comment
Share on other sites

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 by Ch0cu3r
Link to comment
Share on other sites

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 by practicallyalex
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.