Jump to content

Recommended Posts

So I wrote this code, what its supposed to do is draw an image on a grid space, based on its X,Y coordinates from a mySql database, compared to the player X,Y.  Heres the code:

 

<table>
<tr>
<td>
<?php
//northwest
echo "nw";
$x1 = $_SESSION[x] - 1;
$y1 = $_SESSION[y] + 1;
$hasobject1 = mysql_query("SELECT * FROM objects WHERE x='$x1' && y='$y1'");
while($row = mysql_fetch_array($hasobject1))
{
$image = "space_228.gif";
$image = $row[image];
}
if(!empty($image)){
echo "<IMG SRC=$image WIDTH=64 HEIGHT=64 onerror='onImgError(this)>'";
}
else{
echo "<IMG SRC='space_228.gif' WIDTH=64 HEIGHT=64>";
}
?></td>
<td><?php
//north
echo "n";
$x2 = $_SESSION[x];
$y2 = $_SESSION[y] + 1;
$hasobject2 = mysql_query("SELECT * FROM objects WHERE x='$x2' && y='$y2'");
while($row = mysql_fetch_array($hasobject2))
{
$image = "space_228.gif";
$image = $row[image];
}
if(!empty($image)){
echo "<IMG SRC=$image WIDTH=64 HEIGHT=64 onerror='onImgError(this)>'";
}
else{
echo "<IMG SRC='space_228.gif' WIDTH=64 HEIGHT=64>";
}
?></td>
<td><?php
//northeast
echo "ne";
$x3 = $_SESSION[x] + 1;
$y3 = $_SESSION[y] + 1;
$hasobject3 = mysql_query("SELECT * FROM objects WHERE x='$x3' && y='$y3'");
while($row = mysql_fetch_array($hasobject3))
{
$image = "space_228.gif";
$image = $row[image];
}
if(!empty($image)){
echo "<IMG SRC=$image WIDTH=64 HEIGHT=64 onerror='onImgError(this)>'";
}
else{
echo "<IMG SRC='space_228.gif' WIDTH=64 HEIGHT=64>";
}
?></td>
</tr>
<tr>
<td><?php
//wesr
echo "w";
$x4 = $_SESSION[x] - 1;
$y4 = $_SESSION[y];
$hasobject4 = mysql_query("SELECT * FROM objects WHERE x='$x4' && y='$y4'");
while($row = mysql_fetch_array($hasobject4))
{
$image = "space_228.gif";
$image = $row[image];
}
if(!empty($image)){
echo "<IMG SRC=$image WIDTH=64 HEIGHT=64 onerror='onImgError(this)>'";
}
else{
echo "<IMG SRC='space_228.gif' WIDTH=64 HEIGHT=64>";
}
?></td>
<td><?php
echo "p";
//player
echo "<IMG SRC='spaceship_wire5.jpg' WIDTH=64 HEIGHT=64>";
?></td>
<td><?php
//east
echo "e";
$x5 = $_SESSION[x] + 1;
$y5 = $_SESSION[y];
$hasobject5 = mysql_query("SELECT * FROM objects WHERE x='$x5' && y='$y5'");
while($row = mysql_fetch_array($hasobject5))
{
$image = "space_228.gif";
$image = $row[image];
}
if(!empty($image)){
echo "<IMG SRC=$image WIDTH=64 HEIGHT=64 onerror='onImgError(this)>'";
}
else{
echo "<IMG SRC='space_228.gif' WIDTH=64 HEIGHT=64>";
}
?></td>
</tr>
<tr>
<td><?php
//southwest
echo "sw";
$x6 = $_SESSION[x] - 1;
$y6 = $_SESSION[y] - 1;
$hasobject6 = mysql_query("SELECT * FROM objects WHERE x='$x6' && y='$y6'");
while($row = mysql_fetch_array($hasobject6))
{
$image = "space_228.gif";
$image = $row[image];
}
if(!empty($image)){
echo "<IMG SRC=$image WIDTH=64 HEIGHT=64 onerror='onImgError(this)>'";
}
else{
echo "<IMG SRC='space_228.gif' WIDTH=64 HEIGHT=64>";
}
?></td>
<td><?php
//south
echo "s";
$x7 = $_SESSION[x];
$y7 = $_SESSION[y] - 1;
$hasobject7 = mysql_query("SELECT * FROM objects WHERE x='$x7' && y='$y7'");
while($row = mysql_fetch_array($hasobject7))
{
$image = "space_228.gif";
$image = $row[image];
}
if(!empty($image)){
echo "<IMG SRC=$image WIDTH=64 HEIGHT=64 onerror='onImgError(this)>'";
}
else{
echo "<IMG SRC='space_228.gif' WIDTH=64 HEIGHT=64>";
}
?></td>
<td><?php
//southeast
echo "se";
$x8 = $_SESSION[x] + 1;
$y8 = $_SESSION[y] - 1;
$hasobject8 = mysql_query("SELECT * FROM objects WHERE x='$x8' && y='$y8'");
while($row = mysql_fetch_array($hasobject8))
{
$image = "space_228.gif";
$image = $row[image];
}
if(!empty($image)){
echo "<IMG SRC=$image WIDTH=64 HEIGHT=64 onerror='onImgError(this)>'";
}
else{
echo "<IMG SRC='space_228.gif' WIDTH=64 HEIGHT=64>";
}
?></td>
</tr>
</table> 

Now what happens is it draws the image on a bunch of different spots. The object is at 0,0.  It doesnt display the object if the object is out of range of the player, or the player is at 0,0. But if the player is near the object, it draws it on like half the grid spaces, and on some places everywhere but the players.  Heres the page, login with test, password test. http://spacetimemmo.webuda.com/login.php

Link to comment
https://forums.phpfreaks.com/topic/237812-what-the-hell/
Share on other sites

How many items are in your database? What is the code that sets/changes those session variables. What is the structure of the table you are using? IS there a reason you use the x,y variables as strings in the queries? have you tried removing the quotes around the x,y coordinates and seen if that gave you better results?

 

You could make your script more efficient if you used a for loop to build 1 large query, and query the database once. However, this should be considered after you fix your script.

Link to comment
https://forums.phpfreaks.com/topic/237812-what-the-hell/#findComment-1222069
Share on other sites

aight, I only have one object

I'll try clearing image, dont think it will work though...

 

Haha thanks! Didnt think that would work! Still one bug though, when the player is directly under the object, it draws it on the northeast and north box, but probably a simple bug, quick fix.

 

Okay, the problem was I forgot to clear image in North, knew it would be simple...

Link to comment
https://forums.phpfreaks.com/topic/237812-what-the-hell/#findComment-1222082
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.