我使用以下代码片段从数据库中获取记录:
if($pdo)
{
$stmt = $pdo->query('SELECT ID,Province,City,Position,Locale FROM jobs');
$result=$stmt->fetchAll(PDO::FETCH_ASSOC);
print("<pre>".print_r($result,true)."</pre>");
}
我正在获取阵列:
我想为每个循环检索值并在网站上显示:
Province name
city name:
position name1:
position name 2:
province name
city name:
Position name 3:
Position name 4:
我写过代码:
<?foreach($result as $key=>$val):?>
<div class='r-wr'>
<h2 class="region-heading"><?=$key?></h2>
<?foreach($val as $city=>$result):?>
<div class='c-wr'>
<h3 class="city-heading"><?=$city?></h3>
<?foreach($result as $pos_key=>$position):?>
<div class="pstn-btn" position-id="<?=$pos_key;?>"><?=$position;?></div><br>
<?endforeach;?>
</div>
<?endforeach;?>
</div>
<?endforeach;?>
任何人都可以帮助我如何编写每个循环来获取记录值。
由于
答案 0 :(得分:0)
foreach($ result as $ row){
echo“省名”。$ row ['省']。“
;
echo“City Name”。$ row ['City']。“
;
echo“Position Name”。$ row ['Position']。“
;
}
答案 1 :(得分:0)
foreach($result as $key=>$val){
$res[$val['Province']][$val['City']][$val['ID']] = $val['Position'];
}
输出结果为:
Array
(
[BC] => Array
(
[Winnipeg] => Array
(
[1] => Licensed Technician - Body Technician
)
)
[Prairies] => Array
(
[Winnipeg] => Array
(
[2] => Licensed Technician - Body Technician
)
[Edmonton] => Array
(
[3] => Tire Technician
)
)
[Ontario] => Array
(
[Regional Support Centre (Mississauga)] => Array
(
[4] => Customer Service Representative
[5] => Payroll and Benefits Administrator
)
[Mississauga] => Array
(
[6] => Shunt-Delivery Driver
[7] => Technician
)
)
)
答案 2 :(得分:0)
我运行演示
var height = $(".height").text();
结果:
<?php
$result = array(
array(
'ID' => 1,
'Province' => 'BC',
'City' => 'Winnipeg',
'Position' => 'Licensed Technician - Body Technician',
'Locale' => 'en'
),
array(
'ID' => 2,
'Province' => 'Prairies',
'City' => 'Winnipeg',
'Position' => 'Licensed Technician - Body Technician',
'Locale' => 'en'
),
array(
'ID' => 3,
'Province' => 'Prairies',
'City' => 'Edmonton',
'Position' => 'Tire Technician',
'Locale' => 'en'
),
array(
'ID' => 4,
'Province' => 'Ontario',
'City' => 'Regional Support Centre (Mississauga)',
'Position' => 'Payroll and Benefits Representative',
'Locale' => 'en'
),
array(
'ID' => 5,
'Province' => 'Ontario',
'City' => 'Regional Support Centre (Mississauga)',
'Position' => 'Payroll and Benefits Administrator',
'Locale' => 'en'
),
array(
'ID' => 6,
'Province' => 'Ontario',
'City' => 'Mississauga',
'Position' => 'Shunt-Delivery Driver',
'Locale' => 'en'
),
array(
'ID' => 7,
'Province' => 'Ontario',
'City' => 'Mississauga',
'Position' => 'Technician',
'Locale' => 'en'
)
);
$temp = array();
foreach ($result as $key => $value) {
$temp[$value['Province']][$value['City']][] = $value['Position'];
}
print_r($temp);
我希望它适合你
答案 3 :(得分:0)
因为我已经搞乱了PHP,但你可以使用array_reduce
来构建一个基于省的新哈希,并在该键下嵌套匹配记录,这样可以更容易地迭代:< / p>
<?php
$data = [array(
'id' => 1,
'Province' => 'Ontario',
'Position' => 'Customer Service',
), array(
'id' => 2,
'Province' => 'Ontario',
'Position' => 'Customer Service II',
), array(
'id' => 3,
'Province' => 'Edmunton',
'Position' => 'Janitor',
), array(
'id' => 4,
'Province' => 'Edmunton',
'Position' => 'Tire Technician',
)];
$provincePositions = array_reduce($data, function($carry, $item) {
if (array_key_exists($item['Province'], $carry)) {
array_push($carry[$item['Province']], $item);
} else {
$carry[$item['Province']] = [$item];
}
return $carry;
}, []);
foreach($provincePositions as $province => $positions) {
echo $province . "\n";
foreach ($positions as $position) {
echo "\t" . $position['Position'] . "\n";
}
}
输出结果为:
Ontario
Customer Service
Customer Service II
Edmunton
Janitor
Tire Technician