Hi everyone,
I have a slight issue that I have been unable to resolve.
Firstly a little explanation, What this function does is pull off results from a table(tenants), specifically using 1 field(tenants_roomnum).
It should loop through the while statement and build a list of all rooms for the property and if there is a tenant assigned to that property append Room # with 'occupied'.
I have narrowed down the problem to the while loop itself, in that I cant transfer the variable $db_field['tenants_roomnum'] for EVERY tenant into the while loop, even if i set $roomNum = $db_field.... It only uses the highest value, in our examples, room 4.
In the examples I am using for testing there is 3 tenants assigned to room 4, room 3 and room 2.
The while loop only executes correctly on the highest room value, i.e. 4
I have commented the code with notes and feel free to ask questions because I must be missing something and I am totally stuck. Thanks for your help.
Neil
function roomsTaken($room, $property)
{
include "../includeDbDetails.php";
// roomCount() returns the amount of rooms that a property
// has. - Working. In our example, it returns the value 5.
$roomCounter = roomCount($property);
// To be used by our loop, starts the room counting at 1, because
// obviously we do not have a room 0.
$i = 1;
$tenantsdb = mysql_connect($host, $user, $pass); // Connect
$db_connect = mysql_select_db($name, $tenantsdb); // Select which db
if ($db_connect) // Have we connected? If yes:
{
$SQL = "SELECT * FROM tenants where property_id = '$property'"; // Our SQL statement, works fine. $property = 1
$tenantsresult = mysql_query($SQL);
{
while ($db_field = mysql_fetch_assoc($tenantsresult))
{
while($i <= $roomCounter) // In our testing, $roomCounter = 5
{
echo "room: $i"; // Display the room number
if($db_field['tenants_roomnum'] == $i) // In our testing there is 3 tenants assigned to $property
echo " occupied";
echo "<br />"; // For neatness.
$i++; // Increment the room counting.
}
/* The above while statement returns:
room: 1
room: 2
room: 3
room: 4 occupied
room: 5
This is incorrect, it should return:
room: 1
room: 2 occupied
room: 3 occupied
room: 4 occupied
room: 5
*/
echo $db_field['tenants_roomnum'] . "<br />";
/* If we list all entry' room numbers we are provided with:
4
3
2
This proves that there are three tenants in rooms 4,3 and 2.
*/
}
}
mysql_close($tenantsdb); // Close off the connection
} // Uh oh, our database is either wrong or its down.
else
{
print "Database NOT Found ";
mysql_close($tenantsdb); // Close the connection.
}
}