PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 27th, 2013, 11:08 PM
zxcvbnm's Avatar
zxcvbnm zxcvbnm is offline
A Change of Season
Click here for more information.
 
Join Date: Mar 2004
Posts: 1,600 zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Weeks 5 Days 7 m 45 sec
Reputation Power: 70
PHP-OOP - Shrinking the view

Hello friends;

Lates say I want to write some code that creates a drop down that shows months of the year.

I can easily write loose php in view which sucks:
Code:
<select name="post_month">
    <option selected="selected"></option>
    <?php
        $m = 0;
        while($m<12)
            {
                ++$m;
                ?><option value="<?php echo $m;?>"                 <?php if($post_m ?> selected="selected" <?php } ?>
                ><?php echo date("F", mktime(0, 0, 0, $m));?></option><?php 
            }
    ?>
</select>
Now in order to keep my views clean for genious desginers, would this be the right thing to do?

Controller:
PHP Code:
 $this->load->library('months_drop_down','','drop_down');
$data['months'] = $this->drop_down->index('month'); 
And in libraries folder I ll have:
PHP Code:
class Months_drop_down
 
{
      protected 
$posted_month "";
      public function 
index($name=NULL
          {
            
$CI =& get_instance();
            
$CI->posted_month $CI->input->post($name) ? $CI->input->post($name) : "";
          
$m 0;
          
$CI->dropdown="<select name=\\"".$name."\">";
            while(
$m<12)
                            {
            
$m++;
            
$CI->dropdown.="<option value=\"".$m."\"";     if($CI->posted_m
             
{
              
$CI->dropdown.="selected=\"selected\"";
             }
            
$CI->dropdown.= ">\n".date("F"mktime(000$m))."</option>";
                            }
            
$CI->dropdown.="</select>"
            return 
$CI->dropdown;
         }
 } 
And in view
PHP Code:
<?php echo $months;?>
Please advice. Thanks

Reply With Quote
  #2  
Old January 28th, 2013, 08:06 PM
E-Oreo's Avatar
E-Oreo E-Oreo is offline
Lost in code
Click here for more information.
 
Join Date: Dec 2004
Posts: 7,931 E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
Time spent in forums: 2 Months 7 h 48 m 54 sec
Reputation Power: 7053
Don't cater specifically to designers. Either they're smart enough to understand PHP, or they're smart enough to understand what to touch and what not to touch, or they're not smart enough to be mucking around in your application's views.

Your views will be a bit clearer if you use the alternative PHP block syntax:
PHP Code:
 ?>
<select name="post_month">
    <option selected="selected"></option>
    <?php for($m 1$m <= 12$m++): ?>
        <option value="<?php echo $m;?>"
            <?php if(??): ?> selected="selected" <?php endif; ?>>
            <?php echo date("F"mktime(000$m));?>
        </option>
    <?php endfor; ?>
</select> 


Alternatively, create a new view with that snippet of code and include it in your main view using load->view. Then tell your designers not to touch the partial view snippets.
__________________
PHP FAQ
How to program a basic, secure login system using PHP

Quote:
Originally Posted by Spad
Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

Reply With Quote
  #3  
Old January 29th, 2013, 12:27 AM
Catacaustic's Avatar
Catacaustic Catacaustic is offline
Code Monkey V. 0.9
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2005
Location: A Land Down Under
Posts: 1,885 Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level)Catacaustic User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 14 h 31 m 3 sec
Reputation Power: 1798
Another thing to remember when you're doing things like this:

Don't think about "clean" (as far as templating goes), think about "re-use". Your example is actually a very good one because there's a high chance that you'll want to have a months select elements in more then one place, so that's when you should start looking at it to extract that out into it's own helper or function, so I think that you've got the right idea. Depending on how you've got the rest of your system set out you can incorporate E-Oreo's suggestion of using that with a separate snippet for the HTML layout pretty easily.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > PHP-OOP - Shrinking the view

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap