开发者

Echoing row name where posted ID, for pre-selected form value

开发者 https://www.devze.com 2023-04-12 09:09 出处:网络
This will hopefully be an easy one, but I\'m lacking the skills! <select name=\"search_category\"id=\"select1\" >

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开发者_C百科 }?>
  <?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.


i would write a short function which gives this functionality even for others applications. just like

<?php 

    function($id, $table) {

        select ... etc

    }
?>

For security Reasons I would suggest to use Prepared Statements or mysql_escape

Hope i could help

Perhaps

SELCET('id', 'name' FROM yourTable WHERE 'name' = $_POST['ID']) 

you mean something like this or would you select the dropdown option

<?php foreach($categoriesListt as $row) : ?> 
    <option value="<?php echo $row->id; ?>"
          <?php if($row->name == $_POST['search_category']) : ?>
               selected="selected"<?php } ?>>
        <?php echo $row->name; ?>
    </option> 
<?php endforeach; ?>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号