Ngu ngơ học làm web (91) - Sử dụng PHP trong Openemr

Tiếp theo của: Ngu ngơ học làm web (90) - Gửi email
------

Phần 91. Sử dụng PHP trong Openemr


Tới đây đã thấy tự tin hơn một chút, vì đã có thể đọc toàn bộ mã nguồn của dự án Openemr, đọc được mã nguồn thôi chứ chắc chắn chỉ hiểu được vài thứ trong đó, còn lại là mù tịt. Vì vậy vẫn còn rất nhiều thứ cần phải học.

Quay trở lại dự án Openemr, mở tập tin index.php của nó ra xem sao, đây là đoạn mã,

<?php
// Set the site ID if required.  This must be done before any database
// access is attempted.

if (!empty($_GET['site']))
    $site_id = $_GET['site'];
else if (is_dir("sites/" . $_SERVER['HTTP_HOST']))
    $site_id = $_SERVER['HTTP_HOST'];
else
    $site_id = 'default';

if (empty($site_id) || preg_match('/[^A-Za-z0-9\\-.]/', $site_id))
    die("Site ID '".htmlspecialchars($site_id,ENT_NOQUOTES)."' contains invalid characters.");

require_once "sites/$site_id/sqlconf.php";

if ($config == 1) {
    header("Location: interface/login/login_frame.php?site=$site_id");
} else {
    header("Location: setup.php?site=$site_id");
}

Đọc đoạn mã trên, hiểu được mấy thứ sau:

- Mục đích của đoạn mã trên là gán giá trị cho biến $site_id, (tuy nhiên chưa biết biến $site_id được dùng để làm gì) nếu giá trị $site_id không hợp lệ sẽ không cho chạy ứng dụng, ngược lại sẽ thiết lập các tham số cần thiết liên quan đến cơ sở dữ liệu và gọi trang login_frame.php hoặc trang setup.php.

- Đoạn mã trên có sử dụng tới biến $_GET, $_SERVER, HTTP_HOST, biểu thức chính quy, cấu hình liên quan đến cơ sở dữ liệu, thiết lập các tham số cho ứng dụng.


Phần này sẽ làm một việc đơn giản, là sử dụng PHP để viết lại đoạn mã hiển thị giao diện sau (phần khung màu xanh, xem lại phần 35 – jQuery trong Openemr để biết thêm chi tiết).



Nếu không sử dụng PHP, đoạn mã để hiển thị phần giao diện trên sẽ như sau:

<!DOCTYPE html>
<html lang="en">
<head>
            <meta charset="UTF-8"><title>Phan35</title>
            <style>
                        .bgcolor { background: yellow; }
                        .capitalize { text-transform: capitalize; }
            </style>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
            <script>
                        $(document).ready(function() {
                                    //show/hide div-1
                                    $('#chkWho').click(function() {
                                                $('#div_1').toggle(this.checked);
                                    });
                                    //change background for inputs
                                    $('#div_1 input, #div_1 select').bind('click',function() {
                                                $(this).toggleClass('bgcolor');
                                    });
                        });
            </script>
</head>
<body>
            <div>
                        <label><input type="checkbox" id="chkWho"> <strong>Who</strong></label>
            </div>
            <div id="div_1" style="display:none">
                        <table>
                                    <tbody>
                                                <tr>
                                                            <td><strong>Name</strong></td>
                                                            <td>
                                                                        <select id="form_title">
                                                                                    <option value>Unassigned</option>
                                                                                    <option value="Mr.">Mr.</option>
                                                                                    <option value="Mrs.">Mrs.</option>
                                                                                    <option value="Ms.">Ms.</option>
                                                                                    <option value="Dr.">Dr.</option>
                                                                        </select>
                                                                        <input class="capitalize" type="text">
                                                                        <input class="capitalize" type="text">
                                                                        <input class="capitalize" type="text">
                                                            </td>
                                                            <td><strong>External ID:</strong></td>
                                                            <td><input type="text"></td>
                                                </tr>
                                                <tr>
                                                            <td><strong>DOB:</strong></td>
                                                            <td><input type="date"></td>
                                                            <td><strong>Sex:</strong></td>
                                                            <td>
                                                                        <select name="" id="">
                                                                                    <option value>Unassigned</option>
                                                                                    <option value="">Female</option>
                                                                                    <option value="">Male</option>
                                                                        </select>
                                                            </td>
                                                </tr>
                                                <tr>
                                                            <td><strong>S.S.:</strong></td>
                                                            <td><input type="text"></td>
                                                            <td><strong>License/ID</strong></td>
                                                            <td><input type="text"></td>
                                                </tr>
                                                <tr>
                                                            <td><strong>Marital Status</strong></td>
                                                            <td>
                                                                        <select name="" id="">
                                                                                    <option value>Unassigned</option>
                                                                                    <option value="married">Married</option>
                                                                                    <option value="single">Single</option>
                                                                                    <option value="divorced">Divorced</option>
                                                                                    <option value="widowed">Widowed</option>
                                                                                    <option value="separated">Separated</option>
                                                                                    <option value="domestic partner">Domestic Partner</option>
                                                                        </select>
                                                            </td>
                                                </tr>
                                                <tr>
                                                            <td><strong>User Defined</strong></td>
                                                            <td><input type="text"><input type="text"><input type="text"></td>
                                                </tr>
                                                <tr>
                                                            <td><strong>Billing Note:</strong></td>
                                                            <td><input type="text"></td>
                                                </tr>
                                    </tbody>
                        </table>
            </div><!-- end div_1 -->
</body>
</html>

Giờ sẽ viết lại, có sử dụng PHP cho một số đoạn mã có tính lặp lại nhiều lần.

Lưu lại đoạn mã tại đây,

<!DOCTYPE html>
<html lang="en">
<head>
            <meta charset="UTF-8"><title>Phan35</title>
            <style>
                        .bgcolor { background: yellow; }
                        .capitalize { text-transform: capitalize; }
            </style>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
            <script>
                        $(document).ready(function() {
                                    //show/hide div-1
                                    $('#chkWho').click(function() {
                                                $('#div_1').toggle(this.checked);
                                    });
                                    //change background for inputs
                                    $('#div_1 input, #div_1 select').bind('click',function() {
                                                $(this).toggleClass('bgcolor');
                                    });
                        });
            </script>
</head>
<body>
<?php
            $arrTitle = array(
                                                                        'una' => 'Unassigned',
                                                                        'mr' => 'Mr.',
                                                                        'mrs' => 'Mrs.',
                                                                        'ms' => 'Ms.',
                                                                        'dr' => 'Dr.'
                                                            );
            $arrMaritalStatus = array(
                                                                                                'una' => 'Unassigned',
                                                                                                'mar' => 'Married',
                                                                                                'sin' => 'Single',
                                                                                                'div' => 'Divorced',
                                                                                                'wid' => 'Widowed',
                                                                                                'sep' => 'Separated',
                                                                                                'dom' => 'Domestic Partner'
                                                                                    );
            $arrGender = array(
                                                                        'una' => 'Unassigned',
                                                                        'fem' => 'Female',
                                                                        'mal' => 'Male'
                                                            );
            function createSelectBox ($name, $arrData) {
                        $result = '';
                        $result = '<select name="'.$name.'" id="'.$name.'">';
                        foreach ($arrData as $key => $value) {
                                    $result .= '<option value="'.$key.'">'.$value.'</option>';
                        }
                        $result .='</select>';
                        echo $result;
            }
?>
            <div>
                        <label><input type="checkbox" id="chkWho"> <strong>Who</strong></label>
            </div>
            <div id="div_1" style="display:none">
                        <table>
                                    <tbody>
                                                <tr>
                                                            <td><strong>Name</strong></td>
                                                            <td>
                                                                        <?php createSelectBox('title', $arrTitle); ?>
                                                                        <input class="capitalize" type="text">
                                                                        <input class="capitalize" type="text">
                                                                        <input class="capitalize" type="text">
                                                            </td>
                                                            <td><strong>External ID:</strong></td>
                                                            <td><input type="text"></td>
                                                </tr>
                                                <tr>
                                                            <td><strong>DOB:</strong></td>
                                                            <td><input type="date"></td>
                                                            <td><strong>Sex:</strong></td>
                                                            <td>
                                                                        <?php createSelectBox('gender', $arrGender); ?>
                                                            </td>
                                                </tr>
                                                <tr>
                                                            <td><strong>S.S.:</strong></td>
                                                            <td><input type="text"></td>
                                                            <td><strong>License/ID</strong></td>
                                                            <td><input type="text"></td>
                                                </tr>
                                                <tr>
                                                            <td><strong>Marital Status</strong></td>
                                                            <td>
                                                                        <?php createSelectBox('marital', $arrMaritalStatus); ?>
                                                            </td>
                                                </tr>
                                                <tr>
                                                            <td><strong>User Defined</strong></td>
                                                            <td><input type="text"><input type="text"><input type="text"></td>
                                                </tr>
                                                <tr>
                                                            <td><strong>Billing Note:</strong></td>
                                                            <td><input type="text"></td>
                                                </tr>
                                    </tbody>
                        </table>
            </div><!-- end div_1 -->
</body>
</html>


Tới đây có một thắc mắc: có nhiều đoạn mã có thể viết bằng PHP hoặc không cần dùng tới PHP cũng được, nếu viết bằng PHP thì mã nguồn sẽ gọn gàng hơn, vậy chọn cách nào? Vì chưa biết tốc độ đáp ứng người dùng của trang web bị ảnh hưởng như thế nào khi có dùng PHP và khi không dùng PHP cho những đoạn mã như vậy?
-----------
Cập nhật 9/1/2018
-----------
Xem thêm:
Tổng hợp các bài viết về Ngu ngơ học làm web