回显已发布 ID 的行名称,用于预选的表单值
希望这会是一件容易的事,但我缺乏技能!
<select name="search_category" id="select1" >
<option value="">By Category</option>
<?php if (!empty($_POST['search_category'])) { ?>
<option value="<?php echo $_POST['search_category']; ?>" selected="selected"><?php echo $_POST['search_category']; ?></option>
<?php }?>
<?php foreach($categoriesListt as $row) : ?>
<option value="<?php echo $row->id; ?>"><?php echo $row->name; ?></option>
<?php endforeach; ?>
</select>
以上是搜索模块中的众多选择之一。它从我的页面上方的查询返回一个动态选项列表。我的目标是预先选择上次搜索的选项。一切都按预期进行,但我的问题实际上很小;发布的搜索类别的值是一个 ID($row->id。我希望做的是使用关联的 $row->name 进行显示,但保留 id 作为值,以便我的搜索功能仍然有效。
换句话说,我希望做类似的事情:
<?php echo $row->name; WHERE ID = $_POST['search_category']
在上面的代码中是否有一种简单的方法可以做到这一点,或者我需要在页面顶部添加一个特殊的查询,获取与发布ID?
!
谢谢 简化一下,我已经有一个返回 row->id 和 row->name 的查询,我在 foreach 循环中使用它来填充我的选项值和名称,我只需要一种可以添加到的方法或行。我的查询还获取与 POSTED id 匹配的 row->name 的值。
This will hopefully be an easy one, but I'm lacking the skills!
<select name="search_category" id="select1" >
<option value="">By Category</option>
<?php if (!empty($_POST['search_category'])) { ?>
<option value="<?php echo $_POST['search_category']; ?>" selected="selected"><?php echo $_POST['search_category']; ?></option>
<?php }?>
<?php foreach($categoriesListt as $row) : ?>
<option value="<?php echo $row->id; ?>"><?php echo $row->name; ?></option>
<?php endforeach; ?>
</select>
The above is one of many select in a search module. It returns a dynamic list of options from a query higher up in my page. My goal is to have the option last searched pre-selected. Everything works as intended, but my problem is minimal really; the value of the posted search category is an ID($row->id. What I am hoping to do is use the associated $row->name for display, but keep the id for value so my search function still works.
In other words, I'm hoping to do something like:
<?php echo $row->name; WHERE ID = $_POST['search_category']
Is there an easy way to do that in the above code, or will I need to add a special query at the top of my page, fetching the individual row name that matches the posted id?
Thanks!
EDIT: To simplify, I already have a query that returns row->id and row->name, which I use in a foreach loop to populate my option values and names. I simply need a way, or a line that I can add to my query to also get the value of the row->name that matches the POSTED id.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会编写一个简短的函数,即使对于其他应用程序也可以提供此功能。
就像
出于安全原因我建议使用准备好的语句或 mysql_escape
希望我能提供帮助
也许
您的意思是这样的或者您会选择下拉选项
i would write a short function which gives this functionality even for others applications.
just like
For security Reasons I would suggest to use Prepared Statements or mysql_escape
Hope i could help
Perhaps
you mean something like this or would you select the dropdown option