PHP printf to display drop down list

Hi all.

I have the code below that populates a drop down list with the values from the Lot field in my table. I want to insert a blank as the first entry and then the values for the Lot field. Any idea how I can do this?

Example:

<blank>
Lot1
Lot2
Lot3

Currently it displays like this:
Lot1
Lot2
Lot3

Again, I want to be able to add a <blank> entry first, or even a message like "Choose a lot".

Thank you in advance!

foreach ($data1 as $row){
  printf("<option>%s</option>", html_escape($row['Lot']));
 
}

Open in new window

Sim1980Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GaryCommented:
Why not...

echo "<option></option";
foreach ($data1 as $row){
  printf("<option>%s</option>", html_escape($row['Lot']));
} 

Open in new window

0
Sim1980Author Commented:
That didn't work.
0
Sim1980Author Commented:
I tried

printf("<option selected></option><option>%s</option>", html_escape($row['Lot']));

Open in new window


But that puts a blank before EVERY $row['Lot']. I just want it once in the first drowndown box row.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

GaryCommented:
Not in the loop but before it, exactly as I have above.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sim1980Author Commented:
That was it! Thanks.
printf("<option selected></option>");
foreach ($data1 as $row){
 printf("<option>%s</option>", html_escape($row['Lot']));
}

Open in new window

0
Chris StanyonWebDevCommented:
No points, just pointing out a slight typo in Gary's code - missing closing >

echo "<option></option>";
foreach ($data1 as $row){
  printf("<option>%s</option>", html_escape($row['Lot']));
}

Open in new window

There's no reason why it wouldn't work, so if it doesn't, show use the HTML output you do get (and don't forget to wrap the whole thing in a SELECT)
0
GaryCommented:
:o/
p.s.
You don't need selected - the first option will always be selected unless you specify another option with selected.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.