dtmu88600 2013-04-16 10:09
浏览 122
已采纳

在HTML选择选项中的Foreach php函数

Im a newbie to this forum and have just started coding in php. Need some help. I have the following code

<?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.

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.

I hope I've made sense. Thanks in advance.

  • 写回答

5条回答 默认 最新

  • douweng1935 2013-04-16 10:12
    关注

    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> 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100