I'm trying to build a browser card game with mostly PHP and maybe some other useful languages thrown in to test and expand my PHP knowledge with something I always wanted to make. But every online solution I've read and tried to implement wouldn't work and I don't know why. I have logic like this:
//find game form
print "<form method='post' action='game.php'>
<label>Enter host generated game #</label><br>
<label><input type='text' name='gamenumber'></label><br>
<label><input type='submit' name='findgame' value='FindGame'></label><br><br>
</form>";
//host game button
print "<form method='post' action='game.php'>
<label><input type='submit' name='hostgame' value='HostGame'></label>
</form>";
//game logic
if(isset($_POST['hostgame']))
{
$roomnumber = rand();
print $roomnumber;
insertField($Ausername,$roomnumber); //insert field GUI
//link game to host for future field printing
$query = "UPDATE game".$roomnumber." SET host='".$Ausername."' WHERE host='null'";
$result = mysqli_query($link, $query);
print "<div id='quote'>"; //populate host just disappears <meta http-equiv='refresh' content='5' />
//populates self and opponents in hosts perspsective
populatehost($roomnumber);
print "</div>";
}
if(isset($_POST['findgame'])) //this processes after user submits data.
{
$roomnumber = $_POST['gamenumber'];
$_SESSION['gamenumber'] = $_POST['gamenumber'];
//link game to find for future field printing
$query = "UPDATE game".$roomnumber." SET find='".$Ausername."'WHERE find='null'";
$result = mysqli_query($link, $query);
print "<form method='post' action='game.php'>
<label><input type='submit' name='startgame' value='StartGame'></label>
</form>";
//populates self and opponents in finds perspsective
populatefind($roomnumber);
}
if(isset($_POST['startgame'])) //this processes after user submits data.
{
print "<br>Game started.<br>";
print "heads or tails?\n\n";
print "<form method='post' action='game.php'>
<label><input type='text' name='headstails'></label><br>
<label><input type='submit' name='select' value='Select'></label><br><br>
</form>";
//finder always starts game
populatefind($_SESSION['gamenumber']);
}
$gameStatus="";
$flip = rand(1,2);
$coin = "heads";
$gameStarted = "game not started";
if($flip==1)
$coin="heads";
else if($flip==2)
$coin="tails";
if(isset($_POST['headstails'])) //this processes after user submits data.
{
if($_POST['headstails']==$coin)
{
print "find game player goes first";
$gameStarted = "game started";
//notify find game player he goes first
//do logic where he goes first
}
else
{
print "host game player goes first";
$gameStarted = "game started";
//notify host game player he goes first
//do logic where he goes first
}
populatefind($_SESSION['gamenumber']);
}
this is the jquery function I tried that I pasted at the bottom of the page.
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js%22%3E%3C/script%3E">
jQuery(function () {
var $els = $('div[id^=quote]'),
i = 0,
len = $els.length;
$els.slice(1).hide();
setInterval(function () {
$els.eq(i).fadeOut(function () {
i = (i + 1) % len
$els.eq(i).fadeIn();
})
}, 2500)
})
</script>
</body>
</html>
so I was able to get the field printed for both the host and finder, and once the finder flips the coin it says who goes first from their perspective.. but I'm having trouble getting that to appear on the other player's perspective. I was thinking of implementing that by refreshing a div with the function that does all the printing, but nothing seemed to work no matter what. I tried the php sleep method in many different ways, I tried some jquery function, nothing worked. And btw populate host and populate find are just functions with giant tables based on arrays of variables.