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
damianb123Asked:
Who is Participating?
 
Marco GasiFreelancerCommented:
Found! You miss a single quote to close the (deprecated) inline style. Put a single quote after

border-width: 1px

And it will work
0
 
Marco GasiFreelancerCommented:
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

0
 
damianb123Author Commented:
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
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
Marco GasiFreelancerCommented:
Mmmhh, that's strange... Can I see the full code?
0
 
damianb123Author Commented:
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

0
 
Marco GasiFreelancerCommented:
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

0
 
damianb123Author Commented:
Thanks so much.... it does indeed work :-)  Amazing what a second pair of eyes looking can find.

Thanks my friend.
0
 
Marco GasiFreelancerCommented:
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 :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.