I am building a Leasing Company Property web site, and one option they'd like is to have "Similar Properties" recommended to the visitor.

The array contains "Bedroom Type" data (studio, 1 bedroom, 2 bedroom, 3 bedroom).

A sample array is: Array ( [0] => Studio [1] => 1BR )

I am trying to figure out a way to retrieve other buildings that "match" similar bedroom types (if a building has a Studio type, I'd like to list that building).

Any idea on how I can start writing this? Any and all advice is greatly appreciated, I've never had to do any work like this before so I have no starting point.

Member Avatar for diafol

This type of work (querying against parameters) is best left to a database. Arrays are a damn pain. DBs are nicer as they allow you to relate and join data. They are easier for updating, deleting, inserting and even selecting.

Thanks ardav, I was afraid that was going to be the answer. Using DB queries is always my first choice, but with the type of functionality I'm attempting to make, I want to display all the properties, and then compare them to the original bedroom type array.

If the arrays are "similar" (ie: they have a bedroom type in common), I want their checkbox to be selected, if they don't have anything similar (ie: they have absolutely no bedroom type in common), then the property will not be checked.

http://www.millenniumparkplaza.com/bjb/site/view.php?tabs-5&property=12 -- this is where I am attempting to write this functionality.

Thank you ardav! You're always incredibly helpful (love the experts).

Member Avatar for diafol

Too kind stoop! :)

I see what you're saying from the link. However, I still assert that a DB would be the easiest, although the structure may look a little daunting. Creating (maybe) hundreds of arrays for different properties would fill me with a dark dread.
Changing table fields is simple, change once, that's it. Alter a few queries and maybe a couple of form fields and you're done.

Trying to do the same for EVERY property... I don't even want to think... :(

I think you have a number of possible solutions to this. Find one that is maintainable. The last thing you want to do is get it just the way you want and then have to change it a couple of weeks down the line. If it's convoluted php, you'll probably spend a day or more trying to remember how you got all the different bits to interact!

commented: led me in the right direction +2
Member Avatar for diafol

Having read over your first post again, I may have got the wrong end of the stick:

Is your buildings data stored in a DB?

@ardav - thanks for your help. I picked my brain and came up with a Query Building string using the array_in() function.

$sql = "SELECT bedrooms FROM buildings WHERE bid = '$property'";
 $result = mysql_query($sql);
  while($r_b = mysql_fetch_array($result)) {
   $beds = unserialize($r_b['bedrooms']);
  }
  $location = "Gold Coast"; 
  $sql = "SELECT name, bedrooms FROM buildings WHERE location = '$location'";
  $c = 0; 
 
	if (in_array("Studio", $beds)) { 
	   $sql .= " AND (bedrooms LIKE '%Studio%'"; 
	   $c++;
	   }
	   
	if (in_array("1BR", $beds)) {
		if($c > 0) {
		 $sql .= " OR bedrooms LIKE '%1BR%'";
		 } else {
		 $sql .= " AND (bedrooms LIKE '%1BR%'";
		 $c++;
		 }
	}
	
	if (in_array("2BR", $beds)) {
		if($c > 0) {
		 $sql .= " OR bedrooms LIKE '%2BR%'";
		 } else {
		 $sql .= " AND (bedrooms LIKE '%2BR%'";
		 $c++;
		 }
	}
	
	if (in_array("3BR", $beds)) {
		if($c > 0) {
		 $sql .= " OR bedrooms LIKE '%3BR%'";
		 } else {
		 $sql .= " AND (bedrooms LIKE '%3BR%'";
		 $c++;
		 }
	}	
	  
    $sql .= ")";
    $result = mysql_query($sql);

Works like a charm now. You're such a smarty ardav =)

Member Avatar for diafol

What did I do?? Looks like you did all that yourself. Nice one. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.