user1434156
user1434156

Reputation:

Manipulating values from database table with php

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

Upvotes: 0

Views: 100

Answers (3)

itachi
itachi

Reputation: 6393

make another table. say semester_time.

|  id  |  semester_time  |
|  0   |  Fall           |
|  1   |  Spring         |
|  2   |  All_year       |

Then just do a join and retreive through semester_time.

Possible model for the query based on above table:

SELECT course.name, course.code, course.description, course.hours, semester.semester, semester.year, semester_time 
                            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
                            LEFT JOIN semester_time ON semester.semester = semester_time.id

To add space after each loop


 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>";
        echo "<div class='add-space'></div>";
    }

in the css

.add-space {
margin-bottom: 15px; 
}

just change the value to suit your needs.

Upvotes: 1

Miqdad Ali
Miqdad Ali

Reputation: 6147

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'] == 0) ? "FALL":( ($row['semester'] == 1) ? "Spring" : ( ($row['semester'] == 2) ? "All-year":"")) . "<br>";
        print $row['year'] . "<br>";
        print "<br>------------------------------<br />";
    }

Upvotes: 0

Barmar
Barmar

Reputation: 781493

Replace semester.semester in the query with:

CASE semester.semester
  WHEN 0 THEN 'Spring'
  WHEN 1 THEN 'Fall'
  WHEN 2 THEN 'All-year'
END semester

Upvotes: 0

Related Questions