views:

77

answers:

1

Hi folks,

I've been working with a developer on a web-based application. I have some experience with html and css mainly, and now that the heavy lifting is done, I'm wanting to start improving the design elements of the program (I know that is NOT the ideal situation, and in a perfect world, all design elements would have been considered from the beginning-but those of you that work on projects know that it doesn't always go that way :-D )

I'm curious if there is a "standard" way that css and general layout structure is handled when working with extensive PHP conditionals. Here is an example of one area of our page:

$print_data .= '<p><b>My subscription:</b>&nbsp;&nbsp;';

if($period3=="1 M") {
  $print_data .= "$".$amount3." a month.<br />";
  $print_data .= "<b>Billing date:</b>&nbsp;&nbsp; Monthly on the " . date("jS", $subscr_effective_date_string) .".<br /><br /><b>Modification Options:</b><br />";
  $print_data .= '<input type="button" value="$75 every 6 months" onclick="#" /><br />
 <input type="button" value="$135 every 12 months" onclick="#" /><br /><br />';
}

elseif($period3=="6 M") {
  $print_data .= "$".$amount3." every 6 months.<br />";
  $print_data .= "<b>Billing date:</b>&nbsp;&nbsp; Every 6 months on " . date("m/d", $subscr_effective_date_string) ." and " . date("m/d",$subscr_effective_date_add_6mo_string) .".<br /><br /><b>Modification Options:</b><br />";
  $print_data .= '<input type="button" value="$14 a month" onclick="#" /><br /><input type="button" value="$135 every 12 months" onclick="#" /><br /><br />';
}

elseif($period3=="1 Y") {
  $print_data .= "$".$amount3." every year.<br />";
  $print_data .= "<b>Billing date:</b>&nbsp;&nbsp; Yearly on the " . date("m/d", $subscr_effective_date_string) .".<br /><br /><b>Modification Options:</b>";
  $print_data .= '<input type="button" value="$14 a month" onclick="#" /><br /><input type="button" value="$75 every 6 months" onclick="#" /><br /><br />';
}

$print_data .= '<input type="button" value="Unsubscribe" onclick="#" /></p>';

So in short, I'm wanting to contain the above in a <ul> and then have each conditional be a separate <li> group that will populate the <ul> depending on the condition.

Is this sort of thing just usually all handled in-line, or what?

Thanks! Joel

+4  A: 

Uf, that's ugly. That's pretty much the reason web development has been moving to a Model-View-Controller (MVC) approach over the last few years. Ideally your presentation markup (the html tags in your soup above) would be in one file and the logic that populates it in another file. One of the most common ways to do this in PHP is to use the Smarty templating engine.

In the interim, at the very least, the code above should be cleaned up: figure out the values for $amount3 and the text that goes on the billing input button and then just output the HTML in one place, rather than repeating it for each case (and it would be nice to use sprintf instead of jamming the strings together for cleanliness and a bit of added security).

Tom
We were under a big time crunch on this specific part of the code, which is why he ended up just pushing everything out as $print_data.If I'm understanding you correctly (and following my intuition) ideally, each of those lines in each conditional that are using $print_data would be their own variable, and then I'd have the html after this code and populating it with those variables, is that right?
Joel
Correct. Since what you have there is pretty much the same thing repeated each time, take each part where it changes, make that a variable with the right contents and put the variables into their correct spots at the end. It means that you only have you HTML code in one place (in case you need to change it later or add more things etc). For your next project, try to use some form of templating engine or a full MVC approach.
Blair McMillan
So MVC seems to be the answer to my question. Thanks guys...How stupid is it to try and go back through the project and start implementing this? Is it even possible? This is a live site, so keeping everything running is the priority.
Joel
Not stupid at all, assuming you have everything in version control.
Tom
Lol-if you mean me copying a page, renaming it, and trying out some edits...then yes I have version control. :-D
Joel
I kind of figured. Put the code into some sort of version control first, then you have a net above which you may practice all sorts of trapezery without fear.
Tom