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
https://forums.phpfreaks.com/topic/28907-i-need-some-help/#findComment-133638
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
https://forums.phpfreaks.com/topic/28907-i-need-some-help/#findComment-133665
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
https://forums.phpfreaks.com/topic/28907-i-need-some-help/#findComment-134287
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
https://forums.phpfreaks.com/topic/28907-i-need-some-help/#findComment-134295
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
https://forums.phpfreaks.com/topic/28907-i-need-some-help/#findComment-134323
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
https://forums.phpfreaks.com/topic/28907-i-need-some-help/#findComment-134435
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.