通过使用 php 在字段中输入数字来自动填充下拉菜单



我是 php 和 html 的新手,所以请原谅我任何愚蠢的言论。我正在尝试在空白字段中输入数字后自动填充下拉菜单。

我在谷歌上环顾四周,但我找不到太多关于 PHP 的自动填充,只提交一个表单。 现在我有一个SQL查询。当我想输出查询结果时,它可以显示为数组的键或值:

(在我看来(

<?php
foreach($data as $key)
{
print_r($key['name']); 
echo '<br/>';
print_r($key['id']);
echo <br/>;

} ?>

我想基本上说,如果选择第一个键,将显示第一个值。我假设我可能需要某种 if 语句,不确定还有什么。

将 FuelPHP 用于我的后端

我的模型

public static function get_results()
{
$result = DB::query('select substring(cat.item_class_cd,1,1) as id, cat.class_name as name
from MASTER_DB.MS_CATEGORY cat
where length(cat.item_class_cd) = 1')->execute();
// doesn't work
return $result;
//return ['id' => 1, 'name' => 'test']
}

我的控制器

public function action_index()
{
$model = new Model_Shelf();
$data = $model->get_results();
// create the layout view
$view = View::forge('shelf/index');
// assign global variables so all views have access to them
/* $view->data = $result; */

//assign views as variables, lazy rendering
$view->head = View::forge('common/head');
$view->header = View::forge('common/header');
$view->content = View::forge('common/content', array('data'=>$data));
$view->footer = View::forge('common/footer');
// return the view object to the Request
//return $view;
return Response::forge($view);
}

如果您需要任何澄清,请告诉我。我希望我足够清楚><</p>

编辑:澄清

我目前正在看 https://fuelphp.com/docs/classes/input.html 但我很困惑。你到底应该如何使用post($index = null,$default = null(?在我看来,这应该去吗?

<label for="category">Category</label>
<form action="content.php" method="post">
<input type="text" name="category" <?php Input::post($data['id']) ?>><br/>

data['id'] 是类别的 ID 号,data['name'] 应该是类别的名称。

<div class=" form-group">
<select class="form-control form-control-sm">
<?php
Input::get(data['id']);
foreach($data as $option){
<option>$option['name']</option>
}
?>
</select>

我的代码可能有点混乱。我想做的是,如果选择了 0 到 6,它将使用所选类别 ID 号的类别名称填充下拉列表。

如果您需要在数据库中搜索 - 使用 LIKE 条件。 例如:从some_table中选择 *,其中your_field类似于"%2%" (其中 2 - 您尝试查找的数字(。 % 需要制作条件:2 之前的任何数字和 2 之后的任何数字。所以它可以找到例如:132、231 等。

也许我不太了解这个问题,但我认为,如果您使用 LancelHunt 上面显示的提示获得了所有数据,您只需要在下拉菜单中使用 foreach 打印结果,其中回显将在处理下拉项的标签内完成。

最新更新