|
-
May 17th, 2010, 10:53 AM
#1
Thread Starter
Addicted Member
Making links with PHP
Hello Folks,
I have a database called students. I also have two tables in it called current_students and subjects. One table holds all the names of students while the other holds subjects offered.
I have an HTML page that shows current students. I call it current_students.php. When I view this page it shows all the current students in my database. I want to be able to see the subjects that each current student is enrolled on when I click on their names. So, I created another HTML page and called it subjects.php. At the moment, I have a page that shows all the subjects students are doing.
But my problem now is, how do I show the names of current students and when I click on a particular student another page opens and shows me the subject that particular student is doing? I am not using any form in my design, but you can see the code for the two pages below please.
The code below is for the current_students.php page.
Code:
<html>
<head><title>Making links from one page to another</title>
</head>
<body>
<?php
//set the database login details
$hostname = "localhost";
$username = "root";
$password = "";
//connecting to the database
$conn = mysql_connect($hostname, $username, $password)
or die("There was an error connecting" .mysql_error());
//select the student database to use
mysql_select_db("students", $conn);
$result = mysql_query("SELECT FirstName, LastName FROM current_students ORDER BY FirstName ASC");
echo "<strong>Current students " ."</strong>" ."<br/>";
while ($row = mysql_fetch_array($result))
{
echo $row{'FirstName'} . " ". " " ." " .$row{'LastName'} . "<br/>" ;
}
mysql_close($conn);
?>
</body>
</html>
The code below is for the subjects.php page.
Code:
<html>
<head><title>subjects by current students</title>
</head>
<body>
<?php
//set the database login details
$hostname = "localhost";
$username = "root";
$password = "";
//connecting to the database
$conn = mysql_connect($hostname, $username, $password)
or die("There was an error connecting" .mysql_error());
//select the student database to use
mysql_select_db("students", $conn);
$result = mysql_query("SELECT Subject_Name FROM subjects ORDER BY Subject_Name ASC");
echo "<strong>Subjects enrolled on " ."</strong>" ."<br/>";
while ($row = mysql_fetch_array($result))
{
echo $row{'Subject_Name'} ."<br/>" ;
}
mysql_close($conn);
?>
</body>
</html>
This is the sql code for the structures of the tables.
Code:
//I created the current_students table through phpmyadmin
CREATE TABLE current_students (
id INT (4) NOT NULL AUTO_INCREMENT PRIMARY KEY,
FirstName VARCHAR (20) NOT NULL,
LastName VARCHAR (20) NOT NULL);
//I created the subjects table with this code through phpmyadmin
CREATE TABLE subjects (
id INT (4) NOT NULL AUTO_INCREMENT PRIMARY KEY,
Subject_Name VARCHAR (50) NOT NULL,
term VARCHAR (20) NOT NULL);
//I inserted the subjects into the table with the code below throw phpmyadmin
INSERT INTO `students`.`subjects` (`id`, `Subject_Name`, `term`) VALUES (NULL,
'History', 'First term'), (NULL, 'Geography', 'Second term');
INSERT INTO `students`.`subjects` (`id`, `Subject_Name`, `term`) VALUES (NULL,
'Economics', 'Third term'), (NULL, 'ICT', 'Second term');
INSERT INTO `students`.`subjects` (`id`, `Subject_Name`, `term`) VALUES (NULL,
'Mathematics', 'Third term'), (NULL, 'Physics', 'First term');
INSERT INTO `students`.`subjects` (`id`, `Subject_Name`, `term`) VALUES (NULL,
'Programming', 'second term'), (NULL, 'Arts', 'First term');
INSERT INTO `students`.`subjects` (`id`, `Subject_Name`, `term`) VALUES (NULL,
'Swimming', 'Third term'), (NULL, 'Football', 'First term');
INSERT INTO `students`.`subjects` (`id`, `Subject_Name`, `term`) VALUES (NULL,
'Mountain climbing', 'Second term'), (NULL, 'Music', 'Second term');
Looking at the images, what I want to do is, for example, when I click on Anna Benda it show me another page with the subjects she is enrolled on.
Am I on the right process of achieving this goal? If not, could someone suggest to me how to do it please? I will appreciate your help.
Thanks,
Menre
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|