简体   繁体   English

使用另一个表中的信息从表中获取内容

[英]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. 我需要找到一种方法来获取具有匹配propspaceIds )的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.

相关问题 Laravel:如何使用 pivot 表从另一个表中检索信息? - Laravel : How to retrieve information from another table using a pivot table? 使用中间表从另一个表获取信息 - Get information from another table using intermediate table 从表中获取多行并将其添加到另一个表中,然后使用复选框从上一个表中删除 - Fetching Multiple Rows from Table and Adding Them into Another Table and then Remove from Previous Table, Using Checkboxes 使用外键laravel从另一个表中获取数据 - Fetching Data from another table using foreign key laravel 显示一个表中的信息,然后显示另一个表中的相关信息 - Displaying information from a table then showing related information from another table 从 1 个表中获取行并将其插入到另一个表中 - Fetching rows from 1 table and inserting it into another 通过从mysql中的另一个表中获取数据来从表中删除数据 - Delete data from a table by fetching data from another table in mysql 从其他网站获取信息? - Fetching information from another website? 如何使用PHP和MYSQL从表中获取信息到另一个页面? - How to get the information from table to another page using PHP and MYSQL? 使用PHP和MySQL将信息从一个表转移到另一个表 - Transferring information from one table to another using PHP and MySQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM