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.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Sep 2007
    Posts
    98
    Thanks
    25
    Thanked 4 Times in 4 Posts

    Stuck! While loop iterations

    I haven't coded in a while... and I'm feeling really really rusty. I'm doing some tests to create a map - I've began by trying to iterate all the axis points with some being replaced by items which exist in the DB. It seemed to be working fine for the first item I placed in the DB - but after that it hasn't. I've been stuck on this simple task for quite a while now and was hoping for some help.

    PHP Code:
    <?
    mysql_select_db
    ($database_DB_A$DB_A);
    $query_map "SELECT * FROM world_map ORDER BY axisx, axisy ASC";
    $map mysql_query($query_map$DB_A) or die(mysql_error());
    $row_map mysql_fetch_assoc($map);

    $countx=0;$county=1;$count=1;

    while (
    $count<=100) { 
        
    $countx++; $count++;
        if (
    $countx==11)
            { 
    $countx=1$county++; echo "<br/>";}
        
    $x=$row_map['axisx']; $y=$row_map['axisy'];

        if((
    $x==$countx)&&($y==$county)) 
        { echo 
    "<b>[$x-$y]</b>"; } 
            else { echo 
    "($countx-$county)"; }
    }
    ?>
    This is what is outputted.

    The issue I have is there should be more than one field bolded...

    This is the SQL data;
    Code:
    id		axisx	axisy	type	house	subof
    0000000001	1	2	r	001	NULL
    0000000002	1	3	r	002	NULL
    0000000003	1	4	r	001	NULL
    It doesn't seem to be looping through the rows... Or am I just being incredibly stupid? :S
    Last edited by weir-07; 01-29-2012 at 08:53 PM.

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Why aren't you just looping through the result set? What if you never have 100 rows? Try this
    PHP Code:
    <?php
    mysql_select_db
    ($database_DB_A$DB_A);
    $query_map "SELECT * FROM world_map ORDER BY axisx, axisy ASC";
    $map mysql_query($query_map$DB_A) or die(mysql_error());
    if(
    mysql_num_rows($map) > 0)
    {    
        
    $countx=0;
        
    $county=1;
        
    $count=1;
        
        while (
    $row_map mysql_fetch_assoc($map))
        { 
            
            if (
    $countx == 11)
            {
                
    $countx 1;
                
    $county++;
                echo 
    "<br/>";
            }
            
    $x $row_map['axisx'];
            
    $y $row_map['axisy'];
            
            if(
    $x == $countx && $y == $county
            {
                echo 
    "<b>[$x-$y]</b>";
            } 
            else
            {
                echo 
    "($countx-$county)";
            }
            
    $countx++;
        }
    }
    ?>
    Proper indention and bracket alignment makes things easier to understand the flow of the code.
    Last edited by _Aerospace_Eng_; 01-29-2012 at 10:42 PM.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    New Coder
    Join Date
    Sep 2007
    Posts
    98
    Thanks
    25
    Thanked 4 Times in 4 Posts
    Thanks for the reply! That's what I did originally but it just outputs;

    Code:
    (0-1)(1-1)(2-1)
    Which aren't even the right variables from the DB.

    The 100 is there because in the area there are going to be 100 tiles.

    I'm trying to list 1-1 through to 10-10 in a block, replacing as the DB notes.

    THIS is how it should look - http://dft.ba/-MapOutputGood
    This is how mine looks... http://dft.ba/-MapOutPut

  • #4
    New Coder
    Join Date
    Sep 2007
    Posts
    98
    Thanks
    25
    Thanked 4 Times in 4 Posts
    Nobody got any ideas?

  • #5
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    What I would do, if I were you, is construct a 2D array first then, as you're retrieving the co-ordinated from mysql, replace the information into the 2D array. However, you'll have to remember that arrays start at 0, so your co-ordinates should do the same - for simplicity sake. Something like this:
    PHP Code:
    $xLimit 10;
    $yLimit 10;

    // Initialise the array.
    // Use array_fill() and fill all the y co-ordinates first - you'll be able to see why
    $y array_fill(0,$yLimit,'Default value');
    $map array_fill(0,$xLimit,$y); 
    Then, as you iterate through your mysql results, get the x co-ordinate and the y co-ordinate, and enter them like the following:
    PHP Code:
    $map[$x][$y] = $mysql_row
    Then, after your processing, your $map 2D array will be filled with either blank values, or mysql filled values.
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.


  •  

    Posting Permissions

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