...

View Full Version : Grid Map Help



Ndogg
02-28-2010, 11:10 PM
I cant figure out why this isnt working:



<?
$amount = $_POST["amount"];
for($i = 0; $i < 1; $i++){$xx[$i] = $_POST["X[$i]"]; $yy[$i] = $_POST["Y[$i]"];} //Gets X/Y coords from form

for($y = 0; $y < 10; $y++){echo "<br>"; //Draws the Y coords
for($x = 0; $x < 10; $x++){ //Draws the X coords
if ($x == $xx[$i] && $y == $yy[$i]){echo "<img src=\"images/v2.png\">";}else{echo "<img src=\"images/Grass75x.png\">";} //Draws bg or towns at coords from above
}}
?>


It always puts the img "v2.png" at the coords 0,0.

mlseim
03-01-2010, 01:47 AM
I had to spread it out and use more proper indentations so I could see what you're doing ...
You should work on how your listings look ... easier to troubleshoot.
Don't worry about adding white space and making the file larger.
This is 2010, not 1975 ... memory is not a big deal anymore. Being able to see
your code (and for others to see it) is much more important than crunching things together.

I commented where the problem is, but not sure which variable you really want.
You are using $xx[$i] and $yy[$i], but that is wrong ... I just don't know what you really are doing.
Maybe you meant $xx[$x] and $yy[$y]?

