Unstoppable Domains โ€” Expired Auctions

PHP game variable issue - values won't update first button press PLZ HELP! Reward!

SpaceshipSpaceship
Watch

omegapandor

Established Member
Impact
4
I'm having a problem. I'm trying to make a game site, but I have been having an issue.

To view the problem, go to www.zombiegate.com and login under:
user: namepros
pass: guest

and click on the game button. To switch games click on find and then switch to another game if you need to. Variables are reset at midnight central time.

I have some game variables that are being displayed on the page. The expected results is: user presses a button, the page refreshes, and the new values are displayed. However, it would seem that the page does not actually refresh until the second button press, and it works from then on. The first press seems to refresh the page, but the new values are not shown.

Maybe there is a way other than ajax where I could show the new values without generating the page again?

I have fixed this on at least one other page by changing the order of the code. This must be avoided, though, since I may want the page to look a certain way, and I don't want to be hampered by something like this. Also
I already tried it on this particular page, and it didn't fix it.

If you help me solve this, you will most definitely get a free premium account or whatever if the site gets up and running one day. Maybe 20 NP$?

The survivor button should do stuff (add one to survivor and subtract one from turns), but the weapons button intentionally does nothing because it seems to refresh the page correctly the first press.
Not sure if the barricade button does anything (it's been a few days).

NOTE: the site is on drupal, and all caching is disabled right now. I would do ASP but I'm not sure if drupal natively supports it (as far as being displayed correctly in the block) yet.

Also, I'm just screwing around with learning php right now. The games direction or style is completely undetermined. That's why it looks like it would suck.

THANK YOU ANYONE THAT HELPS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


:'(

EXTRA INFO: I have already tried forcing the page to refresh after the $_POST of the button. The page simply refreshed again without showing the updated values. Doing it twice had the same result - just look longer!
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
It almost looks like a browser caching issue.
You could try something like...
Code:
<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
?>
At the top of your page.

Also (But not related to the issue at hand), the site is difficult to navigate because it cuts off about 50 pixels on the right-hand side. I can scroll over, but some people might not be so intelligent.


Bruce
 
0
•••
Hmm. I might have to pick a different theme or fix this one. I've never had that problem before, and I've seen the site both on IE and firefox2.0.

What browser are you using?

Thanks - I'll try that trick. I do figure it has something to do with the caching since various other non-updating issues occur with graphics. I currently have firefox set to delete the cache upon exit, but I guess that doesn't really fix the problem for others (although it isn't a problem as long as I don't want to change anything - lol).

Just tried it. Unfortunately, it didn't fix the issue.

I wonder if it's a drupal thing?

PHP:
<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past

$conect_db = mysql_connect("account_information");

if (!$connect_db)
	{
	die('Could not connect: ' . mysql_error());
	}

mysql_select_db("database",$con);

global $user;

//GAME VARIABLES
$r = mysql_query("SELECT * FROM users WHERE name = '$user->name' ");
$z = mysql_query("SELECT * FROM game WHERE id = '$user->gameid' ");

while($row = mysql_fetch_array($r)) {

$name = $row['name'];
$turns = $row['turns'];
$zdollars = $row['money'];
$gid = $row['gameid']; 
}

while($row2 = mysql_fetch_array($z)) {
$followers = $row2['survivors'];
$zombies = $row2['zombies'];
$zpool = $row2['zpool'];
$cades = $row2['barricades'];
}

//POST DATA
if($_POST[sfind])
{
mysql_query("UPDATE game SET survivors = survivors + '1' where id='$gid' ");  
mysql_query("UPDATE users SET turns = turns - '1' where name='$user->name' ");

}
if($_POST[build])
{
mysql_query("UPDATE game SET barricades = barricades + '1' where id='$gid' ");  
mysql_query("UPDATE users SET turns = turns - '1' where name='$user->name' ");
mysql_query("UPDATE game SET zpool = zpool - '3' WHERE id = '$gid' ");
}

echo "<html>
<head>
<style type='text/css'>
a.nav2, a.nav2:link, a.nav2:visited
{
display: block;
width: 100px;
height: 30px;
border: 1px solid #000;
color: #000;
margin-left: 2px;
float: left;
text-align: center;
text-decoration: none;
text-size: 12px;
line-height: 25px;
overflow: hidden;
background: #638F0A;
color: #fff;
}

.submitlink
{
display: block;
width: 150px;
height: 30px;
border: 1px solid #000;
color: #000;
margin-top: 4px;
text-align: center;
text-decoration: none;
text-size: 12px;
line-height: 25px;
cursor: pointer;
cursor: hand;
overflow: hidden;
background: #638F0A;
color: #fff;
}

.submitlink:hover
{
background: #86C20E;
}


</style>
</head>
<body>
<form method = 'post'>
<input class = 'submitlink' type='submit' name='sfind' value='Look for Survivors' />
<input class='submitlink' type='submit' name='findweapon' value='Look for Weapons' />
<input class='submitlink' type='submit' name='build' value='Build Barricade' />
<form><br/><br/><br/><br/>
<a class='nav2' title='Your username'>Survivor: " . $name . "</a>
<a class='nav2' title='Your Game ID'>Game: " . $gid . "</a>
<a class='nav2' title='Your current Zdollars'>ZPool: " . $zpool . "</a>
<a class='nav2' title='Your remaining turns'>Turns: " . $turns . "</a>
<a class='nav2' title='Followers'>Followers: " . $followers . "</a>
<a class='nav2' title='Zombies waiting outside'>Zombies: " . $zombies . "</a>
<a class='nav2' title='Barricades Constructed'>Barricades: " . $cades . "</a>


</body>
</html>";




mysql_close($connect_db);
?>

Here is the code from that page. Note that previously the if $_POST stuff was below the inputs - so that wouldn't fix it (I guess). I have obviously changed some variables and arguments to protect myself somewhat.

My problem wouldn't have to do with echoing everything, would it? I mean - I think php pages in drupal have to be entirely php. Maybe I'm mistaken - though I used to think that it simply meant php was allowed, and I thought I found myself wrong one day.
 
Last edited:
0
•••
Edit: I think I found the error...
You update the database when they do something, but you never update the variables themselves.
You're pulling the old information in the beginning into the variables, updating it in the database, then printing the old information back out.
That's why it isn't updating.

If that still doesn't fix things...

Besides the header (which I haven't really worked with much, but really should), you can try another trick.

You can force the browse to believe it is a different page by having a randomly generated number on the link.

Code:
<li class="menu-122 active"><a href="/maingame<?php=rand(1,100000)?>" title="" class="active">Game</a></li>

Regarding the size of the site: The top banner is 1,000 pixels. For a normal 1024 pixel wide resolution, this is fine. I'm one of those crazy people who moved their window bar to the left of the screen, which takes up about 100 pixels. Despite the fact it makes some websites more difficult to read, I can fit about 20 different windows down without Windows XP trying to group things like Firefox and Word together (Which is ridiculously annoying). I'll screenshot it for you if you'd like.

The point being, anyone with a normal 1024px configuration is going to be fine. Its the 800x800px morons who are going to have the issues.


Bruce
 
Last edited:
0
•••
I've moved some stuff around and I still hasn't fixed it. I wasn't sure what you meant by printing the old information back out, but I did redeclare/assign variables after the changes occur within each if statement. no difference. I tried putting all the echo stuff before the updates. same thing.

Tried inserting headers after the mysql updates and the text would actually update on the second button press and then go back to the old variable before correcting itself again. weird!

Something very important to note: the first time I hit the button, the "this page has been updated" note stays at the top, as if the page hasn't changed at all. Then, the second time when the text changes, that note goes away. So, I know the page isn't refreshing at all with the first button press, even when it's forced to.

Haven't tried the random page trick yet. . .er. . .rather, I just haven't gotten it to work. I'm going to just forgo the whole menu thing (where would I put that? CSS?) and just keep the <a> tag and throw it on my about page or something. I guess I'll have to convert the whole page to php, though, since it didn't recognize the code. . .er. . .maybe not. I'll try changing the input format to php and just inserting the <a> tag.

Back to work.

UPDATE: The weapons button, which has no if($_POST) will correctly refresh the page the first time it is pressed.

UPDATE 2: Now when I hit the survivor button, the page refreshes (the note goes away), but the text doesn't update. hitting one of the buttons updates the text but it's still one value behind. ALSO, hitting the the button without an if($_POST) will restart the process where the survivor button won't do anything the first press.
 
Last edited:
0
•••
Can you post/PM me the new code?


Bruce
 
0
•••
PHP:
<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past

$connt = mysql_connect("account");

if (!$connt)
	{
	die('Could not connect: ' . mysql_error());
	}

mysql_select_db("omegapan_drpl1",$connt);

global $user;

//GAME VARIABLES GAME VARIABLES GAME VARIABLES
$r = mysql_query("SELECT * FROM users WHERE name = '$user->name' ");
$z = mysql_query("SELECT * FROM game WHERE id = '$user->gameid' ");

while($row = mysql_fetch_array($r)) {
$name = $row['name'];
$turns = $row['turns'];
$zdollars = $row['money'];
$gid = $row['gameid']; 
}

while($row2 = mysql_fetch_array($z)) {
$followers = $row2['survivors'];
$zombies = $row2['zombies'];
$zpool = $row2['zpool'];
$cades = $row2['barricades'];
}

echo "<html>
<head>
<style type='text/css'>
a.nav2, a.nav2:link, a.nav2:visited
{
display: block;
width: 100px;
height: 30px;
border: 1px solid #000;
color: #000;
margin-left: 2px;
float: left;
text-align: center;
text-decoration: none;
text-size: 12px;
line-height: 25px;
overflow: hidden;
background: #638F0A;
color: #fff;
}

.submitlink
{
display: block;
width: 150px;
height: 30px;
border: 1px solid #000;
color: #000;
margin-top: 4px;
text-align: center;
text-decoration: none;
text-size: 12px;
line-height: 25px;
cursor: pointer;
cursor: hand;
overflow: hidden;
background: #638F0A;
color: #fff;
}

.submitlink:hover
{
background: #86C20E;
}


</style>
</head>
<body>
<form method = 'post'>
<input class = 'submitlink' type='submit' name='sfind' value='Look for Survivors' />
<input class='submitlink' type='submit' name='findweapon' value='Look for Weapons' />
<input class='submitlink' type='submit' name='build' value='Build Barricade' />
<form><br/><br/><br/><br/>
<a class='nav2' title='Your username'>Survivor: " . $name . "</a>
<a class='nav2' title='Your Game ID'>Game: " . $gid . "</a>
<a class='nav2' title='Your current Zdollars'>ZPool: " . $zpool . "</a>
<a class='nav2' title='Your remaining turns'>Turns: " . $turns . "</a>
<a class='nav2' title='Followers'>Followers: " . $followers . "</a>
<a class='nav2' title='Zombies waiting outside'>Zombies: " . $zombies . "</a>
<a class='nav2' title='Barricades Constructed'>Barricades: " . $cades . "</a>
</body>
</html>";


//POST DATA
if($_POST[sfind])
{
mysql_query("UPDATE game SET survivors = survivors + '1' where id='$gid' ");  
mysql_query("UPDATE users SET turns = turns - '1' where name='$user->name' ");
while($row = mysql_fetch_array($r))
   {
$turns = $row['turns'];
   }

while($row2 = mysql_fetch_array($z)) 
   {
$followers = $row2['survivors'];
   }

}
if($_POST[build])
{
mysql_query("UPDATE game SET barricades = barricades + '1' where id='$gid' ");  
mysql_query("UPDATE users SET turns = turns - '1' where name='$user->name' ");
mysql_query("UPDATE game SET zpool = zpool - '3' WHERE id = '$gid' ");
}



mysql_close($connt);
?>

Not much is changed since nothing worked.

I might still be doing what you mentioned earlier though.
 
0
•••
Code:
<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past

$connt = mysql_connect("account");

if (!$connt)
    {
    die('Could not connect: ' . mysql_error());
    }

mysql_select_db("omegapan_drpl1",$connt);

global $user;



//GAME VARIABLES GAME VARIABLES GAME VARIABLES
$r = mysql_query("SELECT * FROM users WHERE name = '$user->name' limit 1");
$z = mysql_query("SELECT * FROM game WHERE id = '$user->gameid' limit 1");

  $row = mysql_fetch_array($r);
  $name = $row['name'];
  $turns = $row['turns'];
  $zdollars = $row['money'];
  $gid = $row['gameid'];


  $row2 = mysql_fetch_array($z);
  $followers = $row2['survivors'];
  $zombies = $row2['zombies'];
  $zpool = $row2['zpool'];
  $cades = $row2['barricades'];
}


//POST DATA
if($_POST['sfind'])
{
  mysql_query("UPDATE game SET survivors = survivors + '1' where id='$gid' ");  
  mysql_query("UPDATE users SET turns = turns - '1' where name='$user->name' ");
}
if($_POST['build'])
{
  mysql_query("UPDATE game SET barricades = barricades + '1', zpool = zpool - '3'  where id='$gid' ");  
  mysql_query("UPDATE users SET turns = turns - '1' where name='$user->name' ");
}


if ($_POST['sfind'] || $_POST['build']) {
  $r = mysql_query("SELECT * FROM users WHERE name = '$user->name' limit 1");
  $z = mysql_query("SELECT * FROM game WHERE id = '$user->gameid' limit 1");

  $row = mysql_fetch_array($r);
  $name = $row['name'];
  $turns = $row['turns'];
  $zdollars = $row['money'];
  $gid = $row['gameid'];


  $row2 = mysql_fetch_array($z);
  $followers = $row2['survivors'];
  $zombies = $row2['zombies'];
  $zpool = $row2['zpool'];
  $cades = $row2['barricades'];
}


echo "<html>
<head>
<style type='text/css'>
a.nav2, a.nav2:link, a.nav2:visited
{
display: block;
width: 100px;
height: 30px;
border: 1px solid #000;
color: #000;
margin-left: 2px;
float: left;
text-align: center;
text-decoration: none;
text-size: 12px;
line-height: 25px;
overflow: hidden;
background: #638F0A;
color: #fff;
}

.submitlink
{
display: block;
width: 150px;
height: 30px;
border: 1px solid #000;
color: #000;
margin-top: 4px;
text-align: center;
text-decoration: none;
text-size: 12px;
line-height: 25px;
cursor: pointer;
cursor: hand;
overflow: hidden;
background: #638F0A;
color: #fff;
}

.submitlink:hover
{
background: #86C20E;
}


</style>
</head>
<body>
<form method = 'post'>
<input class = 'submitlink' type='submit' name='sfind' value='Look for Survivors' />
<input class='submitlink' type='submit' name='findweapon' value='Look for Weapons' />
<input class='submitlink' type='submit' name='build' value='Build Barricade' />
<form><br/><br/><br/><br/>
<a class='nav2' title='Your username'>Survivor: " . $name . "</a>
<a class='nav2' title='Your Game ID'>Game: " . $gid . "</a>
<a class='nav2' title='Your current Zdollars'>ZPool: " . $zpool . "</a>
<a class='nav2' title='Your remaining turns'>Turns: " . $turns . "</a>
<a class='nav2' title='Followers'>Followers: " . $followers . "</a>
<a class='nav2' title='Zombies waiting outside'>Zombies: " . $zombies . "</a>
<a class='nav2' title='Barricades Constructed'>Barricades: " . $cades . "</a>
</body>
</html>";






mysql_close($connt);
?>

I just had it re-update all the variables if anything was posted. That should work...

A few tips on php/mysql:
When you select data from a database (Like you did in $r and $z), chances are you don't need every field of data (eg, if the table `users` contained their email and password as well as the information you needed). It's better to select only the fields you need.
Code:
mysql_query("SELECT `name`, `turns`, `money`, `gameid` FROM users WHERE name = '$user->name' limit 1");

Also, you're probably only expecting one row of data to be returned (after all, the person playing is probably not two people at once). For this reason, you can add a "limit 1" to the end of your query. Especially if you have a large table, mysql will stop searching as soon as it finds the correct name.
I also recommend doing everything by ID instead of name. Why?
1. People enjoy changing their name on occasion. Make this a possibility.
2. If you're going to be getting data from a table, you need to be using Indexes. Basically, if you have a field "indexed", it saves all that data in the RAM (instead of the disk like all the rest of the fields are stored in) so it is really fast to search for. Sure, you can index name, but names are usually 10-20 characters long. My guess is you won't have more than 1,000,000 users (Sorry, I wish we all could, but we won't), so you can make the ID Int(6). That means mysql stores 3-4x less data in the RAM, making it more efficient and much faster.

Also, since you're only expecting one row of data, you don't need the whole while($row=) thing. Just do a $row=mysql_fetch_array($r);

I also fixed the $_POST[field]s. While php will recognize $_POST[field] as $_POST['field'], it is much slower (php checks to see if field is a constant first, then after it determines there is no field constant, converts it to a string.


Finally, you probably don't even need to do all the original variable setting.
If $user is an object containing all the information, then $gid should just be $user->gameid, correct?
Then, you could wipe out the first set of $r, $z, $row, $row2, and those other assignments.
Set $gid to $user->gameid
Check for the posted data and do your updating with it.
Finally, remove the "if ($_POST['sfind'] || $_POST['build']) {" statement, as you need to set those variables for output later.

I also changed your building update: You can update more than one field in the same mysql query by separating them with commas.


Bruce
 
1
•••
OMFG IT WORKS!!! I don't know which thing you changed did it, but it works perfectly!

30NP$ Donated. You earned it. Rep added, too.

Yeah, I wasn't sure what to do about selecting stuff from the users table. I figured I was probably wasting time and memory, though. In the case of game, however, I use every variable on the table anyway.

I had seen that most places used id instead of name. I knew in the long run it could waste a lot of processor - just hadn't gotten around to trying to implement the id. Most places use $user[id] but I'm not sure that will work in my situation. I guess I could just use $user->id ?

In drupal, changing your name directly changes it in the database so users could still change their name and $user->name would work. In fact, I did it a couple of times.
I still planning on using the obviously faster method, but that might be important to know in the future or something.


I had NO IDEA about the $_POST['var'] vs $_POST[var] thing. I'll use quotes from now on. Thanks!

I never thought to use || either. Is that what fixed the problem? I'm not really getting how if($_POST) can/should be done twice. . . I'm assuming in php the || just means 'or' so I figure that statement just takes away the need for two separate if sections.

I'll probably read your post several more times before I fully understand some of the things. Thank you so much for your help! I'll find happiness/success yet!
 
Last edited:
0
•••
Spaceship
Domain Recover
DomainEasy โ€” Zero Commission
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back