配对使用其他表数据我遇到了一些问题。请看我的代码:
$sql = mysqli_query($con, "SELECT * FROM properties WHERE category like '$property_for' AND property_type like '$property_type'
AND search_radius >= '$area' AND price >= '$price_min'
AND price <= '$price_max' AND bed_rooms like '$beds' AND bath_rooms like '$baths'
ORDER BY date_added DESC") or die(mysqli_error($con));
$productCount = mysqli_num_rows($sql);
if ($productCount > 0)
{
while($row = mysqli_fetch_array($sql))
{
$pid = $row["property_id"];
$agent_id = $row["agent_id"];
$property_name = $row["property_name"];
$category = $row["category"];
$location = $row["location"];
$property_type=$row["property_type"];
$price = $row["price"];
$bed_rooms = $row["bed_rooms"];
$bath_rooms = $row["bath_rooms"];
$commercial_type = $row["commercial_type"];
$area = $row["area"];
$address = $row["address"];
$description = $row["description"];
$date_added = $row["date_added"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
$imagesql = mysqli_query("SELECT * from tbl_images WHERE pid = '$pid'");
while($row = mysqli_fetch_array($imagesql))
{
$image = row['image_name'];
// Here there are totally 3 image_names. I want to fetch all the image names
}
}
}
我在这里使用两个名为properties
和tbl_images
的表。在properties
中,我插入了类别,价格,大小等所有数据。都是单一的价值观。但在tbl_images
中,我插入了3 image_names
,引用了properties
表的product_id
。所以,现在我需要获取properties
表中的所有数据以及image_names
表中的所有3 tbl_images
,并在property_id
表中引用properties
。这是我的问题。所以,Mates帮助我......我认为Array会帮助这个脚本。但我不知道在哪里以及如何做到这一点。所以请帮助我。
答案 0 :(得分:0)
$imagesql = mysqli_query("SELECT * from tbl_images WHERE pid = '$pid'");
$images = array();
while($i = mysqli_fetch_array($imagesql)) {
$images[] = $i['image_name'];
}
var_dump($images); //will contain an array of all images
答案 1 :(得分:0)
There is two while
loops using the variable with the same name $row
in your code. The variable in the second loop will override the variable in the first one. Change $row
in the second loop to $row2
.
Next, you can use two-dimensional array of images, i.e. $images[$pid][]
instead of $image
. This way is preferred if the number of images per property in tbl_images
may increase in future.
while($row2 = mysqli_fetch_array($imagesql))
{
$image[$pid][] = row['image_name'];
}
Or, if the number of images in tbl_images
is fixed and equals to 3 per property
and will not increase in future, you can merge two queries into the big one. To do it you should add new column to the tbl_images
, for example type
with values 1, 2 and 3 for each image. Here is code:
tbl_images
structure:id image_name created_time pid type
1 first 2015-01-01 1 1
2 second 2015-01-02 1 2
3 third 2015-01-03 1 3
4 alpha 2015-01-04 1 1
5 beta 2015-01-05 1 2
6 gamma 2015-01-06 1 3
...
$sql = mysqli_query($con, "SELECT a.*, b.image_name as image1, c.image_name as image2, d.image_name as image3 FROM properties WHERE a.category like '$property_for' AND a.property_type like '$property_type' AND a.search_radius >= '$area' AND a.price >= '$price_min' AND a.price <= '$price_max' AND a.bed_rooms like '$beds' AND a.bath_rooms like '$baths' LEFT JOIN `tbl_images` b ON b.pid = a.property_id AND b.type = 1 LEFT JOIN `tbl_images` c ON c.pid = a.property_id AND c.type = 2 LEFT JOIN `tbl_images` d ON d.pid = a.property_id AND d.type = ORDER BY a.date_added DESC") or die(mysqli_error($con));
if ($productCount > 0)
{
while($row = mysqli_fetch_array($sql))
{
$pid = $row["property_id"];
$agent_id = $row["agent_id"];
$property_name = $row["property_name"];
$category = $row["category"];
$location = $row["location"];
$property_type=$row["property_type"];
$price = $row["price"];
$bed_rooms = $row["bed_rooms"];
$bath_rooms = $row["bath_rooms"];
$commercial_type = $row["commercial_type"];
$area = $row["area"];
$address = $row["address"];
$description = $row["description"];
$date_added = $row["date_added"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
$image1 = $row['image1'];
$image2 = $row['image2'];
$image3 = $row['image3'];
}
}