Thursday, April 25, 2013

PHP, MySQL and HTML coding 101 or maybe 102

I am in one of my custom reports today, adding a bit more functionality, per my customers request and cleaned this up a bit regarding php and html and a little SQL.

PHP

I cleaned up the use of echo.  I was echoing every line, which is ok, but a little redundant.

Before
echo "bla bla bla";
echo "bla bla bla";
echo "bla bla bla bla bla"';
echo "bla bla bla";
echo "bla bla bla bla bla"';

After

echo
"bla bla bla".
"bla bla bla".
"bla bla bla bla bla"'.
"bla bla bla".
"bla bla bla bla bla"';

I removed echo from the start of each line and replaced the semicolon ; at the end of each line with a connecting . period.  This adds up in a long document.

 CSS

I added some css to the page to reduce the size of the text and to color the background of every other row.

Initially, I put the css code directly into the html like this

tr style="some css rule"

Then I remembered that is grade school.  Separate the formatting code from the presentation code (HTML).
So I added a style block at the top of the page like this:

 style type= "text/css"
.generaltable tr td{
    font-size:.9em;
}

.rows2{
    background-color: #EFEFEF;
    font-size:.9em;
}

.rows{
    font-size:.9em;
}

.smaller{
    font-size:.9em;
}

<
/style

That was great, until I needed to use the same style rule on another page.  Do NOT copy the css into another page.  If you find yourself copying and pasting, that is a red light to refactor a little.  Meaning, in this case, pull the style block out of the page and place in a separate page and reference it as an external file from both pages, like this.

LINK REL=StyleSheet HREF="style.css" TYPE="text/css" MEDIA=screen

where the style.css file is located in the same folder as the page referencing it.

Finally, a little MySQL

As I was tweaking the query used to return the main set of records to the php page, I formatted it and added a little formatting of the moodle unix time date fields.  Like this.
function getRecs(){
    global $DB;
   
    return
        $DB->get_records_sql('
            SELECT ue.id as ueid, c.id AS courseid, e.roleid, c.fullname, u.firstname, u.lastname, u.id as userid, u.email, u.city,
            from_unixtime(ue.timecreated, "%y/%m/%d") as date_enrolled,
            from_unixtime(ue.timestart, "%y/%m/%d") as date_started,
            date_format( date_add(from_unixtime(ue.timestart, "%y/%m/%d")   , interval 20 week) , "%y/%m/%d") as projected_completion_date,
            date_format( date_add(from_unixtime(ue.timestart, "%y/%m/%d")   , interval 24 week) , "%y/%m/%d") as projected_completion_date24,
            ue.status AS enrollmentstatus
           
            FROM
           
             mdl_user_enrolments ue
             join mdl_enrol e on e.id = ue.enrolid
             join mdl_user u on u.id = ue.userid
             join mdl_course c on c.id = e.courseid
           
             WHERE e.roleid = 5
        ');
}
 This query is heavy on enrollment information.  It is joining the 4 related tables and formatting the enrollment date fields. It is also creating two calculated date fields by adding 20 and 24 weeks to a state enrollment date.  Hence the slightly complex line of code

date_format( date_add(from_unixtime(ue.timestart, "%y/%m/%d")   , interval 20 week) , "%y/%m/%d") as projected_completion_date

No comments:

Post a Comment