Hi all. This question is probably already somewhere in here but I can’t find it. I want to display rental apartments grouped by property. I have 2 tables, one that has the property info (location, etc.) the other has info for each apartment in the property (rent, vacancy, etc.) The SELECT statement I have now is
"SELECT properties.buildingID, properties.location, units.rentalID, units.unitnum, units.rent_status
FROM properties
LEFT JOIN units
ON properties.buildingID = units.buildingID ";
it displays this:
Property 1 Unit 1 Rented
Property 1 Unit 2 Rented
Property 2 Unit 1 Rented
Property 2 Unit 2 Vacant
…and so on. What I’d like is to display it like this:
Even if I change it from buildingID to location (which is the physical address) I still get this
Another note, the rent_status field is boolean (tinyint, only 0 or 1 entered) so the “3” after “is” shouldn’t be there at all. What am I doing wrong?
properties table:
Field Type Null Key Default Extra
buildingID tinyint(6) NO PRI NULL auto_increment
mailingaddress text NO NULL
description text NO NULL
features text NO NULL
location varchar(75) NO NULL
image1 varchar(255) YES NULL
image2 varchar(255) YES NULL
image3 varchar(255) YES NULL
image4 varchar(255) YES NULL
image5 varchar(255) YES NULL
image6 varchar(255) YES NULL
image7 varchar(255) YES NULL
image 8 varchar(255) YES NULL
units table:
Field Type Null Key Default Extra
rentalID tinyint(12) NO PRI NULL auto_increment
buildingID tinyint(6) NO NULL
unitnum tinyint(5) NO NULL
rent_status tinyint(1) NO NULL
image1 varchar(255) YES NULL
image2 varchar(255) YES NULL
image3 varchar(255) YES NULL
image4 varchar(255) YES NULL
details text NO NULL
features text NO NULL
numbedrooms varchar(10) NO NULL
rent varchar(45) NO NULL
deposit varchar(35) NO NULL
lease varchar(25) NO NULL
And yes, I do want the location rather than ID numbers, which is why I did a LEFT JOIN in the query. The location column is in the for loop but doesn’t show up.
I’ve also tried designating the tables as well as the columns in the for loop (i.e. properties.buildingID etc.). I’ve even tried using a non-numerical column like units.features but it comes out exactly the same no matter what I change.
$query_rs_status = "
SELECT p.location as property
, u.unitnum as unit
, u.rent_status as status
FROM properties as p
LEFT JOIN units as u
ON p.buildingID = u.buildingID ";
UNTESTED
That should bring back rows of data which resemble those I used and example in reply #2.
Add rows back in as you see fit, but hopefully this - with the code I posted - will illustrate my point.
It is now showing the actual names of the properties, but still every line
123 Water Street
101 is 1
123 Water Street
102 is 1
123 Water Street
103 is 1
.../etc.
I’ve rebooted in the meantime so maybe cache got cleared or something. Anyway, all data is being displayed but still the same as what I originally had.
Just saw your last replay and I’m not sure how to do a var_dump(), but this is the result from phpMyAdmin
Thanks for that, I’ll definitely look up the var_dump
That result set looks about right to me, try adding the code I gave you featuring a foreach() loop instead of your attempt.
OK, here is what I have for the query, including the ORDER BY that r937 recommended:
mysql_select_db($database_conn_taylor, $conn_taylor);
$query_rs_status = "SELECT p.buildingID as bID, p.location as property, u.unitnum as unit, u.rent_status as status
FROM properties as p
LEFT JOIN units as u
ON p.buildingID = u.buildingID
ORDER BY bID";
$rs_status = mysql_query($query_rs_status, $conn_taylor) or die(mysql_error());
$row_rs_status = mysql_fetch_assoc($rs_status);
$totalRows_rs_status = mysql_num_rows($rs_status);
35 Belvedere Ave
1 is 1
35 Belvedere Ave
2 is 1
35 Belvedere Ave
3 is 1
460 St. Peter's Rd
1 is 1
460 St. Peter's Rd
2 is 1
315 Norwood Rd.
2 is 1
315 Norwood Rd.
3 is 0
Etc.
I feel like something obvious is missing or out of place but I just can’t see it.
You need to order by property, not bID. This line:
$row_rs_status = mysql_fetch_assoc($rs_status);
Shouldn’t be called either, you only getting the first row, which appears to be pointless. you also don’t appear to be requesting a ‘featues’ column, or am I mistaken?
Cups is correct, you need to break it down down into smaller chunks and debug it as you go.
Give this a whirl, it’s essentially what Cups has asked you to do already though…
so, essentially one row of data. No idea why it would display as numbers though.
I realized my mistake in doing the ORDER BY on buildingID (duh!) and changed it to property when I was testing but that was after my post. Results were the same regardless. But it works now [dance of happiness]