<?php
global $wpdb;
$result = $wpdb->get_results( "SELECT name FROM student_reg");
foreach ($result as $row){
echo "<select name='name'>";
echo "<option value='" . $row->name . "'>" . $row->name . "</option>";
echo "</select>";
}
?>
This code fetch into many dropdown button not in one button. I want to fetch it into one dropdown button only. Anyone help me?
2
Answers
Try like this:
You’re putting a full
<select>
inside each iteration of your foreach loop. You need to move those outside of the loop, and only retain the<option>
inside it.WordPress also has some escaping functions like
esc_attr()
that you should be using when you’re outputting values into HTML attributes, URLs, etc.