[英]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. 有关属性的信息位于称为属性的表下-图像存储在名为
propertyImages
的表中-它们的共同点是其propspaceId
这就是它们如何链接的方式。
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. 某些属性包含一张以上的图片-假设一个包含四张照片的广告在该广告中具有一个唯一的
propspaceID
(在属性表中)-但是在propertyImages表中属于该广告的照片具有该ID的所有照片放那个广告。
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. 我需要找到一种方法来获取具有匹配
propspaceId
( s
)的propertyImages
表中的照片数量,然后例如,如果propspaceId = 45666
有4个重复出现的值,我希望该变量为4。
The second code is the problem. 第二个代码就是问题所在。 You either need to convert it to an
if
or do it like this: 您需要将其转换为
if
或执行以下操作:
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
. 在您的
switch
您将$property['totalImages']
的整数值与$property['totalImages'] >= 1
的布尔结果进行比较。
To use an if
something like: 要使用
if
,例如:
if($property['totalImages'] >= 1) {
$rank += 100;
}
elseif($property['totalImages'] == 0) {
//rank = something else
}
//other stuff, you didn't show more than 1 condition
尝试这个:
select count(*) as number_photos from propertyImages where propspaceId = ?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.