我有一个查询结果。
| id | name |
-----------------
| 3 | candi |
| 4 | gedangan |
| 11 | sidoarjo |
我的问题是如何将查询结果显示在单个输入字段中。
<input type="text" name="tags" id="" value="Candi, Gedangan, Sidoarjo" />
这是我的疑问。
<?php
$query = mysql_query("SELECT kecamatan.KECAMATAN_ID , kecamatan.KECAMATAN_NAMA
FROM sumber_air a
JOIN sumber_air_kecamatan ON a.ID_SUMBER=sumber_air_kecamatan.ID_SUMBER
JOIN kecamatan ON sumber_air_kecamatan.KECAMATAN_ID=kecamatan.KECAMATAN_ID
WHERE a.ID_SUMBER = " . $_GET['id'] . "'
GROUP BY kecamatan.KECAMATAN_ID");
while ($row = mysql_fetch_array($query)) {
<input type=text name=tags value=<?=$row['KECAMATAN_NAMA'];?> />
}
?>
答案 0 :(得分:2)
$value = "";
while ($row = mysql_fetch_array($query)) {
$value .= $row['KECAMATAN_NAMA'].", ";
}
echo '<input type="text" name="tags" value="'.$value.'" />';
答案 1 :(得分:0)
你可以这样做:
<?php
$query = mysql_query("SELECT kecamatan.KECAMATAN_ID , kecamatan.KECAMATAN_NAMA
FROM sumber_air a
JOIN sumber_air_kecamatan ON a.ID_SUMBER=sumber_air_kecamatan.ID_SUMBER
JOIN kecamatan ON sumber_air_kecamatan.KECAMATAN_ID=kecamatan.KECAMATAN_ID
WHERE a.ID_SUMBER = " . $_GET['id'] . "'
GROUP BY kecamatan.KECAMATAN_ID");
$tags = '';
//adds every "KECAMATAN_NAMA" to $tags, seperated by ", "
while ($row = mysql_fetch_array($query)) {
$tags .= $row['KECAMATAN_NAMA'] . ', ';
}
//rtim($tags, ', ') removes the last ", " from the tags
echo '<input type="text" name="tags" value="' . rtrim($tags, ', ') . '"/>';
?>
答案 2 :(得分:0)
$res = "";
while ($row = mysql_fetch_array($query)) {
// Build up a string of results
$res .= $row['KECAMATAN_NAMA']. ", ";
}
//Remove last comma
$res = rtrim($res, ', ');
//Echo result
echo '<input type="text" name="tags" value="'.$res.'" />';
答案 3 :(得分:0)
您可以将所有值收集到一个数组中,然后将它们与implode
$names_array = array();
while ($row = mysql_fetch_assoc($query)) {
$names_array[] = htmlentities($row['KECAMATAN_NAMA']);
}
$names = implode(', ', $names_array);
echo "<input type='text' name='tags' value='$names'/>";