gaban gaban - 1 year ago 79
PHP Question

Using php array elements to create dropbox

I am trying to create combo box with shows php array elements inside html code. The combo box is the part of the html table inside my application. So the problem is I could not put my php foreach loop and options inside my select tags. I tried doing it like this but it did not work. I have issues with single and double quotes again ı suppose.

echo "<td>"."<input type=text name =DoctorID value =".$row[DoctorID]."/></td>";
echo "<td>"."<input type =text name = name value =".$row[name]." /></td>";
echo "<td>"."<input type =text name = surname value =".$row[surname]."/></td>";
echo "<td>
<select name='specialityID'>
foreach($a as $b)
<option value='".$b."'>".$b."</option>;
</select></td>" ;

Answer Source

Another way: Use PHP as a 'templating' language...

No struggling with different types or quotes or concatenation.

Note: The short echo tags (<?=) are perfectly safe and valid.

PHP offers an alternative syntax for some of its control structures; namely, if, while, for, foreach, and switch. In each case, the basic form of the alternate syntax is to change the opening brace to a colon (:) and the closing brace to endif;, endwhile;, endfor;, endforeach;, or endswitch;, respectively.

/* end php mode */ 
    <select name='specialityID'>
    <?php foreach($a as $b): ?>
        <option value="<?= $b ?>"><?= $b ?></option>;
    <?php endforeach ?>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download