질문에 혼란 스럽지만 SQL 문의 SELECT *는 데이터베이스의 모든 열이 $ row 배열의 key => value 쌍으로 나타나야한다는 것을 의미합니다. 따라서 여기에 HTML 목록 요소 <li>
에 다른 "열"이 필요하면 해당 열 이름을 배열 키로 표시합니다 (메모 : "인쇄"하지 않음). 당신이 이름을 "모델"과 열에있는 자동차 컬럼의 종류를 필요에 따라서 당신은이 작업을 수행 할 것 :
<?php
$sql = "SELECT * FROM apparatus ORDER BY vehicleType";
$getSQL = mysql_query($sql);
// transform the result set:
$data = array();
while ($row = mysql_fetch_assoc($getSQL)) {
$data[$row['vehicleType']][] = $row;
}
?>
<?php foreach ($data as $type => $rows): ?>
<h2><?php echo $type?></h2>
<ul>
<?php foreach ($rows as $vehicleData):?>
<li><?php echo $vehicleData['name'];?></li>
<li><?php echo $vehicleData['model'];?></li>
<?php endforeach ?>
</ul>
<?php endforeach ?>
편집 : 난 아직도 당신의 질문에 불분명 합니다만, 모든 차의 경우 같은 vehicleType을 가지고, 당신은 그냥 한 번 모든 결과를 통해 반복하기 전에, 나는이 그것을 할 것 같은데요 잡아 찾고 :
<?php
// Set up a SQL query to grab one row
$query_to_grab_only_vehicle_type = "SELECT vehicleType FROM apparatus WHERE 1 LIMIT 0,1";
// Fetch that row and turn it into an array
$vehicle_type_array = mysql_fetch_array(mysql_query($query_to_grab_only_vehicle_type));
// Initialize a variable with the array value that came from the vehicleType column
$vehicle_type = $vehicle_type_array['vehicleType'];
// You could uncomment and use the following to echo out the variable
// echo "Vehicle type: $vehicle_type";
$sql = "SELECT * FROM apparatus ORDER BY vehicleType";
$getSQL = mysql_query($sql);
// transform the result set:
$data = array();
while ($row = mysql_fetch_assoc($getSQL)) {
$data[$row['vehicleType']][] = $row;
}
?>
<?php foreach ($data as $type => $rows): ?>
<h2><?php echo $type?></h2>
<ul>
<?php foreach ($rows as $vehicleData):?>
<li><?php echo $vehicleData['name'];?></li>
<li><?php echo $vehicleData['model'];?></li>
<?php endforeach ?>
</ul>
<?php endforeach ?>
를 수행하면'$ vehicleData' 배열이 기계를 포함 깨닫지 세부? 따라서 모델은 $ rows –
이 아니라이 배열에 저장되어야합니다. 별개의 배열을 의미합니까? 배열에 대해 정말 고심하여 어떤 설명을해도 괜찮습니다. – bsandrabr