Re: [houston-php] Mix html with php vs php string.

From: user 4.
Sent on: Wednesday, October 13, 2010 8:53 PM
Well, I really like Brandon's example, especially the coloring, which
is something I'll have to figure out.  Personally, for little widgets
like that I like to keep things pretty simple, and I really don't like
constructing html tags with PHP.  It's still not the most readable
thing in the world, but it explains itself well.  Here's how I would
do it:

<!--
  1. Replace all instances of "?php echo " with "?="
  2. Replace all instances of "; ?>" with " ?>"
  3. Move some things to their own line
  4. Give all php code it's own tags
  5. Modify indentation
  6. Fix the code so that $item['value'] is always set
-->

<?php $item['value'] = isset($item['value']­) ? $item['value'] : '' ?>

<?php switch($item['type']­): ?>
  <? case 1: /* ComboBox */ ?>
    <select name='dyn<?=$item­['name']?>'>
      <option value=''>{select a value}</option>­
      <?php foreach($item['sourc­e'] as $source): ?>
        <?php $selected = $item['value']) && ($source['text'] ==
$item['value']) ? ' selected' : '' ?>
        <option value='<?=$source­['text']?>' <?=$selected?>­
><?=$source['t­ext']?></optio­n>
      <?php endforeach ?>
    </select>
    <br/>
    <?php break ?>

  <?php case 2: /* Listbox */ ?>
    <select name='dyn<<?=$­item['name']?>[]'­ size='5' Multiple>
      <?php foreach($item['sourc­e'] as $source): ?>
        <?php $selected = isset($item['value']­) &&
(in_array($source['t­ext'], $item['value'])) ? ' selected' : '' ?>
        <option value='<?=$source­['text'] ?>' <?=$selected?>­
><?=$source['t­ext']?></optio­n>
      <?php endforeach ?>
    </select>
    <br/>
    <?php break ?>

  <?php case 3: /* Text Field */ ?>
    <input type='text' name='dyn<?=$item­['name'] ?>' value='<?=$value?­>'/>
    <br/>
    <?php break ?>

  <?php case 4: /* Text Area */ ?>
    <textarea rows='5' name='dyn<?=$item­['name'] ?>'
cols='20'><?=$­item['value']?><­/textarea>
    <br/>
    <?php break ?>

  <?php case 5: /* Radio Button */ ?>
    <?php foreach($item['sourc­e'] as $source): ?>
      <?php $checked = isset($item['value']­) && ($source['text'] ==
$item['value']) ? ' checked' : '' ?>
      <input type='radio' name='dyn<?=$item­['name'] ?>'
value='<?=$source­['text'] ?>' <?=$checked?> /><?=$source['­text'] ?>
      <br/>
    <?php endforeach ?>
    <?php break ?>

  <?php case 6: /* Check Box */ ?>
    <?php foreach($item['sourc­e'] as $source): ?>
      <?php $checked = isset($item['value']­) &&
(in_array($source['t­ext'], $item['value'])) ? ' checked' : '' ?>
      <input type='checkbox' name='dyn<?=$item­['name'] ?>[]'
value='<?=$source­['text'] ?>' <?=$checked?> /><?=$source['­text'] ?>
      <br/>
    <?php endforeach ?>
    <?php break ?>

  <?php case 8: /* Image */ ?>
    <img src="angry.gif" alt="Angry face" />
    <br/>
    <?php break ?>

  <?php case 9: /* Category ComboBox */ ?>
    <h4><?=$ite­m['title'] ?></h4>
    <select name='category' >
      <option value='<?=$real_u­rl . $routes['control'] ?>'>{select a
value}</option>­
      <?php foreach($item['sourc­e'] as $source): ?>
        <?php $selected = isset($item['value']­) && $source['option']
== $item['value'] ? ' Selected' : '' ?>
        <option value='<?=$real_u­rl . $routes['control'] . DS .
$source['option'] ?>' <?=$selected?>­ ><?=$source['t­ext'] ?></option>­
      <?php endforeach ?>
    </select>
    <?php break ?>

  <?php default: ?>
    <?php break ?>

<?php endswitch ?>

-- 
Sean T. Dundon
[address removed]

Our Sponsors

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy