TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-07-2007, 04:48 PM   #1 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default Clean code police, need feedback.

PHP Code:
<?php

    
/* 
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
    
    Title : Courses
    Author : Muhammad Haris
    URL : http://www.mharis.net
    CONTACT: isharis@gmail.com
    
    Description : Show, add, edit, delete courses
    All courses for Administrators
    Related courses for School users
    
    Created : 26th September 2007
    Modified : 6th September 2007
    
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
    */
    
    
include_once('../includes/includes.php');
    
    
//
    // Initiates new objects
    //
    
    
$tpl =& new Savant2();
    
$validate = new FormValidator;
    
$database = new Database;
    
$auth = new Auth;
    
$users = new Users;
    
    
    
//
    // If user is not logged in show failure error
    //
    
    
if($auth->check() == false){
        echo 
'Please login';
        
    }
    
    
//
    // If user is an administrator
    //
    
    
elseif($auth->admin_auth() == True){
        
        include_once(
'navigation.php'); // Includes Navigation
        
        //
        // Assigning post values to variables
        //
        
        
$szClassName $_POST['className'];
        
$szCourseType $_POST['courseType'];
        
        
$iStartDateYear $_POST['startDateYear'];
        
$iStartDateMonth $_POST['startDateMonth'];
        
$iStartDateDay $_POST['startDateDay'];
        
$szStartDate $iStartDateYear.'-'.$iStartDateMonth.'-'.$iStartDateDay;
        
        
$iEndDateYear $_POST['endDateYear'];
        
$iEndDateMonth $_POST['endDateMonth'];
        
$iEndDateDay $_POST['endDateDay'];
        
$szEndDate $iEndDateYear.'-'.$iEndDateMonth.'-'.$iEndDateDay;
        
        
$iStartTimeHour $_POST['startTimeHour'];
        
$iStartTimeMinutes $_POST['startTimeMinutes'];
        
$szStartTime $iStartTimeHour.':'.$iStartTimeMinutes;
        
        
$iEndTimeHour $_POST['endTimeHour'];
        
$iEndTimeMinutes $_POST['endTimeMinutes'];
        
$szEndTime $iEndTimeHour.':'.$iEndTimeMinutes;
        
        
$iPrice $_POST['price'];
        
$szInstructor $_POST['instructor'];
        
$szEquipment $_POST['instructor'];
        
$szPrerequisities $_POST['prerequisites'];
        
$szDescription $_POST['description'];
        
$szSchool $_POST['school'];
        
        
        
//
        // Validation rules
        //
        
        
if(isset($_POST['add_course'])){
            
$validate->isEmpty('szClassName''Please enter a class name');
            
$validate->isEmpty('szCourseType''Select course type');
            
$validate->isDate('szStartDate''Select all starting time paramters');
            
$validate->isDate('szEndDate''Select all ending time paramters');
            
$validate->isTime('szStartTime''Select all starting time paramters');
            
$validate->isEmpty('szStartTime''Select all ending time paramters');
            
$validate->isEmpty('iEndTimeMinutes''Select end time minutes');
            
$validate->isEmpty('iPrice''Enter a price');
            
$validate->isEmpty('szInstructor''Enter instructor name');
            
$validate->isEmpty('szEquipment''Please enter equipment details');
            
$validate->isEmpty('szPrerequisities''Please enter prerequisities details');
            
$validate->isEmpty('szDescription''Please enter a description');
            
$validate->isEmpty('szSchool''Assign course to a user');
        }
        
        
//
        // Select all users with rank 2
        //
        
        
$szSQL "SELECT name FROM schools";
        
$szResult $database->execute($szSQL);
        while(
$szRow mysql_fetch_array($szResultMYSQL_ASSOC)){
            
$aSchool[] = $szRow['name'];
        }
        
        
$tpl->assign('aSchool'$aSchool);
        
        
//
        // Get all course types
        //
        
        
$szSQL "SELECT courseType FROM coursetypes";
        
$szResult $database->execute($szSQL);
        while(
$szRow mysql_fetch_array($szResultMYSQL_ASSOC)){
            
$aCourseType[] = $szRow['courseType'];
        }
        
$tpl->assign('aCourseType'$aCourseType);
        
        
//
        // Assigns errors to add course form
        //
        
        
if($validate->isError() && isset($_POST['add_course'])){
            
$tpl->assign('aError'$validate->getErrorList());
        }
        
        
//
        // If no errors found, add the new course
        //
        
        
if(isset($_POST['add_course']) && $validate->isError() == false){
            
            
//
            // Gets selected school ID
            //
            
            
$szSQL "SELECT id FROM schools WHERE name = '$szSchool' LIMIT 0,1";
            
$iResult $database->fetch($szSQL);
            foreach(
$iResult as $iSchoolID){
                
$iSchool $iSchoolID;
            }
            
            
//
            // Gets selected course type ID
            //
            
            
$szSQL "SELECT id FROM coursetypes WHERE courseType = '$szCourseType' LIMIT 0,1";
            
$iResult $database->fetch($szSQL);
            foreach(
$iResult as $iCourseTypeID){
                
$iCourseType $iCourseTypeID;
            }
            
            
$aColumnNames = array('className''coursetype_id''startDate''endDate''startTime'
            
'endTime''price''instructor''prerequisites''description''equipment''school_id'
            
);
            
            
$aValue = array("'$szClassName'""'$iCourseType'""'$szStartDate'"
            
"'$szEndDate'""'$szStartTime'""'$szEndTime'""'$iPrice'""'$szInstructor'",
            
"'$szPrerequisities'""'$szDescription'""'$szEquipment'""'$iSchool'"
            
);
            
            
$szColumnNames '('.implode(", "$aColumnNames).')';
            
$szValues '('.implode(", "$aValue).')';
            
            
$szSQL "INSERT INTO courses $szColumnNames VALUES $szValues";
            
$bResult $database->execute($szSQL);
            
            if(
$bResult){
                
$tpl->assign('szSuccess''Successfully added new course');
            }
        
        }
        
        
//
        // Course Details
        //
        
        
if($_GET['details']){
            
            
$iID $_GET['details'];
            
$szSQL "SELECT * FROM courses WHERE id= $iID";
            
$aResult $database->execute($szSQL);
            while(
$szRow mysql_fetch_array($aResultMYSQL_ASSOC)){
                
$aData = array($szRow);
            }
            
            
$iCourseType $aData[0]['coursetype_id'];
            
            
$szSQL "SELECT courseType FROM coursetypes WHERE id=$iCourseType";
            
$aResult $database->fetch($szSQL);
            foreach(
$aResult as $szCourseType){
                
$tpl->assign('courseType'$szCourseType);
            }
            
            
$iSchool $aData[0]['school_id'];
            
            
$szSQL "SELECT name FROM schools WHERE id='$iSchool'";
            
$aResult $database->fetch($szSQL);
            foreach(
$aResult as $szSchool){
                
$tpl->assign('school'$szSchool);
            }
            
            
$tpl->assign('aCourseDetails'$aData);
            
        }
        
        
// 
        // Delete Course
        //
        
        
elseif($_GET['delete']){
            
            
$iID $_GET['delete'];
            
            
$szSQL "DELETE FROM courses WHERE id = '$iID'";
            
$bResult $database->execute($szSQL);
            if(
$bResult){
                
$tpl->assign('szSuccess''Successfully deleted the course');
            }
            
        }
        
        
//
        //
        // Edit Course
        //
        
        
elseif($_GET['edit']){
            
            
$iID $_GET['edit'];
            
            
//
            // Select all users with rank 2
            //
        
            
$szSQL "SELECT name FROM schools";
            
$szResult $database->execute($szSQL);
            while(
$szRow mysql_fetch_array($szResultMYSQL_ASSOC)){
                
$editASchool[] = $szRow['name'];
            }
        
            
$tpl->assign('editASchool'$editASchool);
        
            
//
            // Get all course types
            //
        
            
$szSQL "SELECT courseType FROM coursetypes";
            
$szResult $database->execute($szSQL);
            while(
$szRow mysql_fetch_array($szResultMYSQL_ASSOC)){
                
$editACourseType[] = $szRow['courseType'];
            }
            
$tpl->assign('editACourseType'$editACourseType);
            
            
//
            // Assigning post values to variables
            //
            
            
$szClassName $_POST['edit_className'];
            
$szCourseType $_POST['edit_courseType'];
            
            
$iStartDateYear $_POST['edit_startDateYear'];
            
$iStartDateMonth $_POST['edit_startDateMonth'];
            
$iStartDateDay $_POST['edit_startDateDay'];
            
$szStartDate $iStartDateYear.'-'.$iStartDateMonth.'-'.$iStartDateDay;
            
            
$iEndDateYear $_POST['edit_endDateYear'];
            
$iEndDateMonth $_POST['edit_endDateMonth'];
            
$iEndDateDay $_POST['edit_endDateDay'];
            
$szEndDate $iEndDateYear.'-'.$iEndDateMonth.'-'.$iEndDateDay;
            
            
$iStartTimeHour $_POST['edit_startTimeHour'];
            
$iStartTimeMinutes $_POST['edit_startTimeMinutes'];
            
$szStartTime $iStartTimeHour.':'.$iStartTimeMinutes;
            
            
$iEndTimeHour $_POST['edit_endTimeHour'];
            
$iEndTimeMinutes $_POST['edit_endTimeMinutes'];
            
$szEndTime $iEndTimeHour.':'.$iEndTimeMinutes;
            
            
$iPrice $_POST['edit_price'];
            
$szInstructor $_POST['edit_instructor'];
            
$szEquipment $_POST['edit_instructor'];
            
$szPrerequisities $_POST['edit_prerequisites'];
            
$szDescription $_POST['edit_description'];
            
$szSchool $_POST['edit_school'];
            
            if(
$szCourseType != ''){
                
$szSQL "SELECT id FROM coursetypes WHERE courseType = '$szCourseType' LIMIT 0,1";
                
$iResult $database->fetch($szSQL);
                foreach(
$iResult as $iCourseTypeID){
                    
$iCourseType $iCourseTypeID;
                }
            }
            
            if(
$szSchool != ''){
                
$szSQL "SELECT id FROM schools WHERE name = '$szSchool' LIMIT 0,1";
                
$iResult $database->fetch($szSQL);
                foreach(
$iResult as $iSchoolID){
                    
$iSchool $iSchoolID;
                }
            }
            
            
//
            // Constructing an array to update
            //
            
            
$aFields = array(
                            array(
'field' => 'className''value' => $szClassName),
                            array(
'field' => 'coursetype_id''value' => $iCourseType),
                            array(
'field' => 'startDate''value' => $szStartDate),
                            array(
'field' => 'endDate''value' => $szEndDate),
                            array(
'field' => 'startTime''value' => $szStartTime),
                            array(
'field' => 'endTime''value' => $szEndTime),
                            array(
'field' => 'price''value' => $iPrice),
                            array(
'field' => 'instructor''value' => $szInstructor),
                            array(
'field' => 'equipment''value' => $szEquipment),
                            array(
'field' => 'prerequisities''value' => $szPrerequisities),
                            array(
'field' => 'description''value' => $szDescription),
                            array(
'field' => 'school_id''value' => $iSchool)
                            );
                            
            
//
            // Makes a new array of filled fields
            //
            
            
foreach($aFields as $iKey => $szValue){
                if(
trim($szValue['value']) !=  ''){
                    
$aDataToUpdate[$szValue['field']] = $szValue['value'];
                }
            }
            
            if(isset(
$_POST['edit_course'])){
                if(
array_key_exists('startDate'$aDataToUpdate)){
                    if(
$aDataToUpdate['startDate'] == '--'){
                       unset(
$aDataToUpdate['startDate']);
                    }
                    elseif(
$aDataToUpdate['startDate'] != ''){
                        
$validate->isDate('szStartDate''Select all starting date parameters');
                    }
                }
                if(
array_key_exists('endDate'$aDataToUpdate)){
                    if(
$aDataToUpdate['endDate'] == '--'){
                       unset(
$aDataToUpdate['endDate']);
                    }
                    elseif(
$aDataToUpdate['endDate'] != ''){
                        
$validate->isDate('szEndDate''Select all ending date parameters');
                    }
                }
                if(
array_key_exists('startTime'$aDataToUpdate)){
                    if(
$aDataToUpdate['startTime'] == ':'){
                       unset(
$aDataToUpdate['startTime']);
                    }
                    elseif(
$aDataToUpdate['startTime'] != ''){
                        
$validate->isTime('szStartTime''Select all starting time parameters');
                    }
                }
                if(
array_key_exists('endTime'$aDataToUpdate)){
                    if(
$aDataToUpdate['endTime'] == ':'){
                       unset(
$aDataToUpdate['endTime']);
                    }
                    elseif(
$aDataToUpdate['endTime'] != ''){
                        
$validate->isTime('szEndTime''Select all ending time parameters');
                    }
                }
                
                if(
$validate->isError()){
                    
$tpl->assign('aError'$validate->getErrorList());
                }
                else{
                    
                    foreach(
$aDataToUpdate as $szColumnName => $szValue){
                        
$szSQL "UPDATE courses SET $szColumnName='$szValue' WHERE id=$iID";
                        
$bResult $database->execute($szSQL);
                    }
                    if(
$bResult){
                        
$tpl->assign('szSuccess''Successfully edited the course with ID '.$iID);
                    }
                }
                
            }
            
        }
        
        
// Table
        
        
if(isset($_POST['find_course'])){
            
            if(
$_POST['className'] != ''){
                
$szSQL sprintf("SELECT * FROM courses WHERE className LIKE '%%%s%%' ORDER BY id"$_POST['className']);
            }
            elseif(
$_POST['school'] != ''){
                
$szSQL sprintf("SELECT id FROM schools WHERE name LIKE '%%%s%%' ORDER BY id"$_POST['school']);
                
$aResult $database->execute($szSQL);
                
                while(
$iRow mysql_fetch_array($aResultMYSQL_ASSOC)){
                    
$iSearchSchoolID[] = $iRow['id'];
                }
                
                
$szQueryBit implode(" OR school_id = "$iSearchSchoolID);
                
$szSQL "SELECT * FROM courses WHERE school_id = $szQueryBit";
            }
            elseif(
$_POST['areaCode'] != ''){
                
$szSQL sprintf("SELECT id FROM schools WHERE areaCode LIKE '%d%%' ORDER BY id"$_POST['areaCode']);
                
$aResult $database->execute($szSQL);
                
                while(
$iRow mysql_fetch_array($aResultMYSQL_ASSOC)){
                    
$iSearchSchoolID[] = $iRow['id'];
                }
                
                
$szQueryBit implode(" OR school_id = "$iSearchSchoolID);
                
$szSQL "SELECT * FROM courses WHERE school_id = $szQueryBit";
            }
            elseif(
$_POST['duration'] != ''){
                
$iDurationInSeconds $_POST['duration'];
                
$szSQL "SELECT * FROM courses WHERE DATEDIFF(endDate, startDate) = $iDurationInSeconds ORDER BY id";
            }
            elseif(
$_POST['zipCode'] != ''){
                
$szSQL sprintf("SELECT id FROM schools WHERE zipCode LIKE '%d%%' ORDER BY id"$_POST['zipCode']);
                
$aResult $database->execute($szSQL);
                
                while(
$iRow mysql_fetch_array($aResultMYSQL_ASSOC)){
                    
$iSearchSchoolID[] = $iRow['id'];
                }
                
                
$szQueryBit implode(" OR school_id = "$iSearchSchoolID);
                
$szSQL "SELECT * FROM courses WHERE school_id = $szQueryBit";
            }
        }
        else{
            
$szSQL "SELECT * FROM courses ORDER BY id";
        }
        
        
$szResult $database->execute($szSQL);
        while(
$szRow mysql_fetch_array($szResultMYSQL_ASSOC)){
            
$aCourseID[] = $szRow['id'];
            
$aClassName[] = $szRow['className'];
            
$aSchoolID[] = $szRow['school_id'];
        }
            
        if(
$aSchoolID != array()){
            foreach(
$aSchoolID as $iKey => $iValue){
                
$szSQL "SELECT name FROM schools WHERE id=$iValue";
                
$szResult $database->execute($szSQL);
                while(
$szRow mysql_fetch_array($szResultMYSQL_ASSOC)){
                    
$aSchoolName[] = $szRow['name'];
                }
            }
        }
            
        
$tpl->assign('aCourseID'$aCourseID);
        
$tpl->assign('aClassName'$aClassName);
        
$tpl->assign('aSchoolName'$aSchoolName);
        
        
$tpl->display(ADMINTEMPLATE_PATH.'adminCourses.tpl.php'); 
        
    }
    
    
