Page 1 of 1

[Solved] Exclude certain values from a FOR loop

Posted: Wed Jul 18, 2007 12:40 pm
by Dale
I have came to a problem. I have a query pulling from the database, stating a SQUARE ID (eg; 4) and then on the map thing I have done it shows the 4th square. However when I want it to shwo a list of squares that are not selected it doesn't seem to work. Here is what I have at the moment, but it don't seem to work. Any ideas why?

Code: Select all

<?php
$sql2 = "SELECT * FROM mapitems WHERE userid='1'";
$result2 = mysql_query($sql2, $conn) or die(mysql_error());
$total = mysql_num_rows($result2);

for($i=1;$i<(51-$total);$i++) {
	$sql = "SELECT * FROM mapitems WHERE userid='1' AND squareid='$i' ORDER BY squareid ASC";
	$result = mysql_query($sql, $conn) or die(mysql_error());
	while($r = mysql_fetch_array($result)) {
		$squi = $r['squareid'];
	}
	if(!$squi) {
		echo "$i\n";
	}
}
?>

Posted: Wed Jul 18, 2007 12:56 pm
by RobertGonzalez
Can you explain a little what you are trying to do here?

Posted: Wed Jul 18, 2007 1:05 pm
by Dale
Well basically the "map" has 50 squares, and I'm wanting it to have a dropdown list of the available sqaures.

- Square 1
- Square 2
- Square 3
- Square 4
etc...

However if Square 3 is taken then it shows it like:
- Square 1
- Square 2
- Square 4

Posted: Wed Jul 18, 2007 1:09 pm
by John Cartwright
How do you define when a square is taken or occupied?

Posted: Wed Jul 18, 2007 1:11 pm
by RobertGonzalez
And how does your layout work when you are running a loop of queries inside another query result loop (a habit that I highly recommend breaking).

Posted: Wed Jul 18, 2007 1:11 pm
by superdezign
Do a select query that selects all EXCEPT for that one.

Code: Select all

SELECT * FROM `squares` WHERE `squareid` != $squareid;

Posted: Wed Jul 18, 2007 2:14 pm
by Dale
superdezign wrote:Do a select query that selects all EXCEPT for that one.

Code: Select all

SELECT * FROM `squares` WHERE `squareid` != $squareid;
I do that and it comes out with 1,1,1,1,1,1,1,2,2,2,2,2,2,2,3,3,3,3,3,3,3,4,4,4,4,4,4,4,5,5,5,5,5,5,5 etc... upto 50.

Posted: Wed Jul 18, 2007 2:15 pm
by John Cartwright
Again, how do you define that a square is occupied?

Posted: Wed Jul 18, 2007 2:19 pm
by superdezign
And I'm sure you can do away with the loop. Most queries can be done without looping.

Posted: Wed Jul 18, 2007 2:29 pm
by Dale
Jcart wrote:Again, how do you define that a square is occupied?
Apologies for missing your previous reply.

Here is the code that generates and spots an area on the map that is occupied:

Code: Select all

