0

mysql テーブルから値を取得して、ドロップダウン エリアを作成したいと考えていました。このコードは機能していないようです。印刷されるのは、コンテンツのないドロップダウンのボックスだけです。どうすればこれを機能させることができますか? またはこれを行う代替手順はありますか?

<?
$connection = mysql_connect("localhost", "root", "");
mysql_select_db("test", $connection);
$query = "SELECT full_name FROM test";
$names = mysql_query($query);
function dropDown($content, $result)
 {
while($row = mysql_fetch_row($result))
    {
        $name = $row[0];
        $content .= "<option value='$name'>$name</option>";
    }
 }
$content=<<<EOT
           <html>
              <body>
                 <select name="name">
EOT;

dropDown($content, $names)

$content.=<<<EOT
                 </select>
              </body>   
            </html>          


EOT;

echo $content;
?>
4

3 に答える 3

2

return文字列。PHP は、便利な場合があるという理由だけでoutパラメータを使用する C ではありません。

function dropDown($result, $fieldName)
{
    $content = '';
    while($row = mysql_fetch_row($result)) {
        $name = $row[0];
        $content .= "<option value='$name'>$name</option>";
    }
    return '<select name="'.$fieldName.'">'.$content.'</select>';
}

$content = '<html><body>';
$content .= dropDown($names, 'name');
于 2012-05-03T06:19:16.073 に答える
0
<?php echo $form->dropDownList($model,'courseprefer', array(

'(*value in database table*'=>'displaying value',
 )); ?>

配列の最初の値がデータベーステーブルにあることを確認するだけで、手動で追加できます.. :)

エラーを避けるために!

于 2012-05-03T07:46:22.587 に答える
0
    here database details      
  mysql_connect('hostname', 'username', 'password');
mysql_select_db('database-name');

     $sql = "SELECT username FROM userregistraton";
        $result = mysql_query($sql);

         echo "<select name='username'>";
  while ($row = mysql_fetch_array($result)) {
   echo "<option value='" . $row['username'] ."'>" . $row['username'] ."</option>";}
   echo "</select>";


      here username is the column of my table(userregistration)
     it works perfectly

or simply the code is 
        <?
         $sql = "SELECT * FROM userregistraton ";
             $result = mysql_query($sql); ?>


 <select name="username" id="username">
<option value="">Select user</option>
    <?php
        while ($row = mysql_fetch_array($result))
        { ?>
          <option value="<?php echo $row['uid']?>"         <?php    if($row['uid']==$_POST["username"]) echo "selected"; ?> >
   <?php echo $row['username'];?></option>
   <?php
         } ?>

于 2012-06-09T10:20:16.300 に答える