Secondly, is this (in red) correct?
for($i = 0; $i < 1; $i++){

That doesn't look right, because $i is already zero (less than 1) ...????
The 1 you have there can't be correct?
Again ... no idea what you're trying to accomplish ... because we can't see the form or anything else.



<?php
$amount = $_POST["amount"];

//Gets X/Y coords from form
// I think you mean to do something more like this: for($i=0; $i<=count($_POST['X']); $i++){
for($i = 0; $i < 1; $i++){
$xx[$i] = $_POST["X[$i]"];
$yy[$i] = $_POST["Y[$i]"];
}

//Draws the Y coords
for($y = 0; $y < 10; $y++){
echo "<br>";

//Draws the X coords
for($x = 0; $x < 10; $x++){

// HERE IS WHERE THE PROBLEM IS ...
// The problem is the $xx[$i] and $yy[$i].
// How does is know what $i is?
// Right now, $i is always going to be the last $i++ that you did in the first "for loop".
if ($x == $xx[$i] && $y == $yy[$i]){
echo "<img src=\"images/v2.png\">";
}
else{
//Draws bg or towns at coords from above
echo "<img src=\"images/Grass75x.png\">";
}
}
}
?>


See how much easier it is to pick-out the loops and if-statements?
The indentations make scanning the code with your eyes almost painless.


.

Ndogg
03-01-2010, 03:18 AM
Thanks, that is much easier to read.

for($i = 0; $i < 1; $i++)
The "1" is acually going to be changed to the $amount var used on the first line. The amount will be determined by how many coordinates they post.

$xx[$i]... parts im pretty positive are what Im trying to do

Its just what you said on how $x == $xx[$i] will always be the last $i++, how can I get it so its checking every $i. I tried wrapping the for($i = 0;... around the other two for loops but that doesnt work either.

mlseim
03-01-2010, 12:22 PM
So, that means when you get to this ...

if ($x == $xx[$i] && $y == $yy[$i]){
echo "<img src=\"images/v2.png\">";
}

$xx[$i] and $yy[$i] are always 0

Maybe you can test the values ... try this:

<?php
$amount = $_POST["amount"];

//Gets X/Y coords from form
for($i = 0; $i < 1; $i++){
$xx[$i] = $_POST['X[$i]'];
$yy[$i] = $_POST['Y[$i]'];
}

echo "X: $xx[0] -- Y: $yy[0]";
exit;


See what the values really are.

Ndogg
03-01-2010, 10:38 PM
It doesnt come out with anything.

mlseim
03-01-2010, 10:52 PM
Really? You mean a blank screen?

That means there is a PHP error and you have error reporting turned off.

hmmm...
Figure out a way to display those two variables.
I think they are not being set properly and the value is always zero.

Ndogg
03-01-2010, 11:04 PM
Heres a part of the form:

<TD width="10%">X: <input type="text" name="X[0]" value="0" size="2"></TD>
<TD width="90%">Y: <input type="text" name="Y[0]" value="0" size="2"></TD>

mlseim
03-02-2010, 12:50 AM
Do it like this (nothing in the brackets):

<TD width="10%">X: <input type="text" name="X[]" value="0" size="2"></TD>
<TD width="90%">Y: <input type="text" name="Y[]" value="0" size="2"></TD>

Then, just try this much of a script and see if you can get the values ...

<?php
$amount = $_POST['amount'];

//Gets X/Y coords from form
for($i = 0; $i <= 1; $i++){
$xx[$i] = $_POST['X[$i]'];
$yy[$i] = $_POST['Y[$i]'];
}

echo "X: $xx[0] -- Y: $yy[0]";

?>

Also, I assume your form has POST? <form method="post" .... >

Ndogg
03-02-2010, 01:33 AM
Ok, none of that changed anything so I changed the name to X and just did echo $_post["X"]; That posted "array"

Whats going on.

Courtney
03-02-2010, 03:15 AM
Probably overlooking something but where do you get the data for:

$amount = $_POST['amount']; I don't see it anywhere in your code.

Ndogg
03-02-2010, 03:17 AM
its not being used yet anyways, but it is from the form.

MattF
03-02-2010, 03:33 AM
for($i = 0; $i <= 1; $i++){
$xx[$i] = $_POST['X[$i]'];
$yy[$i] = $_POST['Y[$i]'];




Should that not be:



for($i = 0; $i <= 1; $i++){
$xx[$i] = $_POST['X'][$i];
$yy[$i] = $_POST['Y'][$i];

Ndogg
03-02-2010, 04:51 AM
Omg, thats correct but its not placing the image at the correct X and Y, because the $i isnt working for the rest of the code.

MattF
03-02-2010, 05:02 AM
Try:



foreach ($_POST['X'] as $key => $val)
{
$xx[$key] = $_POST['X'][$key];
$yy[$key] = $_POST['Y'][$key];

Ndogg
03-02-2010, 05:52 AM
Dang, MattF, two posts and you solve the problem! Bravo :D

edit: wait... It only draws the image for one, so when I have more than one X/Y it only does the X[0]... rather than like X[0-5]

MattF
03-02-2010, 06:01 AM
Post your amended code.

Ndogg
03-02-2010, 06:10 AM
<?php
$amount = $_POST["amount"];

//Gets X/Y coords from form
foreach ($_POST['X'] as $key => $val)
{
$xx[$key] = $_POST['X'][$key];
$yy[$key] = $_POST['Y'][$key];
}

//Draws the Y coords
for($y = 0; $y < 10; $y++){
echo "<br>";

//Draws the X coords
for($x = 0; $x < 10; $x++){

if ($x == $xx[$key] && $y == $yy[$key]){
echo "<img src=\"images/v2.png\">";
}
else{
//Draws bg or towns at coords from above
echo "<img src=\"images/Grass75x.png\">";
}
}
}
?>

mlseim
03-02-2010, 12:30 PM
I totally missed that one! Glad you spotted it.
I know it didn't look right, but I just couldn't pin it down.

MattF
03-02-2010, 02:14 PM
I totally missed that one! Glad you spotted it.
I know it didn't look right, but I just couldn't pin it down.

I just happened to be on the ball, for once. It's a rarity for me. :D


Ndogg, as mlseim mentioned, you were always going to match the last iteration with that code. Something along these lines is what you want, although I've no idea what criteria designates which image is output. Possibly $xx[$key] == $yy[$key]?



<?php

$amount = $_POST["amount"];

# Get the X and Y coordinates from the form.

foreach ($_POST['X'] as $key => $val)
{
if ($key < 10)
{
$xx[$key] = $_POST['X'][$key];
$yy[$key] = $_POST['Y'][$key];

# Draw the Y coordinate.

echo "<br>";

# Draw the X coordinate.

if ([what should $key match against here?])
{
echo "<img src=\"images/v2.png\">";
}
else
{
# Draw bg or towns at the current coordinates.

echo "<img src=\"images/Grass75x.png\">";
}
}
}

?>

Ndogg
03-03-2010, 02:40 AM
So basically rap the foreach around everything now?

That has the same outcome has the for events, it repeats the map grids

So example, lets say it draws this as a map:

OO
OO

Now it does:

OO
OO
OO
OO



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum