Foreach Code / HTML Options

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Foreach Code / HTML Options

Post by donny »

Hello,

can somebody show me how to make a foreach code to generate options based on an array.

My array is like this

Code: Select all

$shirtsqty = array('1'=>"1 shirt",  
			'2'=>"2 shirts",  
			'3'=>"3 shirts",
			'4'=>"4 shirts",
			'5'=>"5 shirts");  
I would like it to generate this <option value="1">1 shirt</option> etc.

thanks a lot
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Foreach Code / HTML Options

Post by Celauran »

Code: Select all

<select name="whatever">
	<?php foreach ($shirtsqty as $key => $value): ?>
		<option value="<?= $key; ?>"><?= $value; ?></option>
	<?php endforeach; ?>
</select>
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: Foreach Code / HTML Options

Post by donny »

thank you. thank you very much.
Post Reply