用不同的图像显示相同的产品。。。PHP
我正试图建立一个网站,让人们发布他们的汽车出售。我已将数据发布到数据库中。一切正常,但当他们进入会员页面时,应该会显示添加了照片的汽车和编辑按钮 以下是我显示新增车辆的功能:用不同的图像显示相同的产品。。。PHP,php,Php,我正试图建立一个网站,让人们发布他们的汽车出售。我已将数据发布到数据库中。一切正常,但当他们进入会员页面时,应该会显示添加了照片的汽车和编辑按钮 以下是我显示新增车辆的功能: function get_user_auto($username) { //extract from the database all the URLs this user has stored $conn = db_connect(); $result = $conn->query(" SELECT images
function get_user_auto($username) {
//extract from the database all the URLs this user has stored
$conn = db_connect();
$result = $conn->query("
SELECT
images.filename,
car_make.make,
car_model.model,
car_generation.name,
cars.year,
cars.price,
cars.car_id
FROM cars
LEFT JOIN car_make ON cars.make = car_make.make_id
LEFT JOIN car_model ON cars.model = car_model.model_id
LEFT JOIN car_generation ON cars.generation = car_generation.generation_id
LEFT JOIN images ON cars.car_id = images.car_id
WHERE cars.username = '$username';");
if (!$result) {
return false;
}
//create an array of the URLs
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$make = $row["make"];
$model = $row["model"];
$generation = $row["name"];
$year = $row["year"];
$price = $row["price"];
$filename = $row["filename"];
$imageURL = 'uploads/'.$filename;
?>
<img src="<?php echo $imageURL; ?>" alt="" width="100"/>
</div>
<div>
<?php echo "" . $make ." " . $model ." - " . $year ." " . $price ." USD";?>
<a href="edit_car_form.php">Edit</a>
</div>
</div>
<?php
}
} else {
echo "You did not add car for sale yet.";
}
}
例如,我添加了一辆车,车上有3张照片,这些照片是通过车id附加到车上的,当我检索数据并回显时,它会用3张不同的照片显示同一辆车3次。如果你只想每辆车有一行,那么你可以添加GROUP by来进行类似的查询
按车号分组。这将只允许每个车号有一行,无论车号有多少张图像,但它可能不会选择您喜欢的图像。这似乎是SQL查询问题,而不是PHP问题。查询返回什么?@Volvox好吧,它返回3倍于3个不同的图像文件名,但相同的汽车品牌、型号、年份、价格和id。