13 MySQL - Join Table
13 MySQL - Join Table
home.page.html completelist.page.html
Students
Users
id studentId country capital
1 1 Malaysia Johor
2 29 Brunei Banda Seri Begawan
3 2 Singapore Singapore
Create 2 tables – students and users in the ionic database
Students table
--
-- Database: `ionic`
--
-- --------------------------------------------------------
--
-- Table structure for table `students`
--
--
-- Dumping data for table `students`
--
--
-- Indexes for table `students`
--
ALTER TABLE `students`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `students`
--
ALTER TABLE `students`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=58;
COMMIT;
--
-- Database: `ionic`
--
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
--
-- Dumping data for table `users`
--
--
-- Indexes for dumped tables
--
--
-- Indexes for table `users`
--
ALTER TABLE `users`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;
COMMIT;
.htaccess
RewriteEngine on
RewriteRule ^api/students/?$ api/app.php
RewriteRule ^api/students/([0-9]+)?$ api/app.php?id=$1
RewriteRule ^api/stud2/?$ api/app2.php
RewriteRule ^api/stud2/([0-9]+)?$ api/app2.php?id=$1
Create a new file, called app2.php in the api folder. Add these codes.
app2.php
<?php
require ("headers.php");
if($_SERVER['REQUEST_METHOD'] === 'GET'){
//echo 'get';
if(isset($_GET['id'])){
$id = $conn->real_escape_string($_GET['id']);
$sql = $conn->query("select * from students where id='$id'");
$data= $sql->fetch_assoc();
}
else {
$data = array();
$query = "SELECT
students.name,
students.phone,
students.email,
users.country,
users.capital
FROM students INNER JOIN users
ON users.studentId=students.id";
$sql = $conn->query($query);
while($d = $sql->fetch_assoc()){
$data[] = $d;
}
}
exit (json_encode($data)); //return json data
}
?>
Create a new service called as jointable service.
ionic g service services/jointable
jointable.ts
constructor(
private http: HttpClient
) { }
getAll(){
return this.http.get<[StudentUser]>(this.url);
}
get(id: string){
return this.http.get<StudentUser>(this.url + '/'+ id);
}
}
Create a new page called as completelist
ionic g page completelist
completelist.page.ts
@Component({
selector: 'app-completelist',
templateUrl: './completelist.page.html',
styleUrls: ['./completelist.page.scss'],
})
export class CompletelistPage implements OnInit {
completestudentlist: StudentUser[];
constructor(
private service: JointableService
) { }
ngOnInit() {
this.service.getAll().subscribe(response => {
//console.log(response);
this.completestudentlist = response;
});
}
Completelist.page.html
<ion-header [translucent]="true">
<ion-toolbar color="secondary">
<ion-buttons slot="start">
<ion-back-button defaultHref="home"></ion-back-button>
</ion-buttons>
<ion-title>
Complete List
</ion-title>
</ion-toolbar>
</ion-header>
<ion-content class="ion-padding">
<ion-list *ngFor = "let student of completestudentlist">
<ion-item >
<ion-avatar slot="start">
<div class="avatar">
{{student.name.substring(3,6).toUpperCase()}}
</div>
</ion-avatar>
<ion-label>
<h3 class="title">{{student.name}}</h3>
<p class="subtext">{{'Email: ' + student.email}}</p>
<p class="subtext">{{'Phone: ' + student.phone}}</p>
<p class="subtext">{{'Country: ' + student.country}}</p>
<p class="subtext">{{'Capital: ' + student.capital}}</p>
</ion-label>
</ion-item>
</ion-list>
</ion-content>
completelist.page.scss
.avatar {
background-color: blueviolet;
width: 100%;
height: 100%;
border-radius: 50px;
display: flex;
justify-content: center;
align-items: center;
color:aliceblue;
.subtext{
font-size: 80%;
margin-top: 0;
color: #8b979f;
}
.title{
font-weight: 600;
font-size: 16px;
}
You can edit your home.page.ts to add the link to the completelist page as below.
home.page.html
<ion-header [translucent]="true">
<ion-toolbar color="secondary">
<ion-buttons slot="end">
<ion-button color="dark" (click)="addStudent()">
<ion-icon name="add-circle"></ion-icon>
</ion-button>
</ion-buttons>
<ion-title>
Home
</ion-title>
</ion-toolbar>
</ion-header>
<ion-content class="ion-padding">
<ion-fab vertical="bottom" horizontal="end" slot="fixed">
<ion-fab-button [routerLink]="['/completelist']">
<ion-icon name="globe"></ion-icon>
</ion-fab-button>
</ion-fab>