1

i write a program to retrieve the data through MySQL using function now i have a problem that how i use it in the select box?

Code for this is given below,

function selectdata(){
$select="SELECT * FROM table_name";

$run=mysql_query($select);

while($fetch=mysql_fetch_array($run)){
 $field_name.=$fetch['field_name'];


}
return $field_name;
}

HTML:

<select name="office_name">

<option value=""><?php echo selectdata(); ?></option>

</select>

it is showing all the data in one field

any help??

2
  • What are you expecting to happen? Did you want a new <option> element for each row from the table? Commented Jun 5, 2013 at 10:29
  • Don't use mysql_ extensions it's deprecated, use PDO or msqli_ instead. Commented Jun 5, 2013 at 10:41

5 Answers 5

2

Do like this

function selectdata(){
   $select="SELECT * FROM table_name";

   $run=mysql_query($select);
   $field_name = '';
   while($fetch=mysql_fetch_array($run)){
       $field_name.="<option value=''>" . $fetch['field_name'] . "</option>";
   }
   return $field_name;
}

And in html

<select name="office_name">
<?php echo selectdata(); ?>
</select>
Sign up to request clarification or add additional context in comments.

Comments

2

That's because you're echoing your string into one option value. A better way would be to return an array from selectdata() and then loop through it in your html like:

function selectdata(){
    $select="SELECT * FROM table_name";

    $run=mysql_query($select);
    $fields = array()
    while($fetch=mysql_fetch_array($run)){
        $fields[] = $fetch['field_name'];
    }
    return $fields;
}

And then

<select name="office_name">
    <?php foreach(selectdata() as $option): ?>
    <option value="<?php echo $option; ?>"><?php echo $option; ?></option>
    <?php endforeach; ?>
</select>

This way, you're keeping HTML out of your PHP.

2 Comments

Why would you want to do a loop twice? If you are already going trough all the rows, why not use it.
It allows you to reuse the selectdata function elsewhere as it wouldn't contain the HTML. Example: Once the user submits their form data you want to validate that it's one of the allowed options. You could then do in_array($_POST['office_name'], selectdata()). It's all about reusability and keeping HTML away from your PHP.
2

Try this one..Not sure the syntax is correct.

function selectdata(){
$select="SELECT * FROM table_name";

$run=mysql_query($select);

while($fetch=mysql_fetch_array($run)){
 $field_name.= "<option value=>" .$fetch['field_name']."</option>";
}
return $field_name;
}

HTML:

<select name="office_name">
<?php echo selectdata(); ?>
</select>

1 Comment

you are missing the parenthesis for the value-Attribute as well as its content.
0
<?php
$query = mysql_query("YOUR QUERY HERE"); // Run your query

echo '<select name="DROP DOWN NAME">'; // Open your drop down box

// Loop through the query results, outputing the options one by one
while ($row = mysql_fetch_array($query)) {
   echo '<option value="'.$row['something'].'">'.$row['something'].'</option>';
}

echo '</select>';// Close your drop down box





?>

Comments

0

You can use as following sample,

<?php
$sql = "SELECT * FROM terms";
$result = mysql_query($sql) or die(mysql_error());

while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
   $term[$row{'term_id'}] = $row{'term_name'};
}
?>
    <select id="mass_term"  name="mass_term" style="width:125px;" >
   <?php foreach($term as $key=>$value){ ?>
      <option <?php echo ($key==$termid)?'selected="selected"':'' ?> value="<?php echo $key; ?>"><?php echo $value; ?></option>
   <?php } ?>
    </select>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.