airider74 airider74 - 3 years ago 145
HTML Question

HTML option list information not displaying within PHP echo

I've got a set of code I'm trying to move within the

php
tag to leverage 'php' logic features. I've got most of it working but I'm struggling a bit with an option list I have. I've looked around the forums but can't seem to find anything close to this.

Here is what my original option list looks like:

<div class="plan">
<label for="g2c3m">Month</label>
<select id="g2c3m" name="g2c3m">
<option value=""></option>
<option <?php echo ($row["g2c3m"] === "1")?"selected" : ""; ?> >1</option>
<option <?php echo ($row["g2c3m"] === "2")?"selected" : ""; ?> >2</option>
<option <?php echo ($row["g2c3m"] === "3")?"selected" : ""; ?> >3</option>
<option <?php echo ($row["g2c3m"] === "4")?"selected" : ""; ?> >4</option>
<option <?php echo ($row["g2c3m"] === "5")?"selected" : ""; ?> >5</option>
<option <?php echo ($row["g2c3m"] === "6")?"selected" : ""; ?> >6</option>
<option <?php echo ($row["g2c3m"] === "7")?"selected" : ""; ?> >7</option>
<option <?php echo ($row["g2c3m"] === "8")?"selected" : ""; ?> >8</option>
<option <?php echo ($row["g2c3m"] === "9")?"selected" : ""; ?> >9</option>
<option <?php echo ($row["g2c3m"] === "10")?"selected" : ""; ?> >10</option>
<option <?php echo ($row["g2c3m"] === "11")?"selected" : ""; ?> >11</option>
<option <?php echo ($row["g2c3m"] === "12")?"selected" : ""; ?> >12</option>
</select>
</div>


I'm trying to transition this
html
based code into
php
based code. Here's what I've come up with:

echo "<div class=\"plan\">";
echo "<label for=\"g2c3m\">Month</label>";
echo "<select id=\"g2c3m\" name=\"g2c3m\">";
echo "<option value=\"\"></option>";
echo "<option ".$row['g2c3m']." === \"1\")?\"selected\" : \"\"; >1</option>";
echo "<option ".$row['g2c3m']." === \"2\")?\"selected\" : \"\"; >2</option>";
echo "<option ".$row['g2c3m']." === \"3\")?\"selected\" : \"\"; >3</option>";
echo "<option ".$row['g2c3m']." === \"4\")?\"selected\" : \"\"; >4</option>";
echo "<option ".$row['g2c3m']." === \"5\")?\"selected\" : \"\"; >5</option>";
echo "<option ".$row['g2c3m']." === \"6\")?\"selected\" : \"\"; >6</option>";
echo "<option ".$row['g2c3m']." === \"7\")?\"selected\" : \"\"; >7</option>";
echo "<option ".$row['g2c3m']." === \"8\")?\"selected\" : \"\"; >8</option>";
echo "<option ".$row['g2c3m']." === \"9\")?\"selected\" : \"\"; >9</option>";
echo "<option ".$row['g2c3m']." === \"10\")?\"selected\" : \"\"; >10</option>";
echo "<option ".$row['g2c3m']." === \"11\")?\"selected\" : \"\"; >11</option>";
echo "<option ".$row['g2c3m']." === \"12\")?\"selected\" : \"\"; >12</option>";
echo "</select>";
echo "</div>";


The option list displays properly, but when I submit the information to the server, it is not echo'd back properly.

I believe I'm following the proper
\
rules regarding the quotes, but it's not working.

What am I missing? I'd be eternally grateful for any assistance.

Answer Source

This isn't an answer to your question but it will help you regardless.

It is perfectly acceptable to open and close PHP code lots of times when writing template code. PHP can be closed and opened as much as is needed (as long as it's not in the middle of an expression). So you can run an if statement, close php, print out HTML, then reopen PHP like so:

<?php if (condition): ?>
    <p>Hello World</p>
<?php else: ?>
    <p>Goodbye World</p>
<?php endif; ?>

or for loops:

<?php foreach($results as $row): ?>
    <tr>
        <td><?php echo $row['name'] ?></td>
    </tr>
<?php endforeach; ?>

This uses the alternative syntax (if/endif, foreach/endforeach) which is great for templating.

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