HTML 选择选项中的 Foreach php 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16034281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Foreach php function inside HTML select options
提问by Daniel Garcia
Im a newbie to this forum and have just started coding in php. Need some help. I have the following code
我是这个论坛的新手,刚刚开始用 php 编码。需要一些帮助。我有以下代码
<?php error_reporting(-1);
require_once('/mysql/sql_connect.php');
$q = "SELECT pty.pty_profile_name AS profile FROM pty, users WHERE users.username = 'testaccount' ";
$r = @mysqli_query ($dbc, $q);
$results_array = array();
while($row = mysqli_fetch_assoc($r)) {
array_push($results_array, $row);
echo "<pre>"; print_r($results_array); echo "</pre>"; }
?>
<p>
<form method="post" action="foreach2.php">
<label for="Property Select" class="title">Select Property</label>
<select name="pty_select" >
<?php foreach($results_array as $key => $value){ ?>
<option value="<?php echo $key; ?>"><?php echo $value['profile']; ?></option>
<?php } ?>
</select>
<input type="submit" name="Submit" />
</form>
<?php
if (isset($_POST['Submit'])) {
echo "<pre>"; echo ($_POST['pty_select']); echo "</pre>"; } ?>
The output I get is correct, but it displays the key, eg, 0 or 1 or 2, based on what I select in the form. I need the value output. Eg 0 = Emerton
, it outputs "0" instead of Emerton
.
我得到的输出是正确的,但它会根据我在表单中选择的内容显示密钥,例如 0 或 1 或 2。我需要值输出。例如0 = Emerton
,它输出“0”而不是Emerton
。
If I echo $value['profile']
instead of pty_select
, I get the last result of the query all the time. Which in this example would be 2, which is Ambarvale
as I believe it just chooses the last row output of the query.
如果我 echo$value['profile']
而不是pty_select
,我总是得到查询的最后结果。在这个例子中是 2,Ambarvale
我相信它只是选择查询的最后一行输出。
I hope I've made sense. Thanks in advance.
我希望我说得有道理。提前致谢。
采纳答案by hsuk
You mean you need the value what you have used to display it. Then, Change to :
您的意思是您需要用于显示它的值。然后,更改为:
<option value="<?php echo $value['profile']; ?>">
<?php echo $value['profile']; ?>
</option>
回答by saveATcode
It will obviously echo the key, as you assigned the value of options as $key
if you need the options in the $_POST['pty_select']
use this:
它显然会回显键,因为您分配了选项的值,就$key
好像您在$_POST['pty_select']
使用中需要这些选项一样:
<select name="pty_select" >
<?php foreach($results_array as $key => $value){ ?>
<option value="<?php echo $value['profile'];?>"><?php echo $value['profile']; ?></option>
<?php } ?>
</select>
回答by Vyktor
And now let's go to ideal world :)
现在让我们进入理想世界:)
Build data pairs database_id => name
for options:
database_id => name
为选项构建数据对:
$q = "SELECT pty.id, pty.pty_profile_name AS profile FROM pty, users
WHERE users.username = 'testaccount'";
$r = mysqli_query($dbc, $q);
$values = array();
while($r = mysqli_fetch_row($r)) {
$values[$r[0]] = $r[1];
}
Never use @
when working with database, why do you want to suppress errors instead of preventing/handling them?
永远不要@
在使用数据库时使用,为什么要抑制错误而不是防止/处理它们?
Now you have real database IDs and respective values (in general, using unique IDs are better... if nothing else they have greater entropy - more efficient search). And sice displaying select box is really common in webs, lets:
现在您有了真实的数据库 ID 和相应的值(一般来说,使用唯一 ID 更好……如果没有别的,它们具有更大的熵 - 更有效的搜索)。并且 sice 显示选择框在网络中非常常见,让我们:
function selectbox( $values = array(), $attributes = array(), $selected_value = null)
{
// Header
echo '<select';
foreach( $attributes as $key => $val){
echo ' ' . htmlspecialchars($key) . '="' . htmlspecialchars( $val) . '"';
}
echo '>';
// Values
foreach( $values as $key => $val){
echo '<option value="' . htmlspecialchars( $key) .'"';
if( $key === $selected_value){
echo ' selected="selected"';
}
echo '>' . htmlspecialchars( $val) . '</option>';
}
echo '</select>';
}
And now usage :)
现在使用:)
<form method="post" action="foreach2.php">
<label for="Property Select" class="title">Select Property</label>
<?php selectbox( $values, array( 'name' => 'pty_select')); ?>
<input type="submit" name="Submit" />
</form>
And what to do with it then?
那怎么办呢?
$id = (int)(isset( $_POST['pty_select']) ? $_POST['pty_select'] : 0);
$name = null;
if( $id && isset( $values[$id])){
$name = $values[$id];
}
回答by chandresh_cool
Give
给
<option value="<?php echo $value['profile']; ?>"><?php echo $value['profile']; ?></option>
instead of
代替
<option value="<?php echo $key; ?>"><?php echo $value['profile']; ?></option>
回答by EJTH
if (isset($_POST['Submit'])) {
echo "<pre>"; echo ($_POST['pty_select']); echo "</pre>"; } ?>
Change it to something like
将其更改为类似
if(isset($_POST['Submit'])) {
echo $results_array[$_POST['pty_select']]['profile'];
}
Or alternatively use profile option value.
或者使用配置文件选项值。