Char
Char

Reputation: 318

Fetching something from a table using information from another table

This is for a real estate website - they display thousands of ads stored inside a database - sadly the photos and the ads are stored in two different tables in the database. The information about the properties are under a table called properties - the images are stored in a table called propertyImages - the one thing they have in common is their propspaceId - this is how they are linked.

Some properties have more than one image - lets say an ad with four photos will have ONE unique propspaceID for that ad (inside the properties table) - but the photos that belong to that ad inside the propertyImages table has that ID for all the photos that go with that ad.

Basically, ALL ads have a ranking number. This ranking number is made up of different factors and I've been able to make this work. However, one of the factors is the number of images each advert has - the old programmer wrote this code:

$property['totalImages'] = mysql_num_rows(mysql_query("SELECT * FROM `propertyImages` WHERE propspaceId=".$property['propspaceId']));

I can't seem to understand this, or write a new one. This code doesn't work. Then, later in the code when I write the conditions, I wrote something like this.

switch($property['totalImages']){
      case $property['totalImages'] >= 1 :
          $rank+=100;
          break;
  }

..etc.

The second code isn't the problem. I need to find a way to fetch the number of photos inside the propertyImages table with matching propspaceId(s) and then for example if there are 4 recurring values for the propspaceId = 45666, I want the variable to be 4.

Upvotes: 0

Views: 37

Answers (2)

AbraCadaver
AbraCadaver

Reputation: 78994

The second code is the problem. You either need to convert it to an if or do it like this:

switch(true) {

      case ($property['totalImages'] >= 1):
          $rank += 100;
          break;
}

In your switch you are comparing the integer value of $property['totalImages'] to the boolean result of $property['totalImages'] >= 1.

To use an if something like:

if($property['totalImages'] >= 1) {
          $rank += 100;
}
elseif($property['totalImages'] == 0) {
          //rank = something else
}
//other stuff, you didn't show more than 1 condition

Upvotes: 1

Bogdan
Bogdan

Reputation: 397

Try this:

select count(*) as number_photos from propertyImages where propspaceId = ?

Upvotes: 0

Related Questions