Jump to content

I need some help


bluwe

Recommended Posts

Thanks for replying. I've got a title column in my customer details table with the following options: Mr, Mrs, Miss, Ms, Dr.

When a user edits the customer details record it is obviously retireved from the db using a SELECT statement and displayed in a html table. I want to populate the first entry in the title drop down menu in the table with the value returned from the db, but still have the other options available. If you see what I mean!

Cheers!
Link to comment
Share on other sites

this will do it

[code]<select name=title>
<?php
$title = array("Mr", "Mrs", "Miss", "Ms", "Dr.");
foreach($title as $tname){
  if($tname == $dbvariable){
  echo "<option value=$title selected>$title</option>";
  } else {
  echo "<option value=$title>$title</option>";
  }
}
?>
</select>[/code]

Ray
Link to comment
Share on other sites

Thanks for the start Ray, needed a bit of debugging though, but finally got it working like so...

[code]<select name=title>
<?php
$dbvariable = "Miss";
$title = array ("Mr", "Mrs", "Miss", "Ms", "Dr");
foreach($title as $tname){
  if($tname == $dbvariable){
  echo "<option value=$dbvariable selected>$dbvariable</option>";
  } else {
  echo "<option value=$title>$tname</option>";
  }
}
echo "</select>";
?>[/code]
Link to comment
Share on other sites

This deffo works though...

[code]<select name=title>
<?php
$dbvariable = "Dr";
$title = array ("Mr", "Mrs", "Miss", "Ms", "Dr");
foreach($title as $tname){
  if($tname == $dbvariable){
  echo "<option value=$tname selected>$dbvariable</option>";
  }
}
foreach($title as $tname){
if($tname !=$dbvariable){
  echo "<option value=$title>$tname</option>";
}
}
echo "</select>";
?>[/code]
Link to comment
Share on other sites

Actually your were closer with this:
[code]<?php
<select name=title>
<?php
$dbvariable = "Miss";
$title = array ("Mr", "Mrs", "Miss", "Ms", "Dr");
foreach($title as $tname){
  if($tname == $dbvariable){
  echo "<option value=$dbvariable selected>$dbvariable</option>";
  } else {
  echo "<option value=$title>$tname</option>";
  }
}
echo "</select>";
?>[/code]

The problem is that you can't use the variable "$title" as a value, since all that will give you is the string "Array".

Try this instead:
[code]<?php
<select name=title>
<?php
$dbvariable = "Miss";
$title = array ("Mr", "Mrs", "Miss", "Ms", "Dr");
foreach($title as $tname){
  $sel = ($tname == $dbvariable)?' selected':''; // sets $sel to either selected or nothing
  echo '<option value="' . $tmame . '"' . $sel . 'selected>' . $tname. "</option><br>"; // make sure all values of attributes are quoted
  }
echo "</select>";
?>[/code]

Ken
Link to comment
Share on other sites

SHIT good catch ken. I buzzed though it so quick I put the wrong one in

[code]<select name=title>
<?php
$title = array("Mr", "Mrs", "Miss", "Ms", "Dr.");
foreach($title as $tname){
  if($tname == $dbvariable){
  echo "<option value=$tname selected>$tname</option>";
  } else {
  echo "<option value=$tname>$tname</option>";
  }
}
?>
</select>[/code]

Ray
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.