Link to home
Start Free TrialLog in
Avatar of damianb123
damianb123Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Add static text to top of select box populated from database

Hi,
     I have a select dropdown box which gets populated from a field in my database, this is the code which populates it:

<?php
echo "<select name='DoctorID' size='1' style='font-family: Arial; font-size: 20pt; border:solid 1px #000000; border-width: 1px>";
   
   while ($list = mysql_fetch_assoc($result)) {
echo "<option value='test'>test</option>";	
	$doctors = array_filter( explode(",", $list['Doctors']), 'strlen' );
      
      foreach($doctors as $docs) {
         echo "<option value='$docs'>$docs</option>";
      } // end foreach Doctors

   } // end while 
echo "</select>";?>

Open in new window


This works fine, but I want to add a static line of text at the top, something along the lines of:

--- Click to select your doctor ---

How can I achieve this?

Thanks
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

A simplest thing: add the option before to enter the loop:
<?php
echo "<select name='DoctorID' size='1' style='font-family: Arial; font-size: 20pt; border:solid 1px #000000; border-width: 1px>";
   echo "<option>--- Click to select your doctor ---</option>";	
   while ($list = mysql_fetch_assoc($result)) {
echo "<option value='test'>test</option>";	
	$doctors = array_filter( explode(",", $list['Doctors']), 'strlen' );
      
      foreach($doctors as $docs) {
         echo "<option value='$docs'>$docs</option>";
      } // end foreach Doctors

   } // end while 
echo "</select>";?>
                                  

Open in new window

Avatar of damianb123

ASKER

Hi Marco,
      That's what i thought too, but for some reason that line just fails to show, only the looped contents appear, as if that new line wasn't present :-\

Damian
Mmmhh, that's strange... Can I see the full code?
Sure, he goes:

$sql = "SELECT * FROM gpw_maindata ORDER BY Doctors ASC";
$result = mysql_query($sql) or die(mysql_error()); 

<div align="center">
<form method="get" action="http://mydomain.co.uk/gpw-results.php">
<?php
echo "<select name='DoctorID' size='1' style='font-family: Arial; font-size: 20pt; border:solid 1px #000000; border-width: 1px>";
   echo "<option value='Please Select Doctor'>--- Click to select your doctor ---</option>";
   while ($list = mysql_fetch_assoc($result)) {

	$doctors = array_filter( explode(",", $list['Doctors']), 'strlen' );
       // echo "<option value='test'>test</option>";	      
      foreach($doctors as $docs) {
         echo "<option value='$docs'>$docs</option>";
      } // end foreach Doctors

   } // end while 
echo "</select>";
?>&nbsp;&nbsp;&nbsp;&nbsp;<input id="submit" name="submit" type="submit" value="Search" />

</div>

</form>

Open in new window

I don't see anything wrong. What happens if you comment out the loop?
$sql = "SELECT * FROM gpw_maindata ORDER BY Doctors ASC";
$result = mysql_query($sql) or die(mysql_error()); 

<div align="center">
<form method="get" action="http://mydomain.co.uk/gpw-results.php">
<?php
echo "<select name='DoctorID' size='1' style='font-family: Arial; font-size: 20pt; border:solid 1px #000000; border-width: 1px>";
   echo "<option value='Please Select Doctor'>--- Click to select your doctor ---</option>";
   while ($list = mysql_fetch_assoc($result)) {

//	$doctors = array_filter( explode(",", $list['Doctors']), 'strlen' );
       // echo "<option value='test'>test</option>";	      
      foreach($doctors as $docs) {
   //      echo "<option value='$docs'>$docs</option>";
      } // end foreach Doctors

   } // end while 
echo "</select>";
?>&nbsp;&nbsp;&nbsp;&nbsp;<input id="submit" name="submit" type="submit" value="Search" />

</div>

</form>
                                          

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks so much.... it does indeed work :-)  Amazing what a second pair of eyes looking can find.

Thanks my friend.
Anyway, let me say that:

 - you're still using MySql. It's franly ununderstandable the way many still ignore the official Php site reccomandations. MySql extension is deprecated and it will cease to work soon. Migrate now to mysqli or PDO: it's not so difficult as you could think: move, go on.

 - your problem shows perfectly one of the reasons to never use inline styles: you can forget a damned quote and become crazy before to understand why your code doesn't work :-) This should never happened if you had an external css file.

- use better the alternance between php and html.
<?php
$sql = "SELECT * FROM gpw_maindata ORDER BY Doctors ASC";
$result = mysql_query($sql) or die(mysql_error()); 
?>
<div align="center">
<form method="get" action="http://mydomain.co.uk/gpw-results.php">
<select name='DoctorID' size='1' style='font-family: Arial; font-size: 20pt; border:solid 1px #000000; border-width: 1px'>
  <option value='Please Select Doctor'>--- Click to select your doctor ---</option>
<?php
   while ($list = mysql_fetch_assoc($result)) {

		$doctors = array_filter( explode(",", $list['Doctors']), 'strlen' );
      foreach($doctors as $docs) {
         echo "<option value='$docs'>$docs</option>";
      } // end foreach Doctors
   } // end while 
?>
 </select>
&nbsp;&nbsp;&nbsp;&nbsp;<input id="submit" name="submit" type="submit" value="Search" />
</form>
</div>

Open in new window


Please not that this way I noticed another error: you were closing the div open after the form before to close the form.

- <div align="center"> Are you preparing HTML6? This attribute doesn't exist. As minimum, you should U(but you don't should do it) write <div style="text-align='center'">

Good luck for your project and forgive me for my tricks :-)