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

From: Chris O.
Sent on: Wednesday, October 13, 2010 10:20 PM
This is how I would write it:��http://pastebin.com/7ifj2Vh5

Major changes:
> separate the switch/case logic from the input element display logic; put input elements in functions or another structure

-- Chris

On Wed, Oct 13, 2010 at 7:13 PM, Jonny Nguyen <[address removed]> wrote:
The example I gave you was to simple to address the complexity. Here is my real concern.

<?php switch($item['type']):
������ case 1: /* ComboBox */ ?>
������ <select name='dyn<?php echo $item['name']; ?>'>
������ <option value=''>{select a value}</option>
������ <?php foreach($item['source'] as $source): ?>
������ <option value='<?php echo $source['text']; ?>'<?php if(isset($item['value']) && ($source['text'] == $item['value'])) { echo ' selected'; }?>><?php echo $source['text']; ?></option>
������ <?php endforeach; ?>
������ </select><br/>������
������ <?php break; ?>
������
������ <?php case 2: /* Listbox */ ?>
������ <select name='dyn<?php echo $item['name']; ?>[]' size='5' Multiple>������
������ <?php foreach($item['source'] as $source): ?>
������ <option value='<?php echo $source['text']; ?>'<?php if(isset($item['value']) && (in_array($source['text'], $item['value']))) { echo ' selected'; }?>><?php echo $source['text']; ?></option>������
������ <?php endforeach; ?>������
������ </select><br/>
������ <?php break; ?>
������
������ <?php case 3: /* Text Field */ ?>
������ <input type='text' name='dyn<?php echo $item['name']; ?>' value='<?php if(isset($item['value'])) { echo $item['value']; }?>'/><br/>
������ <?php break; ?>
������
������ <?php case 4: /* Text Area */ ?>
������ <textarea rows='5' name='dyn<?php echo $item['name']; ?>' cols='20'><?php if(isset($item['value'])) { echo $item['value']; }?></textarea><br/>
������ <?php break; ?>

������ <?php case 5: /* Radio Button */ ?>
������ <?php foreach($item['source'] as $source): ?>
������ <input type='radio' name='dyn<?php echo $item['name']; ?>' value='<?php echo $source['text']; ?>'<?php if(isset($item['value']) && ($source['text'] == $item['value'])) { echo ' checked';�� }?>><?php echo $source['text']; ?><br/>
������ <?php endforeach; ?>
������ <?php break; ?>
������
������ <?php case 6: /* Check Box */ ?>
������ <?php foreach($item['source'] as $source): ?>
������ <input type='checkbox' name='dyn<?php echo $item['name']; ?>[]' value='<?php echo $source['text']; ?>'<?php if(isset($item['value']) && (in_array($source['text'], $item['value']))) { echo ' checked';�� }?>><?php echo $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><?php echo $item['title']; ?></h4>
������ <select name='category' onchange="location = options[selectedIndex].value;">
������ <option value='<?php echo $real_url . $routes['control']; ?>'>{select a value}</option>
������ <?php foreach($item['source'] as $source): ?>
������ <option value='<?php echo $real_url . $routes['control'] . DS . $source['option'] ?>'<?php if(isset($item['value']) && $source['option'] == $item['value']) { echo ' Selected'; } ?>><?php echo $source['text']; ?></option>
������ <?php endforeach; ?>
������ </select>
������ <?php break; ?>
������
������ <?php default: ?>
������ <?php break; ?>
<?php endswitch; ?>

Please provide input on how to make this code more readable or better. My ears are yours



On Wed, Oct 13, 2010 at 3:06 PM, Sean Dundon <[address removed]> wrote:
Oh, absolutely mix the two of them. ��Even better, declare all of your
variables and do your calculations at the top:

<?php
��$heading = 'Hello World';
?>
<html>
<head />
<body>
<h3><?=$heading?></h3>
</body>
</html>

Some people don't like the <?= tag, as it doesn't play nice with xml,
but in my experience it works great. ��Now you have your logic
separated from your output, which is a much more peaceful way to live.

On Tue, Oct 12, 2010 at 5:50 PM, Jonny Nguyen <[address removed]> wrote:
> I need your input regarding mix html with php or just create a php string.
> See example below.
>
> 1. Html mix with php
> <html>
> <head></head>
> <body>
> <h3><?php echo 'Hello World'; ?></h3>
> </body>
> </html>
>
> 2. php string
> <?php
> strhtml = '<html>';
> strhtml .= '<head></head>';
> strhtml .= '<body>';
> strhtml .= '<h3>Hello World</h3>';
> strhtml .= '</body>';
> strhtml .= '</html>';
> echo strhtml;
> ?>
>
> thanks
>
> Jonny


--
Sean T. Dundon
[address removed]




--
Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
http://www.meetup.com/houston-php-users-group/
This message was sent by Sean Dundon ([address removed]) from Houston PHP Users Group.
To learn more about Sean Dundon, visit his/her member profile: http://www.meetup.com/houston-php-users-group/members/4591778/
To unsubscribe or to update your mailing list settings, click here: http://www.meetup.com/houston-php-users-group/settings/
Meetup, PO Box 4668 #37895 New York, New York[masked] | [address removed]






--
Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
This message was sent by Jonny Nguyen ([address removed]) from Houston PHP Users Group.
To learn more about Jonny Nguyen, visit his/her member profile

To unsubscribe or to update your mailing list settings, click here

Meetup, PO Box 4668 #37895 New York, New York[masked] | [address removed]

Our Sponsors

People in this
Meetup are also in:

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