<?php
for($i=1;$i<51;$i++) {
	if($i=="11" OR $i=="12" OR $i=="13" OR $i=="14" OR $i=="15" OR $i=="16" OR $i=="17" OR $i=="18" OR $i=="19" OR $i=="29" OR $i=="32" OR $i=="33" OR $i=="34" OR $i=="35" OR $i=="36" OR $i=="37" OR $i=="38" OR $i=="39" OR $i=="42") {
	echo "<td width="64" height="64">";
	// The Road
	switch($i) {
		case 11: echo "<img src="./images/mapitems/road.gif">"; break;
		case 12: echo "<img src="./images/mapitems/road.gif">"; break;
		case 13: echo "<img src="./images/mapitems/road.gif">"; break;
		case 14: echo "<img src="./images/mapitems/road.gif">"; break;
		case 15: echo "<img src="./images/mapitems/road.gif">"; break;
		case 16: echo "<img src="./images/mapitems/road.gif">"; break;
		case 17: echo "<img src="./images/mapitems/road.gif">"; break;
		case 18: echo "<img src="./images/mapitems/road.gif">"; break;
		case 19: echo "<img src="./images/mapitems/road_l_d.gif">"; break;
		case 29: echo "<img src="./images/mapitems/road_d.gif">"; break;
		case 32: echo "<img src="./images/mapitems/road_r_d.gif">"; break;
		case 33: echo "<img src="./images/mapitems/road.gif">"; break;
		case 34: echo "<img src="./images/mapitems/road.gif">"; break;
		case 35: echo "<img src="./images/mapitems/road.gif">"; break;
		case 36: echo "<img src="./images/mapitems/road.gif">"; break;
		case 37: echo "<img src="./images/mapitems/road.gif">"; break;
		case 38: echo "<img src="./images/mapitems/road.gif">"; break;
		case 39: echo "<img src="./images/mapitems/road_d_l.gif">"; break;
		case 42: echo "<img src="./images/mapitems/road_d.gif">"; break;
	}

	} else {

	echo "<td width="64" height="64" background="./images/mapitems/canbuild.gif">";
	$sql = "SELECT * FROM mapitems WHERE squareid = '$i' AND userid='1'";
	$result = mysql_query($sql, $conn) or die(mysql_error());
	while($r = mysql_fetch_array($result)) {
		if($r[itemid]) {
			echo "<img src="./images/mapitems/grass.gif">";
		}
	}
	}
	echo "</td>\n";
	if($i == "10" OR $i == "20" OR $i == "30" OR $i == "40" OR $i == "50") {
	if($i!="50") {
	echo "</tr>\n";
	echo "<tr valign="top" background="./images/mapitems/canbuild.gif">\n";
	}
	}
}
?>
P.S. I think I may need to win an award for the messiest coder around. ;)

Posted: Wed Jul 18, 2007 2:39 pm
by RobertGonzalez

Code: Select all

<?php
if($i=="11" OR $i=="12" OR $i=="13" OR $i=="14" OR $i=="15" OR $i=="16" OR $i=="17" OR $i=="18" OR $i=="19" OR $i=="29" OR $i=="32" OR $i=="33" OR $i=="34" OR $i=="35" OR $i=="36" OR $i=="37" OR $i=="38" OR $i=="39" OR $i=="42")
?>
can be written as

Code: Select all

<?php
if ( ($i >= 11 && $i <= 19) OR $i == 29 OR ($i >= 32 && $i <= 39) OR $i == 42 )
?>

Posted: Wed Jul 18, 2007 2:49 pm
by Dale
Everah wrote:

Code: Select all

<?php
if($i=="11" OR $i=="12" OR $i=="13" OR $i=="14" OR $i=="15" OR $i=="16" OR $i=="17" OR $i=="18" OR $i=="19" OR $i=="29" OR $i=="32" OR $i=="33" OR $i=="34" OR $i=="35" OR $i=="36" OR $i=="37" OR $i=="38" OR $i=="39" OR $i=="42")
?>
can be written as

Code: Select all

<?php
if ( ($i >= 11 && $i <= 19) OR $i == 29 OR ($i >= 32 && $i <= 39) OR $i == 42 )
?>
Cheers, the code it looking a bit cleaner. :)

Posted: Wed Jul 18, 2007 3:46 pm
by Dale
Oh my goodness.. I am soooooooooooooothick. I've just solved it. ;)

Cheers for the help you guys. :)

Code: Select all

<select>
<?php
for($i=1;$i<51;$i++) {
 	$sql = "SELECT * FROM mapitems WHERE userid='1' AND squareid='$i'";
	$result = mysql_query($sql, $conn) or die(mysql_error());
	while($r = mysql_fetch_array($result)) {
		$r_squareid = $r[squareid];
	}
	if($i != $r_squareid) {
		echo "<option>$i</option>";
	} else {

	}
}
?>
</select>