Manipulating values from database table with php

Posted by charliecodex23 on Stack Overflow See other posts from Stack Overflow or by charliecodex23
Published on 2012-11-02T04:45:23Z Indexed on 2012/11/02 5:00 UTC
Read the original article Hit count: 137

Filed under:

I currently have 5 tables in MySQL database. Some of them share foreign keys and are interdependent of each other. I am displaying classes accordingly to their majors. Each class is taught during the fall, spring or all_year. In my database I have a table named semester which has an id, year, and semester fields. The semester field in particular is a tinyint that has three values 0, 1, 2. This signifies the fall, spring or all_year. When I display the query instead of having it show 0 or 1 or 2 can I have it show fall, spring etc? Extra: How can I add space to the end of each loop so the data doesn't look clustered?

Key

0 Fall
1 Spring
2 All-year

PHP

<?


try {

    $pdo = new PDO ("mysql:host=$hostname;dbname=$dbname","$username","$pw");
    } catch (PDOException $e) {
    echo "Failed to get DB handle: " . $e->getMessage() . "\n";
    exit;
  }
      $query = $pdo->prepare("SELECT course.name, course.code, course.description, course.hours, semester.semester, semester.year
                            FROM course
                            LEFT JOIN major_course_xref ON course.id = major_course_xref.course_id
                            LEFT JOIN major ON major.id = major_course_xref.major_id
                            LEFT JOIN course_semester_xref ON course.id = course_semester_xref.course_id
                            LEFT JOIN semester ON course_semester_xref.semester_id = semester.id");
      $query->execute();

     if ($query->execute()){

      while ($row = $query->fetch(PDO::FETCH_ASSOC)){       
        print $row['name'] . "<br>";
        print $row['code'] . "<br>";
        print $row['description'] . "<br>";
        print $row['hours'] . " hrs.<br>";
        print $row['semester'] . "<br>";
        print $row['year'] . "<br>";
    }
    }
else
    echo 'Could not fetch results.';

      unset($pdo); 
      unset($query);

?>

Current Display

Computer Programming I
CPSC1400
Introduction to disciplined, object-oriented program development.
4 hrs.
0
2013

Desire Display

Computer Programming I
CPSC1400
Introduction to disciplined, object-oriented program development.
4 hrs.
Fall
2013

© Stack Overflow or respective owner

Related posts about php