PHP Code Snippets Episode 2 - Use An Array To fill a Option Box

In this post I am going to show you how to take the values from an array and use them, in a form option box.

Written By

Kenny Turner

Founder of southcoastweb, Passionate about PHP, MySQL, Laravel and Linux Servers.

Making a simple array

First of all we are going to make a simple array..

For this example i will just make a basic array, however you could use a mysql_fetch_array() to read data from a database to make your array then continue with step 2.

I will show you the basics of this in my next tutorial.


$options = ["option_1","option_2","option_3","option_4","option_5","option_6"];
<select name="example">
foreach($options as $choice) {
echo '<option value="'.$choice.'">'.$choice.'</option>';

As you can see this will give you all the options from the array into your option choices.

I hope this little snippet will help.

Next time

To view the next tutorial where i use a database to get information please click here.

More Tutorials

Here are some more tutorials that we think might be helpful for you. Feel free to contact us if there's anything you might need

Automating your service and repository patterns in Laravel with command generators

"Why spend 20 seconds doing something when you can spent 4 hours automating it," goes the proverb. See any professional proverbists around any more? No, because they've all been automated. Also it's fun. Whatever your programming pattern in Laravel, chances are that the php artisan make:x command is going to leave you high and dry on occasion. That's why it can be useful to have your own commands available to cover those gaps. We're going to go with the example of the "Service" pattern below.

Easier Laravel polymorphic relationships with MorphMap

Polymorphic relationships are a hugely powerful way to make connections between tables in a Laravel application. Say you have tables called companies, users, &amp; admins and all three needed to store addresses, without polymorphic relationships, we'd either have to forego standard two-way relationships in an addresses table and just use the ID, or we'd have to have three separate addresses tables, or even have all of our address fields on each of our three tables. All carry the weight of redundancy, repetition and unnecessary complexity.

Why you should be using backed enums in Laravel

Another year, another new "killer" feature for the poor, embattled software developer to learn rears its ugly head. Fortunately, enums only take a few minutes to learn and can be pretty useful for standardising database columns. Are they going to change the world? Probably not. Are they going to make a few problems slightly less problem-y? Quite possibly.

Passing through slots in Vue

If you're keeping your Vue component sizes small, there's a good chance you'll need to implement a wrapper component at some point. If you're only utilising the default slot for these, it can be as simple as putting a &lt;slot /&gt; tag inside your wrapper component. However, there's a bit more effort involved if you need to pass through named slots to your base component

How to Create a Simple Button Component in Figma

In this tutorial, we’ll create a simple button using Figma’s built-in component system.

Copyright 2007 - 2024 southcoastweb is a brand of DSM Design.