I'm struggling for a while now with the readability of my code, I after I tried to get as much insight as possible (for my standards). On my level, I think I understand and use it all right for my level.
But I'm still having big chunks of mixed html/css in the presentation. Often I have a mediocre complex multi-dimensional array as a return value and on the actual presentation page, I iterate through it, but still do a lot of stuff with it.
So I'm looking now into template engines like Smarty, but I can't get my head around it, how I would save some actual code with it in examples like the following where I iterate and work with the array in the presentation:
$courseinfo = new courseinfo($_SESSION['course_short']);
$row = $courseinfo->get_all();
$default = $courseinfo->get_default();
$prices = $courseinfo->get_prices();
$month_min_show = 5;
//color settings for prices
$colorlow = '#6F6';
$colormid = '#09F';
$colorhigh = '#F90';
$colorspecial = '#F0F';
$colorfull = 'rgba(255,0,0,0.3)';
/* CONTENT CALENDAR
-----------------
-----------------
*/
echo '<div id="calendar">';
$count['month'] = 0;
foreach($row as $month)
{
$lastcourse = end($month['course']);
$laststart = $lastcourse['date'];
$enddate = new DateTime($laststart);
$enddate->modify('+ '.($lastcourse['length']-1).' days');
$iterate = new DateTime('01-'.date('m',strtotime($laststart)).'-'.$month['year']);
if (!isset($stored['year']) || isset($stored['year']) && $stored['year'] != $month['year'])
{
if($count['month'] > $month_min_show) { break;} // don't show next year if $month_min_show months already displayed
if(isset($stored['year'])) { echo '<br /><br /><br /><div style="margin-top:-10px"></div>';}
echo '<span class="year" style="float:left;">'.$month['year'].'</span>';
echo '<div style="float:right;margin-top:-20px;padding-right:5px;">';
echo '<div style="float:left;font-size:12px;font-weight:bold;">PRICES '.$month['year'].'</div>';
echo '<div class="pricelegend" style="background-color:'.$colorfull.'">fully booked</div>';
if (in_array('low',$prices[$month['year']])) { echo '<div class="pricelegend" style="background-color:'.$colorlow.'">'.$default['price_low'].' €</div>'; }
if (in_array('mid',$prices[$month['year']])) { echo '<div class="pricelegend" style="background-color:'.$colormid.'">'.$default['price_mid'].' €</div>'; }
if (in_array('high',$prices[$month['year']])) { echo '<div class="pricelegend" style="background-color:'.$colorhigh.'">'.$default['price_high'].' €</div>'; }
if (in_array('custom',$prices[$month['year']])) { echo '<div class="pricelegend" style="background-color:'.$colorspecial.'">Special Offer</div>';}
echo '</div><div style="clear:both;"></div><hr width="800px;" align="left"/>';
}
echo '<div class="m_start">'.mb_strtoupper($month['monthname'],'UTF-8').'<br />';
echo '<span class="yearsmall">'.$month['year'].'</span>';
echo '</div>';
echo '<div class="courses">';
echo '<div style="float:left;width:10px;"> </div>';
while($iterate<=$enddate)
{
$dayname = strftime('%a',$iterate->format('U'));
if ($dayname == "So" OR $dayname == "Sa") { $daycolor = "#999"; } else { $daycolor = "#FFF";}
echo '<div class="dayname" id="'.$iterate->format('dmY').'" style="color:'.$daycolor.'">'.$dayname.'</div>';
$iterate->modify('+ 1 days');
}
echo '<br />';
$lineswitch = 0;
foreach($month['course'] as $course)
{
$date = $course['date'];
$date = new DateTime("$date");
$coursewidth = $course['length']*20-2;
if($course['class'] == 'low') { $pricecolor = $colorlow; }
elseif($course['class'] == 'mid') { $pricecolor = $colormid; }
elseif($course['class'] == 'high') { $pricecolor = $colorhigh; }
else {$pricecolor = $colorspecial;}
if($course['user'] >= $course['usermax']) { $pricecolor = $colorfull; }
if(isset($_SESSION['course_id']) && $_SESSION['course_id'] == $course['id']) { $bordercolor = 'border-color:#FFF';} else {$bordercolor = '';}
if($course['user'] < $course['usermax']) { echo '<a class="clink" id="'.$course['id'].'" href="'.$_SESSION['book_url'].'?course='.$course['id'].'" target="_self">'; }
echo '<div class="course" style="background-color:'.$pricecolor.';'.$bordercolor.';width:'.$coursewidth.'px;margin-top:'.$lineswitch*17 .'px;margin-left:'.(10+($date->format('d')-1)*20).'px">';
if($course['user'] < $course['usermax'])
{
echo '<span class="coursestart"> '.$date->format('d').'</span>';
if($course['length'] > 1)
{
echo '-';
$date->modify('+ '.($course['length']-1).' days');
echo '<span class="courseend">'.$date->format('d').' </span>';
}
}
else { echo '<span style="color:#000;">x</span>'; }
echo '</div>';
if($course['user'] < $course['usermax']) {echo '</a>';}
unset($date);
if ($lineswitch == 0) { $lineswitch = 1;} else {$lineswitch = 0;}
}
echo '</div>';
echo '<div class="m_end"></div>';
echo '<div style="clear:both;"></div><br />';
$stored['year'] = $month['year'];
$count['month']++;
}
echo '</div>';
Here's an example of the array I'm iterating through:
Array ( [04.2012] =>
Array ( [monthname] => April [year] => 2012 [course] =>
Array (
[0] => Array ( [id] => 106 [date] => 2012-04-02 14:00:00 [length] => 3 [class] => mid [price] => 110 [user] => 0 [usermax] => 20 [day] => 02 [week] => 14 [dayname] => Mo [hours] => 3 )
[1] => Array ( [id] => 107 [date] => 2012-04-03 10:00:00 [length] => 3 [class] => mid [price] => 110 [user] => 0 [usermax] => 20 [day] => 03 [week] => 14 [dayname] => Di [hours] => 3 )
[2] => Array ( [id] => 108 [date] => 2012-04-05 14:00:00 [length] => 3 [class] => mid [price] => 110 [user] => 0 [usermax] => 20 [day] => 05 [week] => 14 [dayname] => Do [hours] => 3 )
)
)
This is quite a bit of code as you can see, just so you can get an idea how I still have to work a lot with the array.
- So how could I split this into smaller chunks or just make it more readable and easier to work with ?
Hope I could make clear what I want here...and sure if you find anything else that's totally stupid in this code, give me a word!
I wouldn't know how a template engine would help as it still is a lot of ifs and dynamic changes in there.
Sidenote: I'm working alone and always will, so the separation is just for me.