//
    // If user is a school user @school
    //
    
    
elseif($auth->school_auth() == True){
        
        include_once(
'navigation.php'); // Includes Navigation
        
        //
        // Checks if user is assigned
        //
        
        
$szUsername $_SESSION['username'];
        
$szSQL "SELECT assigned FROM users WHERE user = '$szUsername'";
        
$iResult $database->fetch($szSQL);
        foreach(
$iResult as $iAssigned){
            
$iAssignedUser $iAssigned;
        }
        
        if(
$iAssignedUser == 0){
            
header('location: admin.php');
            exit;
        }
        
        
//
        // Gets logged in user ID
        //

        
$szUsername $_SESSION['username'];
        
        
$szSQL "SELECT id FROM users WHERE user = '$szUsername' LIMIT 0,1";
        
$iResult $database->fetch($szSQL);
        foreach(
$iResult as $iUserID){
            
$iUser $iUserID;
        }
        
        
//
        // Gets logged in user associated school's ID
        //
            
        
$szSQL "SELECT id FROM schools WHERE user_id = '$iUser' LIMIT 0,1";
        
$iResult $database->fetch($szSQL);
        foreach(
$iResult as $iSchoolID){
            
$iSchool $iSchoolID;
        }
        
        
//
        // Assigning post values to variables
        //
        
        
$szClassName $_POST['className'];
        
$szCourseType $_POST['courseType'];
        
        
$iStartDateYear $_POST['startDateYear'];
        
$iStartDateMonth $_POST['startDateMonth'];
        
$iStartDateDay $_POST['startDateDay'];
        
$szStartDate $iStartDateYear.'-'.$iStartDateMonth.'-'.$iStartDateDay;
        
        
$iEndDateYear $_POST['endDateYear'];
        
$iEndDateMonth $_POST['endDateMonth'];
        
$iEndDateDay $_POST['endDateDay'];
        
$szEndDate $iEndDateYear.'-'.$iEndDateMonth.'-'.$iEndDateDay;
        
        
$iStartTimeHour $_POST['startTimeHour'];
        
$iStartTimeMinutes $_POST['startTimeMinutes'];
        
$szStartTime $iStartTimeHour.':'.$iStartTimeMinutes;
        
        
$iEndTimeHour $_POST['endTimeHour'];
        
$iEndTimeMinutes $_POST['endTimeMinutes'];
        
$szEndTime $iEndTimeHour.':'.$iEndTimeMinutes;
        
        
$iPrice $_POST['price'];
        
$szInstructor $_POST['instructor'];
        
$szEquipment $_POST['instructor'];
        
$szPrerequisities $_POST['prerequisites'];
        
$szDescription $_POST['description'];
        
        
        
//
        // Validation rules
        //
        
        
if(isset($_POST['add_course'])){
            
$validate->isEmpty('szClassName''Please enter a class name');
            
$validate->isEmpty('szCourseType''Select course type');
            
$validate->isDate('szStartDate''Select all starting time paramters');
            
$validate->isDate('szEndDate''Select all ending time paramters');
            
$validate->isTime('szStartTime''Select all starting time paramters');
            
$validate->isEmpty('szStartTime''Select all ending time paramters');
            
$validate->isEmpty('iEndTimeMinutes''Select end time minutes');
            
$validate->isEmpty('iPrice''Enter a price');
            
$validate->isEmpty('szInstructor''Enter instructor name');
            
$validate->isEmpty('szEquipment''Please enter equipment details');
            
$validate->isEmpty('szPrerequisities''Please enter prerequisities details');
            
$validate->isEmpty('szDescription''Please enter a description');
        }
        
        
//
        // Get all course types
        //
        
        
$szSQL "SELECT courseType FROM coursetypes";
        
$szResult $database->execute($szSQL);
        while(
$szRow mysql_fetch_array($szResultMYSQL_ASSOC)){
            
$aCourseType[] = $szRow['courseType'];
        }
        
$tpl->assign('aCourseType'$aCourseType);
        
        
//
        // Assigns errors to add course form
        //
        
        
if($validate->isError() && isset($_POST['add_course'])){
            
$tpl->assign('aError'$validate->getErrorList());
        }
        
        
//
        // If no errors found, add the new course
        //
        
        
        
if(isset($_POST['add_course']) && $validate->isError() == false){
            
            
//
            // Gets selected course type ID
            //
            
            
$szSQL "SELECT id FROM coursetypes WHERE courseType = '$szCourseType' LIMIT 0,1";
            
$iResult $database->fetch($szSQL);
            foreach(
$iResult as $iCourseTypeID){
                
$iCourseType $iCourseTypeID;
            }
            
            
$aColumnNames = array('className''coursetype_id''startDate''endDate''startTime'
            
'endTime''price''instructor''prerequisites''description''equipment''school_id'
            
);
            
            
$aValue = array("'$szClassName'""'$iCourseType'""'$szStartDate'"
            
"'$szEndDate'""'$szStartTime'""'$szEndTime'""'$iPrice'""'$szInstructor'",
            
"'$szPrerequisities'""'$szDescription'""'$szEquipment'""'$iSchool'"
            
);
            
            
$szColumnNames '('.implode(", "$aColumnNames).')';
            
$szValues '('.implode(", "$aValue).')';
            
            
$szSQL "INSERT INTO courses $szColumnNames VALUES $szValues";
            
$bResult $database->execute($szSQL);
            
            if(
$bResult){
                
$tpl->assign('szSuccess''Successfully added new course');
            }
        
        }
        
        
//
        // Course Details
        //
        
        
if($_GET['details']){
            
            
$iID $_GET['details'];
            
            
$szSQL "SELECT * FROM courses WHERE id= $iID";
            
$aResult $database->execute($szSQL);
            while(
$szRow mysql_fetch_array($aResultMYSQL_ASSOC)){
                
$aData = array($szRow);
            }
            
            
//
            // Checks if school user quering is assigned to the course
            //
            
            
if($aData[0]['school_id'] != $iSchool){
                echo 
'You are not assigned to this course <br/>';
                echo 
'<a href="admin.php">Go Back</a>';
                exit;
            }
            
            
$iCourseType $aData[0]['coursetype_id'];
            
            
$szSQL "SELECT courseType FROM coursetypes WHERE id=$iCourseType";
            
$aResult $database->fetch($szSQL);
            foreach(
$aResult as $szCourseType){
                
$tpl->assign('courseType'$szCourseType);
            }
            
            
$iSchool $aData[0]['school_id'];
            
            
$szSQL "SELECT name FROM schools WHERE id='$iSchool'";
            
$aResult $database->fetch($szSQL);
            foreach(
$aResult as $szSchool){
                
$tpl->assign('school'$szSchool);
            }
                
            
$tpl->assign('aCourseDetails'$aData);
            
        }
        
        
// 
        // Delete Course
        //
        
        
elseif($_GET['delete']){
            
            
$iID $_GET['delete'];
            
            
$szSQL "SELECT * FROM courses WHERE id= $iID";
            
$aResult $database->execute($szSQL);
            while(
$szRow mysql_fetch_array($aResultMYSQL_ASSOC)){
                
$aData = array($szRow);
            }
            
            
//
            // Checks if school user quering is assigned to the course
            //
            
            
if($aData[0]['school_id'] != $iSchool){
                echo 
'You are not assigned to this course <br/>';
                echo 
'<a href="admin.php">Go Back</a>';
                exit;
            }
            
            
$szSQL "DELETE FROM courses WHERE id = '$iID'";
            
$bResult $database->execute($szSQL);
            if(
$bResult){
                
$tpl->assign('szSuccess''Successfully deleted the course');
            }
            
        }
        
        
//
        //
        // Edit Course
        //
        
        
elseif($_GET['edit']){
            
            
$iID $_GET['edit'];
            
            
$szSQL "SELECT * FROM courses WHERE id= $iID";
            
$aResult $database->execute($szSQL);
            while(
$szRow mysql_fetch_array($aResultMYSQL_ASSOC)){
                
$aData = array($szRow);
            }
            
            
//
            // Checks if school user quering is assigned to the course
            //
            
            
if($aData[0]['school_id'] != $iSchool){
                echo 
'You are not assigned to this course <br/>';
                echo 
'<a href="admin.php">Go Back</a>';
                exit;
            }
            
            
//
            // Select all users with rank 2
            //
        
            
$szSQL "SELECT name FROM schools";
            
$szResult $database->execute($szSQL);
            while(
$szRow mysql_fetch_array($szResultMYSQL_ASSOC)){
                
$editASchool[] = $szRow['name'];
            }
        
            
$tpl->assign('editASchool'$editASchool);
        
            
//
            // Get all course types
            //
        
            
$szSQL "SELECT courseType FROM coursetypes";
            
$szResult $database->execute($szSQL);
            while(
$szRow mysql_fetch_array($szResultMYSQL_ASSOC)){
                
$editACourseType[] = $szRow['courseType'];
            }
            
$tpl->assign('editACourseType'$editACourseType);
            
            
//
            // Assigning post values to variables
            //
            
            
$szClassName $_POST['edit_className'];
            
$szCourseType $_POST['edit_courseType'];
            
            
$iStartDateYear $_POST['edit_startDateYear'];
            
$iStartDateMonth $_POST['edit_startDateMonth'];
            
$iStartDateDay $_POST['edit_startDateDay'];
            
$szStartDate $iStartDateYear.'-'.$iStartDateMonth.'-'.$iStartDateDay;
            
            
$iEndDateYear $_POST['edit_endDateYear'];
            
$iEndDateMonth $_POST['edit_endDateMonth'];
            
$iEndDateDay $_POST['edit_endDateDay'];
            
$szEndDate $iEndDateYear.'-'.$iEndDateMonth.'-'.$iEndDateDay;
            
            
$iStartTimeHour $_POST['edit_startTimeHour'];
            
$iStartTimeMinutes $_POST['edit_startTimeMinutes'];
            
$szStartTime $iStartTimeHour.':'.$iStartTimeMinutes;
            
            
$iEndTimeHour $_POST['edit_endTimeHour'];
            
$iEndTimeMinutes $_POST['edit_endTimeMinutes'];
            
$szEndTime $iEndTimeHour.':'.$iEndTimeMinutes;
            
            
$iPrice $_POST['edit_price'];
            
$szInstructor $_POST['edit_instructor'];
            
$szEquipment $_POST['edit_instructor'];
            
$szPrerequisities $_POST['edit_prerequisites'];
            
$szDescription $_POST['edit_description'];
            
$szSchool $_POST['edit_school'];
            
            if(
$szCourseType != ''){
                
$szSQL "SELECT id FROM coursetypes WHERE courseType = '$szCourseType' LIMIT 0,1";
                
$iResult $database->fetch($szSQL);
                foreach(
$iResult as $iCourseTypeID){
                    
$iCourseType $iCourseTypeID;
                }
            }
            
            if(
$szSchool != ''){
                
$szSQL "SELECT id FROM schools WHERE name = '$szSchool' LIMIT 0,1";
                
$iResult $database->fetch($szSQL);
                foreach(
$iResult as $iSchoolID){
                    
$iSchool $iSchoolID;
                }
            }
            
            
//
            // Constructing an array to update
            //
            
            
$aFields = array(
                            array(
'field' => 'className''value' => $szClassName),
                            array(
'field' => 'coursetype_id''value' => $iCourseType),
                            array(
'field' => 'startDate''value' => $szStartDate),
                            array(
'field' => 'endDate''value' => $szEndDate),
                            array(
'field' => 'startTime''value' => $szStartTime),
                            array(
'field' => 'endTime''value' => $szEndTime),
                            array(
'field' => 'price''value' => $iPrice),
                            array(
'field' => 'instructor''value' => $szInstructor),
                            array(
'field' => 'equipment''value' => $szEquipment),
                            array(
'field' => 'prerequisities''value' => $szPrerequisities),
                            array(
'field' => 'description''value' => $szDescription),
                            array(
'field' => 'school_id''value' => $iSchool)
                            );
                            
            
//
            // Makes a new array of filled fields
            //
            
            
foreach($aFields as $iKey => $szValue){
                if(
trim($szValue['value']) !=  ''){
                    
$aDataToUpdate[$szValue['field']] = $szValue['value'];
                }
            }
            
            if(isset(
$_POST['edit_course'])){
                if(
array_key_exists('startDate'$aDataToUpdate)){
                    if(
$aDataToUpdate['startDate'] == '--'){
                       unset(
$aDataToUpdate['startDate']);
                    }
                    elseif(
$aDataToUpdate['startDate'] != ''){
                        
$validate->isDate('szStartDate''Select all starting date parameters');
                    }
                }
                if(
array_key_exists('endDate'$aDataToUpdate)){
                    if(
$aDataToUpdate['endDate'] == '--'){
                       unset(
$aDataToUpdate['endDate']);
                    }
                    elseif(
$aDataToUpdate['endDate'] != ''){
                        
$validate->isDate('szEndDate''Select all ending date parameters');
                    }
                }
                if(
array_key_exists('startTime'$aDataToUpdate)){
                    if(
$aDataToUpdate['startTime'] == ':'){
                       unset(
$aDataToUpdate['startTime']);
                    }
                    elseif(
$aDataToUpdate['startTime'] != ''){
                        
$validate->isTime('szStartTime''Select all starting time parameters');
                    }
                }
                if(
array_key_exists('endTime'$aDataToUpdate)){
                    if(
$aDataToUpdate['endTime'] == ':'){
                       unset(
$aDataToUpdate['endTime']);
                    }
                    elseif(
$aDataToUpdate['endTime'] != ''){
                        
$validate->isTime('szEndTime''Select all ending time parameters');
                    }
                }
                
                if(
$validate->isError()){
                    
$tpl->assign('aError'$validate->getErrorList());
                }
                else{
                    
                    foreach(
$aDataToUpdate as $szColumnName => $szValue){
                        
$szSQL "UPDATE courses SET $szColumnName='$szValue' WHERE id=$iID";
                        
$bResult $database->execute($szSQL);
                    }
                    if(
$bResult){
                        
$tpl->assign('szSuccess''Successfully edited the course with ID '.$iID);
                    }
                }
                
            }
            
        }
        
        
// Table
        
        
if(isset($_POST['find_course'])){
            if(
$_POST['className'] != ''){
                
$szSQL sprintf("SELECT * FROM courses WHERE className LIKE '%%%s%%' AND school_id = %d ORDER BY id"$_POST['className'], $iSchool);
            }
            elseif(
$_POST['duration'] != ''){
                
$iDurationInSeconds $_POST['duration'];
                
$szSQL "SELECT * FROM courses WHERE DATEDIFF(endDate, startDate) = $iDurationInSeconds AND school_id = $iSchool ORDER BY id";
            }
        }
        else{
            
$szSQL "SELECT * FROM courses WHERE school_id = $iSchool ORDER BY id";
        }
        
        
$szResult $database->execute($szSQL);
        while(
$szRow mysql_fetch_array($szResultMYSQL_ASSOC)){
            
$aCourseID[] = $szRow['id'];
            
$aClassName[] = $szRow['className'];
        }
        
        
$tpl->assign('aCourseID'$aCourseID);
        
$tpl->assign('aClassName'$aClassName);
        
        
$tpl->display(ADMINTEMPLATE_PATH.'schoolCourses.tpl.php');
        
    }
    
?>
This is my courses.php. Let me know your feedback on my code , its structure, naming convections and the comments.
Haris is offline  
Reply With Quote
Old 10-07-2007, 05:04 PM   #2 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

One question, why do you have $i in the beginning of each variable?
Or $sz?

This is not feedback on your code, I'm just wondering..
Tanax is offline  
Reply With Quote
Old 10-07-2007, 05:35 PM   #3 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

Quote:
Originally Posted by Tanax View Post
One question, why do you have $i in the beginning of each variable?
Or $sz?

This is not feedback on your code, I'm just wondering..
Variable Type:

sz = String
i = Integer
a = Array
b = Boolean
Haris is offline  
Reply With Quote
Old 10-07-2007, 05:58 PM   #4 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Ah I see :)
What's a boolean? :P(newbie question)
Tanax is offline  
Reply With Quote
Old 10-07-2007, 06:00 PM   #5 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

Quote:
Originally Posted by Tanax View Post
Ah I see :)
What's a boolean? :P(newbie question)
True or false, yes or no, 0 or 1
Haris is offline  
Reply With Quote
Old 10-07-2007, 07:53 PM   #6 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Ah, I see :)
Tanax is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 05:09 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design