Jump to content

trying to echo a variable dynamically


matthewst

Recommended Posts

I have a simple game script here http://matthews.gotdns.org/game/test_game.php

 

test_game.php

<?php
mysql_connect("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("test_game") or die(mysql_error());
//error_reporting(E_ALL);
error_reporting(0);
?>

<?php
$query = "SELECT player_name, hit_points FROM users"; 
$result=mysql_query($query);
while ($row = mysql_fetch_assoc($result))
{
$player_name = $row['player_name'];
$hit_points = $row['hit_points'];
if($hit_points <= "0")
{
echo "$player_name is dead&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
}
else
{
echo "Player: $player_name&nbsp";
echo "Hit points: $hit_points&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
}
}
?>
<title>test game</title>
</head>
<body>
<br>
<a href="atk.php?p=2">hit player two</a>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
<a href="atk.php?p=1">hit player one</a>
</body>
</html>

 

atk.php

<?php
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("test_game") or die(mysql_error());
//error_reporting(E_ALL);
error_reporting(0);
?>
<?php
if($_GET['p'] == 2){ 
$sql = "UPDATE users SET hit_points=hit_points-1 WHERE player_name = 'player_2'"; 
mysql_query($sql)or die(mysql_error());
}
if($_GET['p'] == 1){ 
$sql = "UPDATE users SET hit_points=hit_points-1 WHERE player_name = 'player_1'"; 
mysql_query($sql)or die(mysql_error());
}

echo "<script type='text/javascript'>
<!--
window.location = 'test_game.php'
//-->
</script>";
?>

 

what I'm trying to accomplish is:

I have the page loaded on my computer

you have the page loaded on you computer

 

I click "hit_player_2" and the page on your computer is updated (you lose a hit point) automatically without a refresh

 

I don't wan't anyone to write the script for me (I can't learn that way). I just need someone to point me in a direction or give me a few key words to serach for. I've already read the tutorials but couldn't find anything pertaining to this problem.

Link to comment
Share on other sites

I originally used a form but it didn't work as expected. It would take a point away from the wrong player the first time you clicked a button.

 

<?php
mysql_connect("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("test_game") or die(mysql_error());
//error_reporting(E_ALL);
error_reporting(0);
?>
<?php
$query = "SELECT player_name, hit_points FROM users"; 
$result=mysql_query($query);
while ($row = mysql_fetch_assoc($result))
{
$player_name = $row['player_name'];
$hit_points = $row['hit_points'];
if($hit_points <= "0")
{
echo "$player_name is dead&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
}
else
{
echo "Player: $player_name&nbsp";
echo "Hit points: $hit_points&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
}
}
?>
<title>test game</title>
</head>
<body>
<br><form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="FormName"><input name="hit_player_2" type="submit" value="hit_player_2">
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<input name="hit_player_1" type="submit" value="hit_player_1"></form>
<?php
if(isset($_POST['hit_player_1'])){ 
     $sql = "UPDATE users SET hit_points=hit_points-1 WHERE player_name = 'player_1'"; 
     mysql_query($sql)or die(mysql_error());
}
if(isset($_POST['hit_player_2'])){ 
     $sql = "UPDATE users SET hit_points=hit_points-1 WHERE player_name = 'player_2'"; 
     mysql_query($sql)or die(mysql_error());
}
?>
</body>
</html>

Link to comment
Share on other sites

matthewst, your php and mysql look just fine as your tests show. To have this dynamic processing both on the server side, and the client side without a refresh,  you ought to lookup and realy get to know javascript.

 

words to search for are javascript, events, onclick, ajax, xmlhttp;

 

There is a thread with a lot of good code I participated in today that's worth a peek. http://www.phpfreaks.com/forums/index.php/topic,148418.0.html

 

Javascript and the DOM have broken down the elements on a page to the point where one can query the server for a single value (like player1's HP) and replacing the content of an html element -> in this case perhaps, the html element that contains within it the current player1 HP (span, p, div).

Link to comment
Share on other sites

  • 2 weeks later...

ok, heres what i have now

 

needless to say, it doesn't work

 

game.php

<?php
mysql_connect("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("test_game") or die(mysql_error());
//error_reporting(E_ALL);
error_reporting(0);
?>
<?php
$query = "SELECT player_name, hit_points FROM users"; 
$result=mysql_query($query);
while ($row = mysql_fetch_assoc($result))
{
$player_name = $row['player_name'];
$hit_points = $row['hit_points'];
if($hit_points <= "0")
{
echo "$player_name is dead&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
}
else
{
echo "Player: $player_name&nbsp";
echo "Hit points: $hit_points&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
}
}
?>
<head>
<title>test game</title>
<script language="javascript">
    function createRequestObject() {
    
        var req;
    
        if(window.XMLHttpRequest){
            // Firefox, Safari, Opera...
            req = new XMLHttpRequest();
        } else if(window.ActiveXObject) {
            // Internet Explorer 5+
            req = new ActiveXObject("Microsoft.XMLHTTP");
        } else {
            // There is an error creating the object,
            // just as an old browser is being used.
            alert('There was a problem creating the XMLHttpRequest object');
        }
    
        return req;
    
    }
    
    // Make the XMLHttpRequest object
    var http = createRequestObject();


function sendRequest(act) {
        
        // Open PHP script for requests
        http.open('get', 'atk.php?act='+act);
        http.onreadystatechange = handleResponse;
        http.send(null);
    
    }
    
    function handleResponse() {
    
        if(http.readyState == 4 && http.status == 200){
    
            // Text returned FROM PHP script
            var response = http.responseText;
    
            if(response) {
                // UPDATE ajaxTest content
                document.getElementById("atk").innerHTML = response;
                setTimeout(atk,5000);
            }
    
        }
    }

</script>
</head>
<body>
<br>
<a href="atk.php?p=2">hit player two</a>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
<a href="atk.php?p=1">hit player one</a>
<?PHP
function countPosts() {
        sendRequest('atk');
    }
?>

</body>
</html>

 

atk.php

<?php
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("test_game") or die(mysql_error());
//error_reporting(E_ALL);
error_reporting(0);
?>
<?php
if($_GET['p'] == 2){ 
$sql = "UPDATE users SET hit_points=hit_points-1 WHERE player_name = 'player_2'"; 
mysql_query($sql)or die(mysql_error());
}
if($_GET['p'] == 1){ 
$sql = "UPDATE users SET hit_points=hit_points-1 WHERE player_name = 'player_1'"; 
mysql_query($sql)or die(mysql_error());
}

echo "<script type='text/javascript'>
<!--
window.location = 'test_game.php'
//-->
</script>";
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.