Results 1 to 6 of 6

Thread: Insert into

  1. #1
    New Lounger
    Join Date
    Dec 2008
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Insert into

    I'm having an issue with a php/sql for and insert statement. I have a lot of rows to put into my table and won't be very fun manually entering them all. It'll basically be a grid, the table has 3 fields right now, xmap, ymap, and tile. The table is called map. Here is what I have
    Code:
    <?php
    $con = mysql_connect("blah","blah","blah");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("mydb", $con);
    
    for ($i=-1000; $i<=1000; $i++)
    { 
    $sql="INSERT INTO map (xmap, ymap, tile)
    VALUES
    ('$i','$i','tile.gif')";
    
    if (!mysql_query($sql,$con))
      {
      die('Error: ' . mysql_error());
      }
    echo "1 record added";
    }
    mysql_close($con)
    ?>
    This will enter roughly 2000 rows in my table and will be xmap = -1000, ymap = -1000; xmap=-999, ymap=-999. What I really want is xmap=-1000, ymap=-1000; xmap=-1000, ymap=-999. I need xmap from -1000-1000 and ymap = -1000, then ymap = -999, then ymap = -998 and so on.

  2. #2
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts
    Quote Originally Posted by flnanjl View Post
    What I really want is xmap=-1000, ymap=-1000; xmap=-1000, ymap=-999. I need xmap from -1000-1000 and ymap = -1000, then ymap = -999, then ymap = -998 and so on.
    Can you have another go at explaining what you want? I can't follow this.
    Regards
    John



  3. #3
    New Lounger
    Join Date
    Dec 2008
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    basically what i want is my table to look like this

    xmap ymap tile
    -1000 -1000 tile.gif
    -1000 -999 tile.gif
    -1000 -998 tile.gif
    -1000 -997 tile.gif

    and so on, all they way to ymap is 1000. Then do the next number in xmap.
    xmap ymap tile
    -999 -1000 tile.gif
    -999 -999 tile.gif
    -999 -998 tile.gif

    and so on, until xmap and ymap are both at 1000. think of a quadrant like in math. bottom left is -1000, -1000 top right is 1000,1000 and i need all combination's from in between.

  4. #4
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts
    You need two nested loops, using two variables $i and $j

    for ($i=-1000; $i<=1000; $i++)
    {
    for ($j=-1000; $j<=1000; $j++)
    {

    and

    VALUES
    ('$i','$j','tile.gif')";
    Last edited by johnhutchison; 2011-03-26 at 22:52.
    Regards
    John



  5. #5
    WS Lounge VIP Browni's Avatar
    Join Date
    Dec 2009
    Location
    Rochdale, UK
    Posts
    1,653
    Thanks
    38
    Thanked 161 Times in 139 Posts
    I'm curious to know the intended purpose of this table.

    John's post above seems to meet the OP's criteria but will result in a table of more than 4 million records!

  6. #6
    New Lounger
    Join Date
    Dec 2008
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you very much, I got it to work. This large grid is used as a map for my game I'm designing, I know it sounds like a lot but it only displays a 15x15 grid at a time.

Posting Permissions

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