I like to use the popular Bootstrap CSS template because it's responsive and allows me to focus mainly on the code while also creating a pretty decent looking web application. For more info on Bootstrap see https://getbootstrap.com
Let's create an index.php script using a Bootstrap template.
index.php - Student Listing: using Bootstrap Template
Now let's add code for querying the database for all of our students:
index.php - Student Listing: Querying for all Students
1 2 3 4 5 6 7 8 9101112131415161718192021222324
... <body> <div class="card"> <div class="card-body"> <h1>Students</h1><?phprequire_once('dbconnection.php');$dbc=mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME)ortrigger_error('Error connecting to MySQL server for'.DB_NAME,E_USER_ERROR);$query="SELECT id, first_name, last_name "."FROM studentListing ORDER BY last_name, first_name";$result=mysqli_query($dbc,$query)ortrigger_error('Error querying database studentListing',E_USER_ERROR);</div></div>...</body>...
Displaying the Query Results for All Students with a Link to the Details
Depending upon the results, we'll either display a table listing all of the students' first and last name with a link to a studentdetails.php page, or that no students were found. So let's add the following code:
index.php - Student Listing: Displaying all Students with Link to Student Details
...<?phprequire_once('dbconnection.php');$dbc=mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME)ortrigger_error('Error connecting to MySQL server for'.DB_NAME,E_USER_ERROR);$query="SELECT id, first_name, last_name "."FROM studentListing ORDER BY last_name, first_name";$result=mysqli_query($dbc,$query)ortrigger_error('Error querying database studentListing',E_USER_ERROR);if(mysqli_num_rows($result)>0):?> <table class="table table-striped"> <thead> <tr> <th scope="col">Student Name</th> </tr> </thead> <tbody><?phpwhile($row=mysqli_fetch_assoc($result)){echo"<tr><td>"."<a class='nav-link' href='studentdetails.php?id=".$row['id']."'>".$row['first_name'].' '.$row['last_name']."</a></td></tr>";}?> </tbody> </table> <?phpelse:?> <h3>No Students Found :-(</h3><?phpendif;?> </div> </div> ... </body> ...
Notice the line that echos out table rows of each student's name linking to (the yet to be written) studentdetails.php along with the id field from the query:
index.php - Student Listing: Link with Query Parameter
Let's head back over to the lecture notes to discuss the difference between HTTP GET and POST and how to use GET in this case to send query parameters.