我正在努力在过去3到4天内显示这个多维阵列无济于事。
我需要做的就是显示如下:
carName:
carImage:
天:
金额://这是我面临的问题,显示与特定汽车相关的所有日期和数量。一辆汽车将有几天和每天报价。
//请查看我的代码并帮助...谢谢
<?php
mysql_select_db($database);
$query_showall = "SELECT rental.*,
car_name.*,
gallery.*,
car_make.*
FROM rental,
car_name,
gallery,
car_make
WHERE car_name.carName_id = gallery.carName_id
AND car_name.carMake_id = car_make.carMake_id
AND rental.carName_id = car_name.carName_id
GROUP BY rental.carName_id";
$result_showall = mysql_query($query_showall) or die(mysql_error());
while($row_showall = mysql_fetch_array($result_showall)) {
$carMake_all = $row_showall['carName'];
$carmake_1[$row_showall['carName_id']][] = $row_showall;
}
foreach($carmake_1 as $make_1=>$name_1) {
foreach($name_1 as $n_1) {
echo $n_1['carName'].'<br/>';
echo $n_1['gallery'].'<br/>';
/* I need to loop through the rental table
to retrieve num of days and amount for
each car here.. */
echo $n_1['rental_days'].'<br/>';
echo $n_1['rental_amount'].'<br/>';
}
}
?>
不使用GROUP BY编辑但是如何停止carName和imageName不重复?
<?php
mysql_select_db($database);
$query_showall="SELECT rental.*,car_name.*,gallery.*,car_make.* FROM rental,car_name,gallery,car_make WHERE car_name.carName_id=gallery.carName_id AND
car_name.carMake_id=car_make.carMake_id AND rental.carName_id=car_name.carName_id ORDER BY rental_days ASC";
$result_showall=mysql_query($query_showall)or die(mysql_error());
while($row_showall=mysql_fetch_array($result_showall))
{
$carMake_all=$row_showall['carName'];
$carmake_1[$row_showall['carName_id']][]=$row_showall;
}
foreach($carmake_1 as $make_1=>$name_1)
{
foreach($name_1 as $n_1)
{
echo $n_1['carName'].'<br/>';
echo $n_1['gallery'].'<br/>';
echo $n_1['rental_days'].'<br/>';
echo $n_1['rental_amount'].'<br/>';
}
}
?>
答案 0 :(得分:0)
好的,我会对此采取行动。我要做的是在你尝试将数据转储出来之前重建数组,以便按照你希望的方式组织你的所有数据,这样在foreach的每一步你都只能显示你想要的数据。
此代码将使用没有group by的SQL语句,如您所述,其中包含所有数据。
// Start with an empty array...
$cars = array();
// Loop over all your results
while($row_showall = mysql_fetch_array($result_showall)) {
// Set name, if the ID comes up again, it will be reset but won't hurt anything
$cars[$row_showall['carName_id']]['name'] = $row_showall['carName'];
// Set gallery, if the ID comes up again, it will be reset but won't hurt anything
$cars[$row_showall['carName_id']]['gallery'] = $row_showall['gallery'];
// Now, we add a new property called 'details' and put a new array in...
$cars[$row_showall['carName_id']]['details'][] = array(
// Storing a unique day
'days' => ['rental_days'],
// And a unique amount
'amount' => ['rental_amount']
);
}
// Loop over each entry in our array...
foreach($cars as $car) {
// Print out the name, once
echo $car['name'] . '<br />';
// Print out the gallery, once
echo $car['gallery'] . '<br />';
// Loop over all the details...
foreach($car['details'] as $detail) {
// Print out each day
echo $detail['days'] . '<br />';
// Print out each amount
echo $detail['amount'] . '<br />';
}
}