使用条件语句按祖父母ID一次读取一条记录

时间:2018-10-27 18:54:05

标签: php database

我有3个表郊区,属性和图像。郊区是父母的餐桌 一对多关系中的属性表的图像是一个孩子 属性表具有一对多关系。

我想一次按郊区ID显示一张图像,并将其排序 (ORDER BY)按媒体资源ID。

迭代后,所有图像均正确显示。但是我不能 弄清楚如何使用条件语句一次显示每一行 郊区ID(大父表ID)。

下面是我尝试过不同决策的测试用例:

测试案例1:显示所有图像的迭代

$query = "SELECT i.*, p.property_id, s.suburb_id 
          FROM property_images i
          LEFT JOIN properties p
          ON i.property_id = p.property_id
          LEFT JOIN suburbs s
          ON p.suburb_id = s.suburb_id   
          WHERE s.suburb_id = 60                      
          ORDER BY p.property_id";

$result = $conn->query($query);

$images = array();

foreach($result as $row){
   extract($row);
   $images[] = $image_location . '<br />';
}

print_r($images);

/*
  Output:

  Array ( [0] => public/images/properties/uploads/1.jpg
  [1] => public/images/properties/uploads/2.jpg
  [2] => public/images/properties/uploads/3.jpg
  [3] => public/images/properties/uploads/4.jpg
  [4] => public/images/properties/uploads/5.jpg
  [5] => public/images/properties/uploads/6.jpg
  [6] => public/images/properties/uploads/7.jpg
  [7] => public/images/properties/uploads/8.jpg
  [8] => public/images/properties/uploads/9.jpg
  [9] => public/images/properties/uploads/10.jpg
  [10] => public/images/properties/uploads/11.jpg
  [11] => public/images/properties/uploads/12.jpg
  [12] => public/images/properties/uploads/13.jpg
  [13] => public/images/properties/uploads/14.jpg
  [14] => public/images/properties/uploads/15.jpg
  [15] => public/images/properties/uploads/16.jpg
  [16] => public/images/properties/uploads/17.jpg
  [17] => public/images/properties/uploads/18.jpg
)
*/

测试案例2:比较$ image_id是否等于其等效行

 $query = "SELECT i.*, p.property_id, s.suburb_id 
           FROM property_images i
           LEFT JOIN properties p
           ON i.property_id = p.property_id
           LEFT JOIN suburbs s
           ON p.suburb_id = s.suburb_id   
           WHERE s.suburb_id = 60                     
           ORDER BY p.property_id";

$counter = mysqli_num_rows($result);

$image_id = $row['property_image_id'];
$image_location = $row['image_location'];

if($counter > 0){
     if($image_id == 1){
         print_r($image_location);
     }
 }

 //Here the first row is returned 

/*
 Output:

  public/images/properties/uploads/1.jpg
*/

测试案例3:比较$ image_id(如果它等于行号2)

 $query = "SELECT i.*, p.property_id, s.suburb_id 
           FROM property_images i
           LEFT JOIN properties p
           ON i.property_id = p.property_id
           LEFT JOIN suburbs s
           ON p.suburb_id = s.suburb_id   
           WHERE s.suburb_id = 60                     
           ORDER BY p.property_id";

$counter = mysqli_num_rows($result);

$image_id = $row['property_image_id'];
$image_location = $row['image_location'];

if($conter > 0){
     if($image_id == 2){
         print_r($image_location);
     }
 }

 //Here it returns null/empty 

/*
 Output: (not output)

*/

我想知道为什么测试用例2返回第一行而测试用例3失败了?

我需要根据郊区ID为每个查询分别检索单个图像 (祖父母编号)

请考虑以下伪代码:

 1. if image ID is equal to 1, return the first row of ImageURL.
 2. if image ID is equal to 2, return the second row of ImageURL. 
 3. if image ID is equal to 3, return the third row of ImageURL.
 4. else return the first row.

我错误地忘记了在测试用例2和3中将结果分配到变量$ row中。当我尝试运行查询时,它返回了空结果集。

最好的方法是使用foreach循环从结果中检索数据并测试行的相等性。 在以下代码段中完成了以上方法。

$query = "SELECT i.*, p.property_id, s.suburb_id 
                      FROM property_images i
                      LEFT JOIN properties p
                      ON i.property_id = p.property_id
                      LEFT JOIN suburbs s
                      ON p.suburb_id = s.suburb_id   
                      WHERE s.suburb_id = 60                      
                      ORDER BY p.property_id";
            $result = $conn->query($query);

            $images = array();


            foreach($result as $row){
                extract($row);
                //$images[] = $image_location . '<br />';
                if($property_image_id == 1){
                    print_r($image_location);
                }else if($property_image_id == 2){
                    print_r($image_location);
                }else if($property_image_id == 3){
                    print_r($image_location);
                }
            }

0 个答案:

没有答案