I am trying to make a program that can get data from Sql using php. the received data is a number from 1 to 5 and each number presents a color. every time a number is received a counter adds 1 for that color in html. I have been able to code this but if the page is refreshed the counted value becomes 0.
<?php
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)) {
echo $row['name'].", ".$row['color']";
$red = "5";
$pink = "4";
$yellow = "3";
$black = "2";
$white = "1";
$colorid = $row['color'];
if ($colorid == $red){
echo "Red";
$re = 1;
$re = ++;
} elseif ($colorid == $pink){
echo "Pink";
$pi = 1;
$pi = ++;
} elseif ($colorid == $yellow){
echo "Yellow";
$ye = 1;
$ye = ++$;
} elseif ($colorid == $black){
echo "Black";
$blk = 1;
$blk = ++;
} elseif ($colorid == $white){
echo "White";
$wh = 1;
$wh = ++;
} else {
echo "Cannot verify the color code";
}
}
?>
<div>
<span>White: </span><input name="white" value="<?php echo (isset($wh))?$wh:'';?>">
<span>Black: </span><input name="black" value="<?php echo (isset($blk))?$blk:'';?>">
<span>Yellow: </span><input name="yellow" value="<?php echo (isset($ye))?$ye:'';?>">
<span>Pink: </span><input name="pink" value="<?php echo (isset($pi))?$pi:'';?>">
<span>Red : </span><input name="red" value="<?php echo (isset($re))?$re:'';?>">
</div>
Related
Well, I have created a simple higher/lower game script. Its working fine, but I want to limit the user actions to 3. This means that the user will have to guess the number with 3 moves. On third action I will execute query and save the user to mysql. How I can limit the actions/moves?
<?php
session_start();
function Start_Again() {
$number = rand(1,100);
$_SESSION['higherlower'] = $number;
echo "Select a Number below.";
Display_Form();
}
function Display_Form() {
echo "<table>";
for ($num=1;$num < 101;$num++) {
if (!preg_match("/(.*?)0/", $num)) { echo "<td><a href=\"?number=".$num."\">".$num."</td>"; }
else { echo "<td><a href=\"?number=".$num."\">".$num."</td></tr><tr>"; }
}
echo "</table>";
}
if (isset($_GET['number'])) {
$User_Number = $_GET['number'];
$Actual_Number = $_SESSION['higherlower'];
$count = 0;
if ($User_Number < $Actual_Number) { echo "Higher"; $count + 1; Display_Form(); }
elseif ($User_Number > $Actual_Number) { echo "Lower"; $count + 1; Display_Form(); }
elseif ($User_Number == $Actual_Number) { echo "Bingo, Correct Guess!<br>"; Start_Again(); }
echo $count;
}elseif (!isset($_POST['higherlower'])) { Start_Again(); }
?>
When you initialize the game, simply store the amount of tries in your session.
$_SESSION['tries'] = 3;
Then, when the user picks a number, lower the tries and check if it's 0.
$_SESSION['tries']--;
if ($_SESSION['tries'] <= 0) {
die("Enough! You've been clicking numbers all afternoon.");
}
Full Implementation
<?php
/**
* Higher-lower game
*/
if (!isset($_SESSION)) {
session_start();
}
function Start_Again() {
$number = rand(1,100);
$_SESSION['higherlower'] = $number;
$_SESSION['tries'] = 3;
echo "Select a Number below.";
Display_Form();
}
function Display_Form() {
echo "<table>";
$chunks = array_chunc(range(1, 100), 10);
foreach ($chunks as $chunk) {
echo "<tr>";
foreach ($chunk as $num) {
echo "<td><a href=\"?number=".$num."\">".$num."</td>";
}
echo "</tr>";
}
echo "</table>";
}
if (isset($_GET['number'])) {
$User_Number = $_GET['number'];
$Actual_Number = $_SESSION['higherlower'];
if ($_SESSION['tries'] <= 0) {
die("Oops! You're bad at this!");
}
if ($User_Number < $Actual_Number) { echo "Higher"; $count + 1; Display_Form(); }
elseif ($User_Number > $Actual_Number) { echo "Lower"; $count + 1; Display_Form(); }
elseif ($User_Number == $Actual_Number) { echo "Bingo, Correct Guess!<br>"; Start_Again(); }
$_SESSION['tries']--;
echo $_SESSION['tries'] . 'chances left';
} elseif (!isset($_POST['higherlower'])) {
Start_Again();
}
You are already starting a session so you can store the value in a session variable i.e. $_SESSION['count'];
session_start();
if( !isset( $_SESSION['count] ) ) $_SESSION['count'] = 1;
Later on in the code you will need to update the counter $_SESSION['count']++;
And check whether the person has used up all the guesses
if( $_SESSION['count'] > 3 ) { ..... }
I have 5 variables that generate a random number and a sixth variable which is the users input.
Then I check to see if users input $userNum matches any of the random numbers. I know it's a dumb game, but I'm just messing around to learn more PHP
There has to be an easier way to do this.
if(isset($_POST['submit']))
{
$userNum = $_POST['userNum'];
$spot1 = rand(1, 100);
$spot2 = rand(1, 100);
$spot3 = rand(1, 100);
$spot4 = rand(1, 100);
$spot5 = rand(1, 100);
echo $spot1 ."<br>" .$spot2 ."<br>" .$spot3 ."<br>" .$spot4 ."<br>" .$spot5;
if($userNum == $spot1)
{
echo "you hit a mine!";
exit();
}
if($userNum == $spot2)
{
echo "you hit a mine!";
exit();
}
if($userNum == $spot3)
{
echo "you hit a mine!";
exit();
}
if($userNum == $spot4)
{
echo "you hit a mine!";
exit();
}
if($userNum == $spot5)
{
echo "you hit a mine!";
exit();
} else {
echo "you lived!";
}
}
You don't need to store spots in an array or anything like that just use a simple loop.
<?php
if(isset($_POST['submit'])){
$userNum = (int) $_POST['userNum'];
$hitMine = false;
for($i = 1; $i <= 5; $i++){
$randNum = rand(1, 100);
echo $randNum . '<br />';
if($randNum == $userNum){
$hitMine = true;
}
}
if($hitMine == true){
echo "you hit a mine!";
}
}
?>
I would make an array of the spots
$spot1 = rand(1, 100);
$spot2 = rand(1, 100);
$spot3 = rand(1, 100);
$spot4 = rand(1, 100);
$spot5 = rand(1, 100);
// Make an array of the spots.
$spots = array($spot1, $spot2, $spot3, $spot4, $spot5);
if(in_array($userNum, $spots)) {
echo "you hit a mine!";
exit();
} else {
echo "you lived!";
}
For 50 or more spots you can dynamicaly insert the values in the array assuming you use the rand() function in the real php-code:
$spots = Array();
for ($i = 0; $i < 50; $i ++) {
array_push($spots, rand(1,100));
}
or:
for ($i = 0; $i < 50; $i ++) {
$spots[$i] = rand(1,100);
}
You can use Switch Case in place of if else to make it better and quick.
if(isset($_POST['submit']))
{
$userNum = $_POST['userNum'];
$spot1 = rand(1, 100);
$spot2 = rand(1, 100);
$spot3 = rand(1, 100);
$spot4 = rand(1, 100);
$spot5 = rand(1, 100);
echo $spot1 ."<br>" .$spot2 ."<br>" .$spot3 ."<br>" .$spot4 ."<br>" .$spot5;
Switch($userNum)
{
Case $spot1:
Case $spot2:
Case $spot3:
Case $spot4:
Case $spot5:
echo "you hit a mine!";
break;
default: echo "you lived!";
break;
}
}
Just store the valid spots in an array.
$myhashmap = array();
$myhashmap['spot1'] = true;
$myhashmap['spot2'] = true;
if(isset($myhashmap[$userNum] ) )
{
echo "you hit a mine!";
exit();
}
Here's a link for more info about PHP arrays: http://www.tutorialspoint.com/php/php_arrays.htm
Ok so im having trouble with a project im working on. Its going minesweeper in php but in the bases of the game I can't get the different cell blocks to store their information. Ive tried using session and get, but i can't seem to figure out how to store the information or where to put the code. Below is the basic game code: any ideas?
updated: I really appreciate the help I've been trying to tackle this for awhile now. I am unsure of where to place some of the session values. This does not look right to me:
<html>
<body>
<?php
// ~~~~~~~~~ VARIABLES ~~~~~~~~~~
session_start();
$_SESSION[$piece] = $piece;
$default_board = "default_board";
$filename = "currentproject.php";
$width = "20px";
$height = "20px";
$y_coord = 50;
$how_many_spots = 25;
echo "<table border=1>";
$_SESSION['clicks'] = array();
for ($y=24;$y!=0;--$y) {
$y_coord += $height;
$x_coord = 50;
for ($x=24;$x!=0;--$x){
$x_coord += $width; $cell_sum = $x + $y; $remainder = $cell_sum%2; // some math
if (isset($piece[$x][$y])) {
$background_color = "#991122"; //shows a mine
} else if ($remainder == 0) {
$background_color = "#CCCCCC";
} else $background_color = "#AAAAAA";
echo "<div onclick=\"javascript:document.location.href='$filename?xcord=$x&ycord=$y';\" style=\"border: 1px solid black; background-color: $background_color; position: absolute;left: $x_coord; top: $y_coord; width: $width; height: $height;\">";
echo "</div>";
$_SESSION['clicks'][$xcord][$ycord] = False;
$_SESSION['clicks'][$_GET['xcord']][$_GET['ycord']] = True;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ MINES GO HERE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
while ($how_many_spots!=0){
$i = rand(1,24);
$e = rand(1,24);
if (!isset($piece[$i][$e])){
$piece[$i][$e] = "mine";
--$how_many_spots;
}
}
}
print ("<br>");
}
$_GET['xcord'] = $xcord;
$_GET['ycord'] = $ycord;
?>
</body>
</html>
It could be done using the $_SESSION and $_GET variables. Following is a working example. It was fun to work out and I hope it helps. Note that the code for alternating grey boxes is not quite right and I did not fix it. I also added options for grid size and number of mines as playing with a 25 x 25 grid gave 625 boxes with 25 mines - a rather difficult chance of survival. I defaulted the grid to 4x4 with 1 mine (I like winning) and you can of course change that.
<html>
<head>
<script type="text/javascript">
function gameover($won){
if ($won){
alert("Game Over - You won");
}
else {
alert("Game Over - You lost");
}
}
</script>
</head>
<?php
session_start();
$filename = $_SERVER['PHP_SELF'];
$gridsize = 4;
$nummines = 1;
if (isset($_SESSION['piece'])){
// board has been set up previously - game in progress
if (isset($_GET['new_game'])){
// start a new game
unset($_SESSION['piece']);
}
}
if (isset($_SESSION['grid_size'])){
// game in progress
$gridsize = $_SESSION['grid_size'];
$nummines = $_SESSION['num_mines'];
}
if (isset($_GET['grid_size'])){
// New Game - set from form choice
$gridsize = $_GET['grid_size'];
$nummines = $_GET['num_mines'];
$_SESSION['grid_size'] = $gridsize;
$_SESSION['num_mines'] = $nummines;
}
?>
<body>
<div>
<form name="minesweeper" action="<?php echo $filename ?>" method="GET">
Enter Grid Size: <input type="input" value="<?php echo $gridsize ?>" name="grid size">
Enter Number of Mines: <input type="input" value="<?php echo $nummines ?>" name="num_mines">
<input type="submit" value="New Game" name="new_game">
</form>
</div>
<?php
// ~~~~~~~~~ VARIABLES ~~~~~~~~~~
$default_board = "default_board";
$width = "20px";
$height = "20px";
$y_coord = 50;
$how_many_spots = $gridsize;
echo "<table border=1>";
if (isset($_SESSION['piece'])){
// board has been set up previously - game in progress
if (isset($_GET['x'])){
// if cell clicked
if ($_SESSION['piece'][$_GET['x']][$_GET['y']] == "mine"){
// if mine - game over
$game_over = true;
$_SESSION['piece'][$_GET['x']][$_GET['y']] = "explode";
}
else {
// mark clicked
$_SESSION['piece'][$_GET['x']][$_GET['y']] = "clicked";
}
}
// draw board
$clickcounter = 0;
for ($y=$gridsize;$y!=0;--$y) {
$y_coord += $height;
$x_coord = 50;
for ($x=$gridsize;$x!=0;--$x){
$x_coord += $width; $cell_sum = $x + $y; $remainder = $cell_sum%2; // some math
if ($_SESSION['piece'][$x][$y] === "explode") {
$background_color = "Black";
$game_over = true;
}
elseif ($_SESSION['piece'][$x][$y] === "clicked") {
// clicked
$background_color = "Green";
$clickcounter++;
}
elseif ($_SESSION['piece'][$x][$y] === "mine" && isset($game_over)) {
//shows all mines when game over
$background_color = "#991122";
}
elseif ($remainder == 0) {
$background_color = "#CCCCCC";
}
else {
$background_color = "#AAAAAA";
}
echo "<div onclick=\"javascript:document.location.href='$filename?x=$x&y=$y';\" style=\"border: 1px solid black; background-color: $background_color; position: absolute;left: $x_coord; top: $y_coord; width: $width; height: $height;\">";
echo "</div>";
}
}
if (isset($game_over)){
echo "<script language='JavaScript'>gameover(false)</script>";
}
elseif ($clickcounter >= ($gridsize * $gridsize) - $nummines){
echo "<script language='JavaScript'>gameover(true)</script>";
}
}
else{
for ($y=$gridsize;$y!=0;--$y) {
$y_coord += $height;
$x_coord = 50;
for ($x=$gridsize;$x!=0;--$x){
$x_coord += $width; $cell_sum = $x + $y; $remainder = $cell_sum%2; // some math
// shows a mine for debugging
if (isset($_SESSION['piece'][$x][$y]) && $_SESSION['piece'][$x][$y]=="mine") {
if ($remainder == 0){
$background_color = "#CCCCCC";
}
else {
$background_color = "#AAAAAA";
}
}
elseif ($remainder == 0) {
$_SESSION['piece'][$x][$y] = false;
$background_color = "#CCCCCC";
}
else {
$_SESSION['piece'][$x][$y] = false;
$background_color = "#AAAAAA";
}
echo "<div onclick=\"javascript:document.location.href='$filename?x=$x&y=$y';\" style=\"border: 1px solid black; background-color: $background_color; position: absolute;left: $x_coord; top: $y_coord; width: $width; height: $height;\">";
echo "</div>";
// ~~~~~~~~~MINES GO HERE ~~~~~
while ($nummines!=0){
$i = rand(1,$gridsize);
$e = rand(1,$gridsize);
$_SESSION['piece'][$i][$e] = "mine";
--$nummines;
}
}
print ("<br>");
}
}
?>
</body>
</html>
I want to make something like a anti spam system, I've got this HTML:
What is <?php echo $six; ?> + <?php echo $rand1; ?> <input type="text" name="human" id="human">
And for these variables:
$human = #$_POST['human'];
$rand1 = rand(1, 9);
$six = 6;
$res = $rand1 + $six;
Then I do:
if($human==$res){
echo "Correct";
}else{
echo "Incorrect";
}
This is not working! Any ideas?
Remember to provide your $res or $rand1 variable value via your html form - changing your php to something like this:
$human = #$_POST['human'];
$res = #$_POST['res'];
if($human==$res){
echo "Correct";
}else{
echo "Incorrect";
}
$rand1 = rand(1, 9);
$six = 6;
$res = $rand1 + $six;
Then adding:
<input type="hidden" name="res" value="<php echo $res;?>">
into your html form
I have an array for flash cards, and using shuffle I am outputting 15 unique cards, 3 each for 5 different categories.
What I want to do is create these card sets for about a dozen people on the same web page, but the part I can't figure out is how to make it so each complete set is unique and doesn't repeat from a set given to any other user.
A short code sample with a brief explanation would be the most helpful to me.
Here is the code I modified to my needs. Not much changed really.
<?php
/* original source:
* 3d10-engine.php
* by Duane Brien
*/
if (empty($_POST)) {
for ($i = 1; $i < 16; $i++) {
$numbers['ALL'][] = $i;
}
$picks = array();
$letters = array ('ALL');
foreach ($letters as $letter) {
for ($i = 0;$i < 10;$i++) {
shuffle($numbers[$letter]);
$chunks = array_chunk($numbers[$letter], 5);
$cards[$i][$letter] = $chunks[0];
if ($letter == 'N') {
$cards[$i][$letter][2] = ' '; // Free Space
}
}
foreach ($numbers[$letter] as $number) {
$balls[] = $letter.$number;
}
shuffle($balls);
}
$cardsstr = serialize($cards);
$ballsstr = serialize($balls);
$picksstr = serialize($picks);
} else {
$cards = unserialize($_POST['cardsstr']);
$balls = unserialize($_POST['ballsstr']);
$picks = unserialize($_POST['picksstr']);
array_unshift($picks, array_shift($balls));
echo "<h1>Just Picked: " . $picks[0] . "</h1>";
$cardsstr = serialize($cards);
$ballsstr = serialize($balls);
$picksstr = serialize($picks);
}
?>
Picks : <?php echo implode(',', $picks) ?>
<form method='post'>
<input type='hidden' name='cardsstr' value='<?php echo $cardsstr ?>' />
<input type='hidden' name='ballsstr' value='<?php echo $ballsstr ?>' />
<input type='hidden' name='picksstr' value='<?php echo $picksstr ?>' />
<input type='submit' name='cards' value='next number' />
</form>
Start Over
<?php
foreach ($cards as $card) {
echo "<table border='1'>";
echo "<tr><td>A</td><td>B</td><td>C</td><td>D</td><td>E</td></tr>";
for ($i = 0; $i < 5; $i++) {
echo "<tr><td>" . $card['B'][$i] . "</td><td>" .$card['I'][$i] . "</td><td>" . $card['N'][$i] . "</td>";
echo "<td>" . $card['G'][$i] . "</td><td>" . $card['O'][$i] . "</td></tr>";
}
echo "</table>";
}
?>
Since you have more options in each set, random pick is enough to achieve unique final result.
I mean don't make this thing more complex.
Try this sample
<?php
//Initialize your 5 sets here
$numbers['B'] = range(1,15);
$numbers['I'] = range(16,30);
$numbers['N'] = range(31,45);
$numbers['G'] = range(45,60);
$numbers['O'] = range(61,75);
//My Assumption is you to pick 3 from each
while(TRUE){
$rand = rand(0,5);
if(count($numbers_B) < 3 && !in_array($numbers['B'][$rand]){
$numbers_B[] = $numbers['B'][$rand];
}
$rand = rand(0,5);
if(count($numbers_I) < 3 && !in_array($numbers['I'][$rand]){
$numbers_I[] = $numbers['I'][$rand];
}
$rand = rand(0,5);
if(count($numbers_N) < 3 && !in_array($numbers['N'][$rand]){
$numbers_N[] = $numbers['N'][$rand];
}
$rand = rand(0,5);
if(count($numbers_G) < 3 && !in_array($numbers['G'][$rand]){
$numbers_G[] = $numbers['G'][$rand];
}
$rand = rand(0,5);
if(count($numbers_O) < 3 && !in_array($numbers['O'][$rand]){
$numbers_O[] = $numbers['O'][$rand];
}
if( count($numbers_B) == 3 && count($numbers_I) == 3 && count($numbers_N) == 3 &&
count($numbers_G) == 3 && count($numbers_O) == 3 ){
break;
}
}
$result = $numbers_B + $numbers_I + $numbers_N + $numbers_G + $numbers_O; ?>
Here $result value should be unique, And I consider number of sets is constant. If it is dynamic, then try the same logic with two dimensional array.
Just store the prepared sets in an array and then check each shuffle if it exists in the array using the already (in_array function) or not, if it does then shuffle again.