English 中文(简体)
利用PHP从数据库中挑选出的退学选择
原标题:Get selected dropdown option from database using PHP
  • 时间:2011-04-11 02:21:50
  •  标签:
  • php
  • mysql

我有一个下调栏菜单,我需要根据数据库价值确定选定的价值。

我有一个表格,其结构如下:id, pid, pid, disporder, Owner, url

我当时正在使用这一法典来降低辍学率:

echo "<select name="parent" id="parent">
";
echo "<option value="0">No Parent</option>";
$query = $db->simple_select("navbar", "*", "pid= 0 ");
while($parent = $db->fetch_array($query))
{
    echo "<option value="".$parent[ id ]."">".$parent[ title ]."</option>";
}
echo "</select>";

如何根据数据库中的内容来获得选定的价值?

我的表格有多个条目,因此使用了具有价值(类似于这一点)的阵列,与我想要使用的是:

$options = array( 1 ,  2 ,  3 );
foreach($options as $option)
{
    if($option = $parent[ id ])
    {
        echo "selected";
    }
    else
    {
        echo "";
}

感谢。

问题回答

你没有真正给予足够的信息,以真正说什么确切的解决办法。 如果你重新在购买力平价中形成一个特定的标签,则标志建设的典型模式是:

<?php

$options = get_me_some_options();
$select_markup =  <select name="my-select" id="my-select> ;

foreach ($options as $key => $val) {
    $selected =   ;
    if (is_this_selected($val)) {
        $selected =  selected ;
    }
    $select_markup .= "<option $selected val="" 
        . $val[ id ] . "">" . $val[ name ] .  </option> ;
}

echo $select_markup . "</select>";

它像你一样看待使用问题,但较为复杂。 归根结底,尽管在路段内,你有某种办法确定某一行是否应当选定。

如果我正确理解,请将每一条“代码”[id]与一个叫做<代码>$/code>的阵列的数值进行比较。 为此:

$options = array( 1 ,  2 ,  3 );

echo "<select name="parent" id="parent">
";
echo "<option value="0">No Parent</option>";
$query = $db->simple_select("navbar", "*", "pid= 0 ");
while($parent = $db->fetch_array($query))
{
    $selected = ( in_array($parent[ id ], $options) ?   selected  :   ;
    echo "<option value="".$parent[ id ].""$selected>".$parent[ title ]."</option>";
}
echo "</select>";

如果你重新尝试在多种语言中利用这一手段,这可能有助于

<?php
$optionsToSelect=array(1,2,3);
?>
<select name="parent" id="parent">
<?php
foreach($allOptions as $opt){
?>
<option value="<?php echo $opt[ value ];?>" <? echo $opt[ selected ]==1? selected="selected" :  ;?>><?php echo $opt[ optTitle ];?><option>
<?php
}
?>
</select>




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签