简体   繁体   English

将新选项添加​​到带有php值的select中

[英]add new option into select with values from php

I want to add a dynamic select list, which contain my data from php. 我想添加一个动态选择列表,其中包含来自php的数据。 how should i do it? 我该怎么办? i also wanted to have arguments with variables from other php file eg $mentor==$rows. 我也想让参数与其他php文件中的变量一起使用,例如$ mentor == $ rows。 appreciate it 欣赏它

<th><select name="mentor" style="width:95%">
        <?php
            $dbhost = 'localhost';
            $dbuser = 'root';
            $dbpass = '';
            $conn = mysql_connect($dbhost, $dbuser, $dbpass);
            mysql_select_db("testproject", $conn);

            if(! $conn )
            {
                die('Could not connect: ' . mysql_error());
            }

            $sql = "SELECT mtrname FROM mentors";
            $sql2 = "SHOW COLUMNS from mentors";
            $result= mysql_query( $sql, $conn );
            if(! $result)
            {
                die('Could not get data: ' . mysql_error());
            }
            $fetchrow = mysql_fetch_row($sql2);
            $num = count($fetchrow);
            while($rows = mysql_fetch_array($result))
            {
                for($i=0;$i<$num;$i++)
                {
                     $rows[$i];
                }
            }   
            mysql_close($conn);
        ?>
        <option value="<?php echo $mentor?>" <?php if($mentor==$rows[i]) echo 'selected'?>><?php echo $mentor?></option>
        </select></th>

edited: so i am trying to put select option into when the user first include the data. 编辑:因此,我试图在用户第一次包含数据时将选择选项放入。 what i've tried is <?php $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = ''; $conn = mysql_connect($dbhost, $dbuser, $dbpass); mysql_select_db("testproject", $conn); if(! $conn ) { die('Could not connect: ' . mysql_error()); } $sql = "SELECT mtrname FROM mentors"; $sql2 = "SHOW COLUMNS from mentors"; $result= mysql_query( $sql, $conn ); if(! $result) { die('Could not get data: ' . mysql_error()); } $fetchrow = mysql_fetch_row($sql2); $num = count($fetchrow); while($rows = mysql_fetch_array($result)) { for($i=0;$i<$num;$i++) { echo"<option value='".$rows[i].">".$rows[i]."</option>"; } }
mysql_close($conn); ?>
我试过的是<?php $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = ''; $conn = mysql_connect($dbhost, $dbuser, $dbpass); mysql_select_db("testproject", $conn); if(! $conn ) { die('Could not connect: ' . mysql_error()); } $sql = "SELECT mtrname FROM mentors"; $sql2 = "SHOW COLUMNS from mentors"; $result= mysql_query( $sql, $conn ); if(! $result) { die('Could not get data: ' . mysql_error()); } $fetchrow = mysql_fetch_row($sql2); $num = count($fetchrow); while($rows = mysql_fetch_array($result)) { for($i=0;$i<$num;$i++) { echo"<option value='".$rows[i].">".$rows[i]."</option>"; } }
mysql_close($conn); ?>
<?php $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = ''; $conn = mysql_connect($dbhost, $dbuser, $dbpass); mysql_select_db("testproject", $conn); if(! $conn ) { die('Could not connect: ' . mysql_error()); } $sql = "SELECT mtrname FROM mentors"; $sql2 = "SHOW COLUMNS from mentors"; $result= mysql_query( $sql, $conn ); if(! $result) { die('Could not get data: ' . mysql_error()); } $fetchrow = mysql_fetch_row($sql2); $num = count($fetchrow); while($rows = mysql_fetch_array($result)) { for($i=0;$i<$num;$i++) { echo"<option value='".$rows[i].">".$rows[i]."</option>"; } }
mysql_close($conn); ?>

but it doesnt display anything from the mentor table. 但它不会显示来自指导者表的任何内容。 is my code incorrect or its my mysql database issue? 我的代码不正确还是我的mysql数据库问题?

You should move from mysql since it's not deperecated and move towards PDO or mysqli. 您应该从mysql移走,因为它没有被淘汰,并转向PDO或mysqli。 If the results being returned are correct then you're pretty much there. 如果返回的结果是正确的,那么您就在那里。 Just move the option tag inside the for() loop. 只需将option标签移动到for()循环内即可。

while($rows = mysql_fetch_array($result))
{
    for($i=0;$i<$num;$i++)
    {
         echo "<option value='$mentor'". ($mentor==$rows[i] ? 'selected' : '') .">$mentor</option>";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM