如何从php中的数据库表创建表单下拉列表?

时间:2022-09-14 09:28:22

I am trying to code a function which creates a dropdown of school names selected from a database. It is doing fine creating a dropdown but it is not putting anything in the dropdown. Here is the code:

我正在尝试编写一个函数,该函数创建从数据库中选择的学校名称下拉列表。它正在创建一个下拉列表,但它没有在下拉列表中放置任何内容。这是代码:

function schoolDD($name, $selected){
   $select = '';
   if( $selected != null )
   {
      $select = $selected;
   }

     $qry = "select *   
             from   school
             order by name, id
             where display = 'Y'";

     $schools = _execQry($qry);


   $html = '<select name="'.$name.'" >';

   foreach( $schools as $s ){
      $html .= '<option value="'. $s['id'] .'"';
      if( $select == $s['name'] ){
         $html .= 'selected="selected"';
      }
      $html .= '>'. $s['name'] . '</option>';

   }
   $html .= '</select>';
   return $html;
}

3 个解决方案

#1


Problem solved. It was because in the query I had order before where. It should have been:

问题解决了。这是因为在我之前订购的查询中。应该是:

$qry = "select *   
             from   school
             where display = 'Y'
             order by name, id";

Not:

 $qry = "select *   
             from   school
             order by name, id
             where display = 'Y'";

#2


Looks OK but hard to say without knowing what _execQry does.

看起来不错,但很难说不知道_execQry的作用。

If you add the line

如果添加该行

print_r($schools);

after you call _execQry, are you definitely retrieving results from the database?

在你调用_execQry之后,你肯定是从数据库中检索结果吗?

#3


Im glad you found your answer, but i haaattee php and html mixed together like that.

我很高兴你找到了你的答案,但我讨厌将php和html混合在一起。

what about tsomethign like this...

怎么样这样的tsomethign ......

<?php

$schools = getSchools();
$selectedSchool = 'sfgsfgd';
$name = 'asdfsafd';

?>

<select name="<?= $name ?>">
<?php foreach( $schools as $s ): ?>
    <option value="<?= $s['id'] ?>"<?php if( $s['name'] == $selectedSchool ): ?> selected="selected"<?php endif; ?>><?= $s['name'] ?></option>
</select>

#1


Problem solved. It was because in the query I had order before where. It should have been:

问题解决了。这是因为在我之前订购的查询中。应该是:

$qry = "select *   
             from   school
             where display = 'Y'
             order by name, id";

Not:

 $qry = "select *   
             from   school
             order by name, id
             where display = 'Y'";

#2


Looks OK but hard to say without knowing what _execQry does.

看起来不错,但很难说不知道_execQry的作用。

If you add the line

如果添加该行

print_r($schools);

after you call _execQry, are you definitely retrieving results from the database?

在你调用_execQry之后,你肯定是从数据库中检索结果吗?

#3


Im glad you found your answer, but i haaattee php and html mixed together like that.

我很高兴你找到了你的答案,但我讨厌将php和html混合在一起。

what about tsomethign like this...

怎么样这样的tsomethign ......

<?php

$schools = getSchools();
$selectedSchool = 'sfgsfgd';
$name = 'asdfsafd';

?>

<select name="<?= $name ?>">
<?php foreach( $schools as $s ): ?>
    <option value="<?= $s['id'] ?>"<?php if( $s['name'] == $selectedSchool ): ?> selected="selected"<?php endif; ?>><?= $s['name'] ?></option>
</select>