我的查询在 phpMyAdmin 中输出:
代码:
<?php
if ($stmt = $mysqli->prepare("SELECT service_names.id, service_names.name as service,
service_titles.name as title, service_titles.id as service_title_id FROM `service_names` INNER
JOIN service_titles ON title_id = service_titles.id WHERE service_titles.user_id = ? AND
service_titles.slide_id = ?")) {
$stmt->bind_param('ii', $user_id, $slide_id);
$stmt->execute();
$stmt->bind_result($service_id, $service_name, $service_title, $service_title_id);
$stmt->store_result();
$result_array = array();
while ($stmt->fetch()) {
if (!isset($result_array[$service_title])){
$result_array[$service_title] = array();
}
$result_array[$service_title][] = array('service_name'=>$service_name,'service_id'=>$service_id);
}
$html = "";
foreach($result_array as $key => $value){
$html .= "
<div class=\"list\">
<h3 class=\"secondary\"><span id={ GOES HERE }>$key</span></h3>
<ul>";
foreach($result_array[$key] as $service){
$html .= "<li><span id=\"".$service['service_id']."\">".$service['service_name']."</span></li>\n";
}
$html .= "</ul></div>";
}
echo $html;
$stmt->close();
}
?>
这段代码产生的是:
我需要的是抓住
service_title_id
并将其放入代码中:
<h3 class=\"secondary\"><span id={ GOES HERE }>$key</span></h3>
我正在使用 jQuery 内联编辑脚本,它需要分配一个唯一的 ID。我在变量 $service_title_id 中有值,这只是将其集成到数组中的问题,我对所有不同的数组和值有点困惑。
第一种方法需要对脚本进行最少的更改,是将
service_title_id
添加到每个服务并从数组的第一个元素获取它:
while ($stmt->fetch()) {
if (!isset($result_array[$service_title])){
$result_array[$service_title] = array();
}
$result_array[$service_title][] = array(
'service_name'=>$service_name,
'service_id'=>$service_id,
'service_title_id'=>$service_title_id
);
}
<h3 class=\"secondary\"><span id={$result_array[$key][0]['service_title_id']}>$key</span></h3>
其他更干净的方法是将保存所有服务的数组结构更改为如下所示:
while ($stmt->fetch()) {
if (!isset($result_array[$service_title])){
$result_array[$service_title_id] = array(
'service_title' => $service_title
'services' => array(),
);
}
$result_array[$service_title_id]['services'][] = array(
'service_name'=>$service_name,
'service_id'=>$service_id
);
}
$html = "";
foreach($result_array as $service_title_id => $service_details){
$html .= "
<div class=\"list\">
<h3 class=\"secondary\"><span id=\"{$service_title_id}\">{$service_details['title']}</span></h3>
<ul>";
foreach($service_details['services'] as $service){
$html .= "<li><span id=\"{$service['service_id']}\">{$service['service_name']}</span></li>\n";
}
$html .= "</ul></div>";
}
我认为关于拥有唯一 html id 的评论者是有道理的,但这里有一些改进的 PHP 供您使用。我重命名了你的变量以使事情更清晰
$html = "";
foreach($result_array as $service_title => $services){
$html .= "
<div class=\"list\">
<h3 class=\"secondary\"><span id={$services[0]['service_id']}>$service_title</span></h3>
<ul>";
foreach($services as $service){
$html .= "<li><span id=\"".$service['service_id']."\">".$service['service_name']."</span></li>\n";
}
$html .= "</ul></div>";
}
又快又脏,但会完成工作:
<?php
if ($stmt = $mysqli->prepare("SELECT service_names.id, service_names.name as service,
service_titles.name as title, service_titles.id as service_title_id FROM `service_names` INNER
JOIN service_titles ON title_id = service_titles.id WHERE service_titles.user_id = ? AND
service_titles.slide_id = ?")) {
$stmt->bind_param('ii', $user_id, $slide_id);
$stmt->execute();
$stmt->bind_result($service_id, $service_name, $service_title, $service_title_id);
$stmt->store_result();
$result_array = array();
while ($stmt->fetch()) {
if (!isset($result_array[$service_title])){
$result_array[$service_title] = array();
}
$result_array[$service_title . ':' . $service_id][] = $service_name;
}
$html = "";
foreach($result_array as $key => $value){
list ($title, $id) = explode(':', $key);
$html .= "
<div class=\"list\">
<h3 class=\"secondary\"><span id=\"$id\">$title</span></h3>
<ul>";
foreach($value as $service){
$html .= "<li><span>$service</span></li>\n";
}
$html .= "</ul></div>";
}
echo $html;
将循环更改为:
foreach($result_array as $key => $value){
$html .= "
<div class=\"list\">
<h3 class=\"secondary\"><span id=\"service_title_id_{$value[0]['service_title_id']}\">$key</span></h3>
<ul>";
foreach($value as $service){
$html .= "<li><span id=\"".$service['service_id']."\">".$service['service_name']."</span></li>\n";
}
$html .= "</ul></div>";
}
将此行更改为:
$result_array[$service_title][] = array('service_name'=>$service_name,'service_id'=>$service_id, 'service_title_id' => $service_title_id);
此行:
<h3 class=\"secondary\"><span id=\"".$."\">$result_array[$key]['service_title_id']</span></h3>
这有点多余,但基本上您将 service_title_id 放在数组的第二个维度中并从那里使用它。
使用
title
(或 title_id
)作为第一级键将整行推入新数组,根据查询结果构建分组数组。
$sql = <<<SQL
SELECT service_titles.id AS title_id,
service_titles.name AS title,
service_names.id AS service_id,
service_names.name AS service,
FROM service_names
JOIN service_titles ON title_id = service_titles.id
WHERE service_titles.user_id = ?
AND service_titles.slide_id = ?
SQL;
$resultSet = $mysqli->execute_query($sql, [$user_id, $slide_id]);
$grouped = [];
foreach ($resultSet as $row) {
$grouped[$row['title']][] = $row;
}
然后遍历填充的数组。空组不会存在,因此您可以安全地访问组第一行中的
[0]
来访问 title_id
值。
foreach ($grouped as $title => $rows) {
?>
<div class="list">
<h3 class="secondary">
<span id="<?php echo $rows[0]['title_id']; ?>">
<?php echo $title; ?>
</span>
</h3>
<ul>
<?php
foreach ($rows as $row) {
printf(
'<li><span id="%d">%s</span></li>' . "\n\t\t\t",
$row['service_id'],
$row['service']
);
}
?>
</ul>
</div>
<?php
}