Hello,
I'm trying to create a dice roller, that saves past rolls in a database. Then shows them on the screen as it refreshes each roll. This is as far as i've been able to get. I can get teh roller to work, where it shows the roll, but that's it. >.<. I'd appreciate any and all help. I'm a beginner at php,. and am probaly doing it completely wrong.
<?php
$location = "xxxxx";
$username = "xxxxx";
$password = "xxxxx";
$database = "louisvtm_dice";
$conn = mysql_connect("$location","$username","$password");
if (!$conn) die ("Could not connect MySQL");
mysql_select_db($database,$conn) or die ("Could not open database");
mysql_query("INSERT INTO roll (roll) values ('$action')");
mysql_query("INSERT INTO roll (roll) values ('$dice')");
mysql_query("INSERT INTO roll (roll) values ('$diff')");
/*mysql_query("INSERT INTO roll (roll) VALUES ('.branden.')");
mysql_query("CREATE TABLE user5 (
firstname VARCHAR(20),
surname VARCHAR(20),
location VARCHAR(20))");*/
?>
<?php
$_POST[dice] = $dice;
$str = ':dice roll ' . $dice . 'd10:';
$str = preg_replace('~:dice roll (.*)(>*):~ei', "rolldice('\$1','\$2')", $str);
function rolldice($dice,$sides)
{
$diff = $_POST[diff];
$success = 0;
$total = 0;
$temp = '';
for($i=0; $i < $dice; $i++)
{
$dv=rand(1,10);
$temp .= '' . (NULL) . '' . $dv . ',';
if ($dv > $diff - 1)
{$success = $success + 1;};
if ($dv == 10)
{$success = $success;};
if ($dv == 1)
{$success = $success - 1;};
}
if ($success < 0) {$temp .= 'BOTCH!';}
elseif ($success == 0) {$temp .= 'Failure';}
else $temp .= '<br /><br /><FONT color="CA9803" size="4" FACE="Century Gothic"><b>Difficulty:</font> <font color="#FFFFBE">' . $diff . '</font></b><br /><br /><b><FONT color="#CA9803" size="4" FACE="Century Gothic">Successes: </font></b><b><font color="#FFFFBE">' . $success . '</font></b>';
return $temp;
}
echo '
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Rollertitle>
<link rel="stylesheet" type="text/css" href="style/pop_style.css" />
<META http-equiv="imagetoolbar" CONTENT="no">
<STYLE TYPE="text/css">
<!-- /* $WEFT -- Created by: Louis (xxxxxxxxxxxxxx) on 2/8/2004 -- */
@font-face {
font-family: Docu;
font-style: normal;
font-weight: normal;
src: url(pages/DOCU0.eot);
}
@font-face {
font-family: Century Gothic;
font-style: normal;
font-weight: normal;
src: url(pages/CENTURY0.eot);
}
-->
</STYLE>
</head>
<script type="text/javascript" language="JavaScript1.2" src="js/pop_events.js"></script>
<body bgcolor="#000000" text="#FFFFFF">
<center>
<p><FONT color="#FE0202" size="+4" FACE="Docu">Dice Roller</font></p>
</center>
<table width="100%" align="center" bgcolor="#000000">
<tr>
<td align="center"><p><FONT color="CA9803" size="4" FACE="Century Gothic">You rolled: <font color="#0000FF">'.$str. '</font></p>
</td>
</tr><tr>
<td align="center"><FONT color="CA9803" size="4" FACE="Century Gothic"><br /><br />Make another roll:</font></td>
</tr>
</table>
<form enctype="multipart/form-data" action="dice.php" method="POST" name="dice">
<table bgcolor="#000000" align="center">
<tr>
<td><FONT color="#A70000" size="4" FACE="Century Gothic">Action <input type="text" name="action" size="2"></font></td>
<td><FONT color="#A70000" size="4" FACE="Century Gothic">How many dice to roll? <input type="text" name="dice" size="2"></font></td>
<td><font color="#A70000" size="4" FACE="Century Gothic">Enter the difficulty: <input type="text" name="diff" size="2"></font></td>
<td align="center" colspan="2"><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</head>';
?>
<p><font face="Docu" color="#FFFFFF">Previous Rolls:</font></p>
<blockquote>
<?php
// Query the Database
$query = mysql_query("SELECT * FROM roll"); // replace table with your table name
?>
<?php
while($row = mysql_fetch_array($query)) { // put results into an array
echo "Name: " . $row["reason"] . "<br>"; // (assuming field name in db is 'name'
echo "Email: " . $row["roll"] . "<br>"; }// assuming field name in db is email)
// and so forth
{
// close the while loop
// Display the text of each joke in a paragraph
while ( $row = mysql_fetch_array($result) ) {
echo' "<p>" . $row["roll"] . "</p>"';
{
?>