Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 20

Thread: Grid Map Help

  1. #1
    Regular Coder
    Join Date
    Jun 2009
    Posts
    278
    Thanks
    78
    Thanked 2 Times in 2 Posts

    Grid Map Help

    I cant figure out why this isnt working:

    PHP Code:
    <?
    $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.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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 Code:
    <?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.


    .
    Last edited by mlseim; 03-01-2010 at 02:10 AM.

  • Users who have thanked mlseim for this post:

    Ndogg (03-01-2010)

  • #3
    Regular Coder
    Join Date
    Jun 2009
    Posts
    278
    Thanks
    78
    Thanked 2 Times in 2 Posts
    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.

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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.

  • Users who have thanked mlseim for this post:

    Ndogg (03-02-2010)

  • #5
    Regular Coder
    Join Date
    Jun 2009
    Posts
    278
    Thanks
    78
    Thanked 2 Times in 2 Posts
    It doesnt come out with anything.

  • #6
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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.

  • Users who have thanked mlseim for this post:

    Ndogg (03-02-2010)

  • #7
    Regular Coder
    Join Date
    Jun 2009
    Posts
    278
    Thanks
    78
    Thanked 2 Times in 2 Posts
    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>

  • #8
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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" .... >

  • Users who have thanked mlseim for this post:

    Ndogg (03-02-2010)

  • #9
    Regular Coder
    Join Date
    Jun 2009
    Posts
    278
    Thanks
    78
    Thanked 2 Times in 2 Posts
    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.

  • #10
    New Coder
    Join Date
    Feb 2010
    Posts
    20
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Probably overlooking something but where do you get the data for:

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

  • Users who have thanked Courtney for this post:

    Ndogg (03-02-2010)

  • #11
    Regular Coder
    Join Date
    Jun 2009
    Posts
    278
    Thanks
    78
    Thanked 2 Times in 2 Posts
    its not being used yet anyways, but it is from the form.

  • #12
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by mlseim View Post
    Code:
    for($i = 0; $i <= 1; $i++){
    $xx[$i] = $_POST['X[$i]'];
    $yy[$i] = $_POST['Y[$i]'];

    Should that not be:

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

  • Users who have thanked MattF for this post:

    Ndogg (03-02-2010)

  • #13
    Regular Coder
    Join Date
    Jun 2009
    Posts
    278
    Thanks
    78
    Thanked 2 Times in 2 Posts
    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.

  • #14
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Try:

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

  • Users who have thanked MattF for this post:

    Ndogg (03-02-2010)

  • #15
    Regular Coder
    Join Date
    Jun 2009
    Posts
    278
    Thanks
    78
    Thanked 2 Times in 2 Posts
    Dang, MattF, two posts and you solve the problem! Bravo

    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]
    Last edited by Ndogg; 03-02-2010 at 05:58 AM.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •