BAM! Custom Dice Roller anyone?

This is where you share YOUR scripts with others

Moderators: macek, egami, gesf

New php-forum User
New php-forum User
Posts: 67
Joined: Tue Jan 24, 2012 1:54 pm

BAM! Custom Dice Roller anyone?

Postby xeniar » Sat Feb 04, 2012 4:15 pm

NOTE: There are no coded into this because since you can specify a custom number for the number of sides a die has then, well, that would require a TON of images..... sorry, don't have the time. :D

Here it is:

(unless you change the filename in both displayRollForm() AND reRollForm() you MUST name this file diceroller.php)

Code: Select all

if(!class_exists(dice_roller)) {
   class dice_roller {
      private $_rollspecs = array();
      private $_rolldata = array();
      public function rollDice($numrolls,$numdice,$numsides,$numsides_custom){
         if($numsides == "Custom"){ $sides = $numsides_custom; }
         else{ $sides = $numsides; }
               $rolldata[$x + 1][] = rand(1,$sides);
         $this->_rollspecs = array(
         $this->_rolldata = $rolldata;

      public function displayRolldata($sortby,$maxwidth) {
         $column = 0;
         echo "<table>";
         if($sortby == "roll"){$title = "Roll";$subtitle="Dice";}
         elseif($sortby == "die"){
                  $rolldata[$x][] = $this->rolldata[$y][$x];
            }$title = "Dice";$subtitle="Roll";
            $this->_rolldata = $rolldata;}
         foreach($this->_rolldata as $roll_number=>$dice){
            if($column == 0){ echo "<TR>"; } //Start a new row every 5 columns
            echo "<td>";
            echo "<table border=1 cellpadding=2>
                     <th align='center' colspan='2'>$title #$roll_number</th>
            foreach($dice as $die_number=>$roll){
               echo "<tr><td align='center'>$subtitle #".($die_number+1).": </td><td align='center'>$roll</td></tr>";
            echo "</td></tr></table>";
            if($column == $maxwidth){
               echo "</tr>";
               $column = 0;
            } else {
         echo "</table>";

      function displayRollForm(){

      $commonsides = array(0=>"4","6","8","10","12","20","Custom");
         echo "<form method='post' action='diceroller.php'>
      <table border='0'>
          <td>Number of Rolls: </td>
          <td><input name='numrolls' type='text' size='3' maxlength='3'/></td>
          <td>Number of Dice per Roll: </td>
          <td><input name='numdice' type='text' size='3' maxlength='3'/></td>
          <td>Number of Sides per Dice: </td>
          <td align='right'><select name='numsides'>";
            foreach($commonsides as $val){
               echo "<option value=$val>$val</option>";
            echo "</select>
              Custom Number: <br><input name='numsides_custom' type='text' size='3' maxlength='3'/></td>
              Sort by:
             <select name='sortby'>
                <option value='roll'>Sort by Roll #</option>
                <option value='die'>Sort by Dice #</option>
          <td colspan='2' align='center'><input type='submit' name='Submit' value='Submit' />
              <input type='reset' name='reset' value='Reset' /></td>


      function reRollForm(){
         echo "<form method='post' action='diceroller.php'>
            <input type='hidden' name='numrolls' value='".$_POST['numrolls']."'>
            <input type='hidden' name='numdice' value='".$_POST['numdice']."'>
            <input type='hidden' name='numsides' value='".$_POST['numsides']."'>
            <input type='hidden' name='numsides_custom' value='".$_POST['numsides_custom']."'>
            <input type='hidden' name='sortby' value='".$_POST['sortby']."'>
            <input type='submit' name='Submit' value='Re-Roll'>
            <a href='diceroller.php'>Change Roll Details</a><br><br>
$roller = new dice_roller;
} else {

By default, it displays up to 10 rolls/dice per row but you can change that to whatever you want. Just change the "10" in this line to whatever you want: $roller->displayRollData($_POST['sortby'],"10");


Return to “Your Scripts”

Who is online

Users browsing this forum: No registered users and 1 guest