Forum Moderators: open
$ireland_total_count_query = mysql_query("select property_location, count(*) from " . TABLE_PROPERTY . " where property_status = '1' GROUP BY 'property_location'");
$ireland_total_count = mysql_fetch_array($ireland_total_count_query);
$ireland_total_count_is += $ireland_total_count['total'];
$property_location = array("Dublin", "Cork", . . . );
$count_location = count($property_location);
for ($i=0; $i < $count_location; $i++) {
$ireland_total_count_query = mysql_query("select count(*) as total from " . TABLE_PROPERTY . " where property_status = '1' GROUP BY ". $property_location[$i] ."");
$ireland_total_count = mysql_fetch_array($ireland_total_count_query);
$ireland_total_count_is += $ireland_total_count['total'];
}
Hope this helps
$ireland_total_count_query = mysql_query("SELECT * from " . TABLE_PROPERTY . " where property_status = '1' AND (location = 'Dublin' OR location = 'london' OR . . . )
echo mysql_num_rows($ireland_total_count_query);
If not we need to roll up our sleeves for a bigger query (if it can be done :) )
Habtom
Something like
"select Count(propertyID`) AS PropertyCount, PropertyLocation from PropertyTable group by PropertyLocation having property_status = 1" (we will call this viewPropertyLocationCount)
You can then query the view with something like
"Select PropertyCount, PropertyLocation from viewPropertyLocationCount where PropertyLocation in (list,of,locations)"
Then parse and retrieve data.
Onya
Woz
i need the total count of all active properties in the database
i also need the total of all active CORK properties in the database
i also need the total of all active DUBLIN properties in the database, and so on for the 32 counties.
Do you know if this can be done in one query?
the query below returns my first objective, i.e all active properties.
$ireland_total_count_query = mysql_query("select property_location, count(*) as total from " . TABLE_PROPERTY . " where property_status = '1' GROUP BY 'property_location'");
$ireland_total_count = mysql_fetch_array($ireland_total_count_query);
$ireland_total_count_is += $ireland_total_count['total'];
now, can i use anything like:
$mayo_total_count_is += $ireland_total_count['total(Mayo)'];
to return the Mayo count from that query?
it's driving me mad, but i presume it can be done somehow? - what does the GROUP BY 'property_location' do, can these groups be counted?
thanks again for any help.
[perl]
$q = mysql_query("SELECT property_location, count(*) AS count
FROM " . TABLE_PROPERTY
. " WHERE property_status = '1'
GROUP BY 'property_location'");
while ($row = mysql_fetch_array($q)) {
$the_data[$row['property_location']] = $row['count'];
}
var_dump($the_data); // show the data structure
[/perl]
You'll end up with an array, with the key being the name of the property, and the value being the number of rows.
Sean
array(1) { ["Kerry"]=> string(4) "1436" }
i don't know why it is only showing kerry and the 1436 is the total of all counties not just kerry.
playing with the sql query now to see if i can spot anything, but if you can see why i'm getting the above result it would be appreciated.
SeanW you are correct, my mistake, correct query herewith.
"SELECT Count(propertyID`) AS PropertyCount, PropertyLocation FROM PropertyTable WHERE property_status = 1 Group by PropertyLocation"
re "no need for views"
Well, that depends. It is always best to use the correct tools for the task whenever possible and/or practical. Databases (mySQL, Access, MSSQL, whatever) are designed to store and manipulate Data. Scripting languages (PHP, ASP, whatever) are designed to retrieve/send/manipulate information. They can be used to manipulate data but that does incur an increased server overhead.
If propertyireland2's need to aggregate and count the data is a one-off instance, then setting up a query within PHP is perhaps warranted. However, if this is to be done on a recurring basis, even once a day, then setting up a view would be far preferable.
Onya
Woz
{ ["Kerry"]=> string(4) "1436" }
using:
$q = mysql_query("SELECT property_location, count(*) AS count
FROM " . TABLE_PROPERTY
. " WHERE property_status = '1'
GROUP BY 'property_location'");
while ($row = mysql_fetch_array($q)) {
$the_data[$row['property_location']] = $row['count'];
}
var_dump($the_data); // show the data structure
while ($row = mysql_fetch_array($q)) {
$the_data[$row['property_location']] = $row['count'];
var_dump($the_data); // am I getting the data?
}
Are you **sure** the query works outside of php? Try from the command line. You're getting one row, which tells me there's only one row to be seen, or your query isn't returning the data you think it should.
Sean
If anyone else is stuck with a similar type of query, here is what worked for me.
$query = "SELECT property_location, COUNT(property_id) FROM " . TABLE_PROPERTY . " where property_status = '1' GROUP BY property_location";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
$the_data[$row['property_location']] = $row['COUNT(property_id)'];
}
echo $the_data['Dublin']; //show dublin data
var_dump($the_data); // show the data structure
Thanks again all.