简体   繁体   中英

Using php function within HTML select tag

I created a function in php, my goal with this function is to print the following line in a HTML select tag.

<option value="<?php echo $key . $product[$name . '_id'] == $key ? 'selected' : null  ?>"><?php echo $value ?></option>

This is what I've come up with:

function Select($name){
    $ids = array();
    $values = array();
    $query1 = $sql->query("SELECT * FROM '$name'");
    while($fetch = $query1->fetch_assoc()){
        array_push($ids, $fetch[$name . '_id']);
        array_push($values, $fetch[$name]);
    }
    $names = array_combine($ids, $values);

    foreach($names as $key => $value){
        return '<option value="' . $key . '"' . $product[$name . '_id'] == $key ? 'selected' : null . '>' . $value . '</option>';
    }
}

This does not seem to work, however when I put this directly in the HTML select tag it does work. It looks like this:

<select name="type" class="chozen" id="type">
    <?php
        $brand_ids = array();
        $brand_values = array();
        $query1 = $sql->query("SELECT * FROM brands");
        while($brand = $query1->fetch_assoc()){
            array_push($brand_ids, $brand['brand_id']);
            array_push($brand_values, $brand['brand']);
        }
        $brands = array_combine($brand_ids, $brand_values);

        foreach($brands as $key => $value){
            ?>
            <option value="<?php echo $key ?>"<?php echo $product['brand_id'] == $key ? 'selected' : null ?>><?php echo $value; ?></option>
            <?php
        }
    ?>
</select>

Can someone point me where I went wrong, I can not figure it out.

Your problem is that you're trying to return every single line separately. But your function won't continue after first return. this should work:

$str = '';
foreach($names as $key => $value){
    $str = $str.'<option value="' . $key . '"' . $product[$name . '_id'] == $key ? 'selected' : null . '>' . $value . '</option>';
}
return str;
$brands = array() ;
$query1 = $sql->query("SELECT * FROM brands");
while($brand = $query1->fetch_assoc()){
  $brands[$brand['brand_id']] = $brand ; //Just use brand_id as keys in the array
}

if (!empty($brands)): ?>
  <select name="type" class="chozen" id="type">
    <?php foreach($brands as $id => $brand): ?>
      <option value="<?php echo $brand['brand'] ; ?>"<?php echo ($brand['brand_id'] == $id ? 'selected' : ""); ?> ><?php echo $brand['brand'] ; ?></option>
    <?php endforeach ; ?>
  </select>
<?php else : ?>
  <div>No brands to display!</div>
<?php endif ; ?>

I think you can try the following line:

$str = "";
foreach($names as $key => $value){
   $str .= '<option value="' . $key . '" ' . (($product[$name . '_id'] == $key) ?     'selected'    : '') . '>' . $value . '</option>';
}
return $str;

1) use empty string instead of null. 2) Space after $key in value

hopefully it help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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