View Full Version : Stuck! While loop iterations

01-29-2012, 08:48 PM
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.

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);


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

{ echo "<b>[$x-$y]</b>"; }
else { echo "($countx-$county)"; }

This is what is outputted (http://dft.ba/-MapOutPut).

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

This is the SQL data;

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

01-29-2012, 10:40 PM
Why aren't you just looping through the result set? What if you never have 100 rows? Try this

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)

while ($row_map = mysql_fetch_assoc($map))

if ($countx == 11)
$countx = 1;
echo "<br/>";
$x = $row_map['axisx'];
$y = $row_map['axisy'];

if($x == $countx && $y == $county)
echo "<b>[$x-$y]</b>";
echo "($countx-$county)";
Proper indention and bracket alignment makes things easier to understand the flow of the code.

01-29-2012, 11:03 PM
Thanks for the reply! :) That's what I did originally but it just outputs;


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

01-31-2012, 12:21 AM
Nobody got any ideas? :(

01-31-2012, 01:15 AM
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:

$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:

$map[$x][$y] = $mysql_row;

Then, after your processing, your $map 2D array will be filled with either blank values, or mysql filled values.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum